并查集加一些非常玄幻的操作,主要就是b数组的神奇运用
1 #include<set> 2 #include<map> 3 #include<list> 4 #include<queue> 5 #include<stack> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<vector> 10 #include<bitset> 11 #include<memory> 12 #include<utility> 13 #include<cstdio> 14 #include<sstream> 15 #include<iostream> 16 #include<cstdlib> 17 #include<cstring> 18 #include<algorithm> 19 using namespace std; 20 21 int n,m; 22 int a[200005],b[200005]; 23 struct node{ 24 int id1,id2,val; 25 }zy[200005]; 26 27 bool cmp(node a,node b){ 28 return a.val>b.val; 29 } 30 31 int find(int z){ 32 if(z==a[z]){ 33 return z; 34 } 35 return a[z]=find(a[z]); 36 } 37 38 int main(){ 39 scanf("%d%d",&n,&m); 40 for(int i=1;i<=n;i++){ 41 a[i]=i; 42 } 43 for(int i=1;i<=m;i++){ 44 scanf("%d%d%d",&zy[i].id1,&zy[i].id2,&zy[i].val); 45 } 46 sort(zy+1,zy+1+m,cmp); 47 for(int i=1;i<=m;i++){ 48 if(find(a[zy[i].id1])==find(a[zy[i].id2])){ 49 printf("%d ",zy[i].val); 50 return 0; 51 } 52 if(!b[zy[i].id1]){ 53 b[zy[i].id1]=zy[i].id2; 54 } 55 else{ 56 a[find(zy[i].id2)]=find(b[zy[i].id1]); 57 } 58 if(!b[zy[i].id2]){ 59 b[zy[i].id2]=zy[i].id1; 60 } 61 else{ 62 a[find(zy[i].id1)]=find(b[zy[i].id2]); 63 } 64 } 65 printf("0 "); 66 return 0; 67 }