#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int ls[2000000],_hash[2000000], d[2000000];
int find1(int x)
{
return ls[x] == x ? x : ls[x]=(find1(ls[x]));
}
void add(int x, int y)
{
int xx = find1(x);
int yy = find1(y);
if(xx != yy)
{
ls[yy] = xx;
}
}
int main()
{
int n, m, i;
int ti = 1;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
int cnt = n;
for(i = 0; i < 2000000; i++)
{
ls[i] = i;
d[i] = i;
}
while(m--)
{
char c;
getchar();
scanf("%c",&c);
if(c == 'M')
{
int u, v;
scanf("%d%d",&u,&v);
add(d[u],d[v]);
}
else
{
int u;
scanf("%d",&u);
d[u]=cnt++;
}
}
memset(_hash,0,sizeof(_hash));
int cont = 0;
for(i = 0; i < n; i++)
{
int a = find1(d[i]);
if(!_hash[a])
{
_hash[a] = 1;
cont++;
}
}
printf("Case #%d: %d\n",ti++,cont);
}
return 0;
}
HDU 2473-Junk-Mail Filter-并查集删除
最新推荐文章于 2021-10-09 19:49:02 发布
本文深入探讨了C++中模板和继承的综合使用技巧,通过具体实例展示了如何利用这些特性来提高代码的复用性和灵活性,强调了在实际项目中正确运用模板和继承的重要性。

698

被折叠的 条评论
为什么被折叠?



