一个裸的LCA,唯一的坑点就在于输入(╯‵□′)╯︵┻━┻,不得不GG
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1000;
struct tree{
int to,next;
}Tree[maxn*maxn];
struct query{
int to,next;
}Query[maxn*maxn];
int N,M,ans[maxn],Flag[maxn];
int fat[maxn],vist[maxn],head_tree[maxn],head_query[maxn];
int cnt_query,cnt_tree;
void Init(){
for(int i = 1; i <= N; i++){
head_tree[i] = head_query[i] = -1;
fat[i] = i;
vist[i] = ans[i] = Flag[i] = 0;
}
}
void ADD_tree(int u,int v){
Tree[cnt_tree].to = v;
Tree[cnt_tree].next = head_tree[u];
head_tree[u] = cnt_tree++;
}
void ADD_query(int u,int v){
Query[cnt_query].to = v;
Query[cnt_query].next = head_query[u];
head_query[u] = cnt_query++;
}
int Find(int x){
if(x != fat[x])
x = Find(fat[x]);
return x;
}
void Tarjan(int u,int father){
for(int i = head_tree[u]; ~i; i = Tree[i].next){
int v = Tree[i].to;
if(!vist[v] && v!=father){
Tarjan(v,u);
fat[v] = u;
}
}
vist[u] = 1;
for(int i = head_query[u]; ~i; i = Query[i].next){
int v = Query[i].to;
if(vist[v]){
int temp = Find(v);
ans[temp]++;
}
}
}
int main(){
while(~scanf("%d",&N)){
Init();
cnt_tree = cnt_query = 0;
int u,num,v;
for(int i = 1; i <= N; i++){
scanf("%d:(%d)",&u,&num);
for(int j = 0; j < num; j++){
scanf("%d",&v);
Flag[v] = 1;
ADD_tree(u,v);
ADD_tree(v,u);
}
}
scanf("%d",&M);
for(int i = 0; i < M; i++){
scanf(" (%d %d)",&u,&v);
ADD_query(u,v);
ADD_query(v,u);
}
int pos;
for(int i = 1; i <= N; i++)
if(!Flag[i]){
pos = i;
break;
}
Tarjan(pos,-1);
for(int i = 1; i <= N; i++){
if(ans[i])
printf("%d:%d\n",i,ans[i]);
}
}
return 0;
}