rqnoj343 mty的考验

5 3
1 2
2 3
3 4

4

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1050;
int n,m;
int f[maxn],sz[maxn];
int findf(int x){
return x == f[x] ? x : f[x] = findf(f[x]);
}
int main(){
cin>>n>>m;
for(int i = 1;i <= n;i++){
f[i] = i;
sz[i] = 1;
}
int x,y,fx,fy;
for(int i = 1;i <= m;i++){
scanf("%d%d",&x,&y);
fx = findf(x);
fy = findf(y);
if(fx == fy) continue; //notice
if(sz[fx] > sz[fy]) swap(fx,fy);
sz[fy] += sz[fx];
f[fx] = fy;
}
int ans = 0;
for(int i = 1;i <= n;i++){
if(ans < sz[i]) ans = sz[i];
}
cout<<ans;
return 0;
}

#include<iostream>
using namespace std;
int father[1001];
int find(int x)
{
if(x!=father[x])father[x]=find(father[x]);
return father[x];
}
void un(int f,int z)
{
father[z]=f;
}
int main()
{
int a[1001]={0};
int ans=0;
int n,m;
int x,y;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
father[i]=i;
}
for(int i=1;i<=m;i++)
{
cin>>x>>y;
un(find(x),find(y));
}
for(int i=1;i<=n;i++)
{
a[find(i)]++;
ans=max(a[find(i)],ans);
}
cout<<ans;
//system("pause");
return 0;
}

posted @ 2016-09-17 10:19  ACforever  阅读(166)  评论(0编辑  收藏  举报