比较直接的题目,入门一下。
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int to;
int v;
int next;
}edge[MAXN*];
int pre[MAXN],ind,vis[MAXN],n,in[MAXN],acancestor[MAXN],ans[MAXN],pa[MAXN];
int query[MAXN][MAXN];
void add(int x,int y)
{
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind++;
}
int find(int x)
{
if(x != pa[x])
pa[x] = find(pa[x]);
return pa[x];
}
void dfs(int rt)
{
vis[rt] = ;
acancestor[rt] = rt;
for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dfs(t);
int fx = find(rt);
int fy = find(t);
if(fx != fy){
pa[fy] =fx;
acancestor[fx] = rt;
}
}
}
for(int i=; i<=n; i++){
if(vis[i] && query[rt][i]){
ans[acancestor[find(i)]]+=query[rt][i];
query[rt][i] = query[i][rt] = ;
}
}
}
int main()
{
int i,j,root;
while(scanf("%d",&n)!=EOF)
{
memset(query,,sizeof(query));
for(i=; i<=n; i++){
pa[i] = i;
in[i] = ;
}
ind = ;
memset(pre,-,sizeof(pre));
for(i=; i<=n; i++){
int u,v,t;
scanf("%d:(%d)",&u,&t);
while(t--)
{
scanf("%d",&v);
add(u,v);
add(v,u);
in[v] ++;
}
}
int m;
scanf("%d",&m);
int x,y;
while(m--)
{
char c;
cin>>c;
scanf("%d %d)",&x,&y);
query[x][y]++;
query[y][x]++;
}
memset(acancestor,,sizeof(acancestor));
memset(vis,,sizeof(vis));
memset(ans,,sizeof(ans));
for(i=; i<=n; i++){
if(in[i] == ){
root = i;
break;
}
}
dfs(root);
for(i=; i<=n; i++){
if(ans[i] > ){
printf("%d:%d\n",i,ans[i]);
}
}
}
return ;
}