#include<cstdio> #include<cstring> #include<algorithm> const int MAXN=200+5; int want[MAXN][MAXN];int fa[MAXN],a,group,n; int find(int x){return x==fa[x]?x:find(fa[x]);} void join(int x,int y){int fx=find(x),fy=find(y);if(fx!=fy) fa[fx]=fy;} int main() { scanf("%d",&n); memset(want,0x3f3f3f,sizeof(want)); for(int i=1;i<=n;i++) { fa[i]=i; while(1) { scanf("%d",&a); if(a) want[i][a]=1; else break; } } for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) want[i][j]=std::min(want[i][j],want[i][k]+want[k][j]); { for(int j=1;j<=n;j++) printf("%d\t",want[i][j]); printf("\n"); }*/ for(int i=1;i<=n;i++) want[0][i]=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(want[i][j]!=want[0][0] && want[j][i]!=want[0][0]) join(i,j); for(int i=1;i<=n;i++) { a=find(i); if(!want[0][a]) {group++;want[0][a]=1;} } printf("%d",group); return 0; }
|