"Bzoj" 1016: [JSOI2008] minimum spanning tree count (kruskal+ special tricks)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=1016

I can't imagine Qaq.

The first thing to think of is: the topic has said, with the same weight value of the side will not exceed 10.

Second: Always think of how the combination counts .... So the worst of the violence is not.

So this is a violent mess, Orz.

By adding edges in turn, the number of scenarios for each edge is the scheme.

Note that the collection cannot be compressed, otherwise it will be waqaq because the path compression of the check set is irreversible Qaq

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long long ll; #define PII pair<int, int> #define MKPII make_pair<int, int> #define PDI Pair<double, in T> #define MKPDI make_pair<double, int> #define PLI pair<ll, int> #define MKPLI make_pair<ll, int># Define REP (I, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define FOR2 (i,a,n) for (int i= (a);i< (n); ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= (a);i> (n); Define CC (I,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) #define DBG (x) cout <& Lt (#x) << "=" << (x) << endl#define error (x) (!) ( x) puts ("error"): 0) #define PRINTARR2 (A, B, c) For1 (_, 1, b) {For1 (__, 1, c) cout << a[_][__]; cout &LT;&LT Endl } #define PRINTARR1 (A, B) For1 (_, 1, b) cout << a[_] << ' \ t '; cout << endlinline const int Getint () {int r=0, k=1; char C=getchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }inline const int MAX (const int &a, const int &b) {return a>b?a:b;} inline const int min (const int &AMP;A, const int &b) {return a<b?a:b;} const int n=105, m=1005, Md=31011;int N, M, CNT, p[n], Ans=1, sum;struct dat {int x, y, W;} E[M], A[m];inline const BOOL CMP (const DAT &a, const dat &b) {return a.w<b.w;} inline const int ifind (const int &x) {return x==p[x]?x:ifind (p[x]);} void Dfs (int now, int s, const int &x) {if (NOW&GT;A[X].Y) {if (S==A[X].W) ++sum;return;} DFS (now+1, S, x); int Fx=ifind (e[now].x), Fy=ifind (E[NOW].Y), if (fx!=fy) {p[fx]=fy; Dfs (now+1, s+1, x); p[fx]=fx; p[fy]=fy ; }}int Main () {read (n); read (m); For1 (i, 1, m) read (e[i].x), read (E[I].Y), read (E[I].W); For1 (i, 1, N) p[i]=i;sort (e+1, E+1+m, CMP), int ed=0;for1 (i, 1, m) {if (E[I].W!=E[I-1].W) a[++cnt].x=i, a[cnt-1].y= I-1;int Fx=ifind (e[i].x), Fy=ifind (E[I].Y), if (fx!=fy) {p[fx]=fy;++a[cnt].w;++ed;}} if (ed!=n-1) {puts ("0"); return 0;} A[cnt].y=m;for1 (i, 1, N) p[i]=i;for1 (i, 1, CNT) {Sum=0;dfs (a[i].x, 0, I), ans= (Ans*sum)%md;for1 (J, a[i].x, a[i].y) {int fx= IFind (e[j].x), Fy=ifind (E[J].Y), if (Fx!=fy) p[fx]=fy;}} print (ANS); return 0;}

  

Description

A simple, non-weighted graph is now given. You are not satisfied with finding the smallest spanning tree of the graph, but want to know how many different minimum spanning trees are in the graph. (If there is at least one edge in the two smallest spanning trees, the two minimum spanning trees are different). Because the different minimum spanning tree may be many, you only need to output the scheme number to 31011 modulo.

Input

The first line contains two numbers, N and M, where 1<=n<=100; 1<=m<=1000; Represents the number of nodes and sides of the graph. Each node is numbered with an integer of 1~n. The next M-line, each line contains two integers: A, B, C, the weight of the edge between Node A and B is C, where 1<=c<=1,000,000,000. Data guarantees that no self-back and heavy edges are present. Note: An edge with the same weight will not exceed 10 bars.

Output

How many of the different minimum spanning trees are output. You just need to output a number of 31011 modulo.

Sample Input4 6
1 2 1
1 3 1
1 4 1
2 3 2
2 4 1
3 4 1Sample Output8hint Source

"Bzoj" 1016: [JSOI2008] minimum spanning tree count (kruskal+ special tricks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.