Clarke and MST
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 315 Accepted Submission (s): 176
Problem Descriptionclarke is a patient with multiple personality disorder. One day he turned into a learner of graph theory.
He learned some algorithms of minimum spanning tree. Then he had a good idea that he wanted to find the maximum spanning tree with bit operation and.
A spanning tree is composed byn−1 Edges. Each of points ofn points can reach each other. The size of a spanning tree is generated by bit operation and with values of n−1 Ed Ges.
Now he wants the maximum spanning tree.
Inputthe first line contains an integerT(1≤t≤5) , the number of test cases.
For each test case, the first line contains the integersn,m(2≤n≤300000,1≤m≤300000) , denoting the number of points and the number of edge respectively.
ThenmLines followed, each line contains three integersX,y , w ( 1≤x ,y ≤n 0≤ w≤ 10 9) , denoting an edge betweenx,y With valueW.
The number of test case with n,m>100000 would not exceed 1.
Outputfor each test case, print a line contained an integer represented the answer. If There is no any spanning tree, print 0.
Sample Input14 2 3 to 4 3 4 7 sample Output1 from large to small bitwise enumeration. Because it is & so take out the side of this bit 1, see if these sides can form a spanning tree. See the code specifically. (This is used to determine whether the set is not a tree, you can also use BFS or DFS)
/************************************************author:guanjuncreated time:2016/2/16 19:02:06File Name: bc72c.cpp*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 300000+10#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespacestd;intfa[maxn],w[maxn],p[maxn],q[maxn],n,m;intFINDFA (intx) { if(X==fa[x])returnx; returnfa[x]=Findfa (fa[x]);}voidUnion (intAintb) { intx=Findfa (a); inty=Findfa (b); if(x>y) fa[x]=y; Else if(y>x) fa[y]=x;}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif //freopen ("OUT.txt", "w", stdout); intt,x,y,z; CIN>>T; while(t--) {scanf ("%d%d",&n,&m); for(intI=0; i<m;i++) {scanf ("%d%d%d",&x,&y,&z); W[i]=Z; P[i]=x; Q[i]=y; } intans=0; for(intI= -; i>=0; i--) {x=(1<<i); for(intIi=1; ii<=n;ii++) fa[ii]=II; for(intj=0; j<m;j++){ if((W[j]&x) && ((w[j]&ans) = =ans)) { //cout<<p[j]<< "" <<q[j]<<endl;Union (P[j],q[j]); } } intf=fa[1]; intmark=1; for(intIi=1; ii<=n;ii++){ if(fa[ii]!=f) {Mark=0; Break; } } if(Mark) ans+=x; } printf ("%d\n", ans); } return 0;}
HDU 5627Clarke and MST