题意:给定n对边 判断最大通路的长度
并查集连一次,再询问一次那些根节点已经改变但是子节点尚未改变的点,把它们的根结点变为根结点的根结点,最后统计最大数即可。
注意题意中的
After Mr Wang’s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left.
因此n=0时特判考虑为1;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int N=10000005;
int pre[N];
int vis[N];
int find(int i){
return pre[i]==i?i:pre[i]=find(pre[i]);
}
bool join(int a,int b){
int fa=find(a);
int fb=find(b);
if(fa!=fb){
pre[fb]=fa;
return 1;
}
return 0;
}
int main(){
int n;
while(~scanf("%d",&n)){
for(int i=1;i<N;i++){
pre[i]=i;
vis[i]=0;
}
int ans=1;
int maxn=0;
for(int i=1;i<=n;i++){
int x,y;
scanf("%d %d",&x,&y);
maxn=max(maxn,x);
maxn=max(maxn,y);
join(x,y);
}
for(int i=1;i<=maxn;i++){
int fi=find(i);
if(pre[fi]!=fi){
pre[i]=pre[fi];
}
}
for(int i=1;i<=maxn;i++){
vis[pre[i]]++;
ans=max(ans,vis[pre[i]]);
}
printf("%d\n",ans);
}
}
/*
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
*/