poj 1470 Closest Common Ancestors

#include <iostream>                //并查集
using namespace std;
int main()
{
int n,i,j;
int parent[910],closest[910],vis_anc[910];
while(scanf("%d",&n)!=EOF)
{
int num,father,child;
memset(parent,
0,sizeof(parent[0])*(n+1));
for(i=1;i<=n;i++)
{
scanf(
"%d:(%d)",&father,&num);
while(num--)
{
scanf(
"%d",&child);
parent[child]
=father;
}
}
int test,a,b;
memset(closest,
0,sizeof(closest[0])*(n+1));
scanf(
"%d",&test);
while(test--)
{
scanf(
" (%d%d)",&a,&b);
memset(vis_anc,
0,sizeof(vis_anc[0])*(n+1));

while(parent[a]!=0)
{
vis_anc[a]
=1;
a
=parent[a];
}
vis_anc[a]
=1;

while(1)
{
if(vis_anc[b]==1)
break;
b
=parent[b];
}
closest[b]
++;
}
for(i=1;i<=n;i++)
if(closest[i]==1)
printf(
"%d:%d\n",i,closest[i]);
}
return 0;
}

  

posted on 2011-07-18 11:22  sysu_mjc  阅读(154)  评论(0编辑  收藏  举报

导航