其他分享
首页 > 其他分享> > 连通块中点的数量

连通块中点的数量

作者:互联网

#include <iostream>
using namespace std;
const int N=10010;
int n,m;
int p[N],size[N];
int find(int x)
{
if(x!=p[x]) p[x]=find(p[x]);
return p[x];
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
p[i]=i;
size[i]=1;
}
while(m--)
{
string op;
int a,b;
cin>>op;
if(op=="C")
{
cin>>a>>b;
if(find(a)==find(b)) continue;
size[find(b)]+=size[find(a)];
p[find(a)]=find(b);
}
else if(op=="Q1")
{
cin>>a>>b;
if(find(a)==find(b)) puts("Yes");
else puts("No");
}
else
{
cin>>a;
cout<<size[find(a)];
}
}
}

标签:连通,int,cin,find,else,op,中点,数量,size
来源: https://www.cnblogs.com/xiao--yang/p/16062383.html