zzz
作者:
菜就多练纟
,
2022-03-13 15:54:54
,
所有人可见
,
阅读 180
p2835 并查集 + floyd
/*************************************************************************
> File Name: p2835.cpp
> Author: Ansary
> Created Time: 2022/3/13 11:06:12
************************************************************************/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int pre[300];
int vis[300][300];
int sum = 0;
int find(int x){
if(pre[x] == x)
return x;
return pre[x] = find(pre[x]);
}
void merge(int x, int y){
int fx = find(x);
if(fx == y)
return;
pre[y] = fx;
}
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++)
pre[i] = i;
for(int i = 1; i <= n; i++){
int t;
cin >> t;
while(t != 0){
vis[i][t] = 1;
cin >> t;
}
}
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
vis[i][j] = vis[i][j] || (vis[i][k] && vis[k][j]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(vis[i][j])
merge(i, j);
for(int i = 1; i <= n; i++)
if(pre[i] == i)
sum++;
cout << sum << endl;
}