Consider the arms (a, b) as an undirected edges, then for a communication block
1. When there is no ring, means one tree, then optionally a point as a root, each edge node can only select son, obviously root to choose the largest number
2. when there is a ring, the ring optionally as a root, a side of a ring to rest on the ring so as to become removed a tree, the tree edge selection sons rims a certain order can be selected
then the subject is equivalent to the minimum required to maintain a maximum number of the tree for each block communicating with disjoint-set to maintain
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,x,y,ans,f[1000005],vis[1000005]; 4 int find(int k){ 5 if (k==f[k])return k; 6 return f[k]=find(f[k]); 7 } 8 int main(){ 9 scanf("%d",&n); 10 for(int i=1;i<=n+1;i++)f[i]=i; 11 for(int i=1;i<=n;i++){ 12 scanf("%d%d",&x,&y); 13 if (find(x)==find(y))vis[find(x)]=1; 14 else{ 15 if (find(x)>find(y))swap(x,y); 16 vis[find(y)]|=vis[find(x)]; 17 vis[find(x)]=1; 18 f[find(x)]=find(y); 19 } 20 } 21 ans=1 ; 22 while (screw [years]) years ++ ; 23 printf ( " % d " , ans- 1 ); 24 }