bzoj3296: [USACO2011 Open] Learning Languages
shui
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int fa[11000]; int findfa(int x) { if(fa[x]==x)return x; fa[x]=findfa(fa[x]);return fa[x]; } void unit(int x,int y) { int fx=findfa(x),fy=findfa(y); if(fx!=fy)fa[fx]=fy; } int last[31000]; bool v[11000]; int main() { int n,m; scanf("%d%d",&n,&m); memset(last,0,sizeof(last)); for(int i=1;i<=n;i++)fa[i]=i; for(int i=1;i<=n;i++) { int T,c; scanf("%d",&T); for(int j=1;j<=T;j++) { scanf("%d",&c); if(last[c]!=0)unit(i,last[c]); last[c]=i; } } int ans=0; memset(v,false,sizeof(v)); for(int i=1;i<=n;i++) { int FA=findfa(i); if(v[FA]==false) v[FA]=true, ans++; } printf("%d\n",ans-1); return 0; }
pain and happy in the cruel world.