支持删除的并查集 hdu2473

题解:

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
int fa[maxn],id,vis[maxn];
int find(int x)
{
if (x!=fa[x]) fa[x]=find(fa[x]); return(fa[x]);
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx!=fy) fa[fx]=fy;
}
void del(int x)
{
fa[x]=++id;
}
int main()
{
freopen("noip.in","r",stdin);
freopen("noip.out","w",stdout);
int n,m,Case=;
while (scanf("%d%d",&n,&m),n+m)
{
for (int i=;i<=n;i++) fa[i]=i+n;
for (int i=n+;i<=n+n+m;i++) fa[i]=i;
id=n+n;
int a,b;char ch[];
for (int i=;i<=m;i++)
{
cin>>ch;
if (ch[]=='M')
{
cin>>a>>b;
merge(a,b);
}
else
{
cin>>a;
del(a);
}
}
int ans=;
memset(vis,,sizeof(vis));
for (int i=;i<=n;i++)
{
int x=find(i);
if (!vis[x]) ans++,vis[x]=;
}
printf("Case #%d: %d\n",++Case,ans);
}
return ;
}
上一篇:Go语言的类型转换和类型断言


下一篇:3_PHP表达式_5_数据类型转换_类型强制转换