#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-并查集删除
最新推荐文章于 2015-10-11 11:31:08 发布