http://acm.nyist.edu.cn/JudgeOnline/code.php?runid=2605874
#include <bits/stdc++.h>
using namespace std;
vector<int>mp[101];
int vis[101],low[101],dfn[101];
int cnt,sig,m;
void Tarjan(int s){
vis[s]=1;
low[s]=dfn[s]=cnt++;
// cout<<mp[s].size()<<endl;
for(int i=0;i<mp[s].size();i++){
int v=mp[s][i];
if(vis[v]==0) Tarjan(v);
if(vis[v]==1) low[s]=min(low[s],low[v]);
}
if(dfn[s]==low[s]) sig++;
}
void f(){
memset(vis,0,sizeof(vis));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
sig=0;
cnt=1;
for(int i=1;i<=m;i++){
if(!vis[i]){
Tarjan(i);
}
}
cout<<sig-1<<endl;
}
int main(){
int t;
cin>>t;
while(t--){
for(int i=1;i<=m;i++)
mp[i].clear();
cin>>m;
int x;
for(int i=1;i<=m;i++){
while(cin>>x){
if(!x) break;
mp[i].push_back(x);
}
}
f();
//for(int i=1;i<=m;i++) cout<<dfn[i]<<" "<<low[i]<<endl;
}
return 0;
}