并查集最大秩
#include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<ctype.h> #include<algorithm> #include<vector> #include<string.h> #include<queue> #include<stack> #include<set> #include<map> #include<sstream> #include<time.h> #include<utility> #include<malloc.h> #include<stdexcept> using namespace std; int p[10000010]; int r[10000010]; int maxx ; int find(int x) { if (x==p[x]) return x; else return find ( p[x] ); } void un (int a,int b) { int fa = find (p[a]); int fb = find (p[b]); if(fa == fb ) return ; if (r[fa]>r[fb]) { p[fb]=fa; r[fa]+=r[fb]; if (maxx < r[fa] ) maxx = r[fa]; } else { p[fa]=fb; r[fb]+=r[fa]; if(maxx < r[fb]) maxx = r[fb]; } } int main () { int m; while (scanf ("%d",&m) != EOF ) { for (int i=1;i<=10000000;i++) { p[i]=i; r[i]=1; } int a,b; maxx = 1; for (int i=0;i<m;i++) { scanf ("%d%d",&a,&b); un(a,b); } printf("%d\n",maxx); } return 0; }