大致题意:虽然题目很复杂,说的很绕,但是题意很简单就是说的一个拓扑排序,父节点,子节点什么的。
思路就是最简单的一个拓扑排序,而且不需要判断是否有回路。
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there's nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.
Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.
5 0 4 5 1 0 1 0 5 3 0 3 0
2 4 5 3 1
#include<stdio.h>
#include<string.h>
int e[520][520],dis[520],in[520],book[520];
int n,m;
void tosort()
{
int i,j,k;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(in[j]==0)
{
in[j]=-1; //删除此节点
k=j;
break;
}
}
if(i!=1)
printf(" %d",k);
else
printf("%d",k);
for(j=1; j<=n; j++)
{
if(e[k][j]) //与其相关联的点入度减1
in[j]--;
}
}
printf("\n");
}
int main()
{
int i,j,x,y;
while(~scanf("%d",&n))
{
memset(e,0,sizeof(e));
memset(dis,0,sizeof(dis));
memset(in,0,sizeof(in));
for(i=1; i<=n; i++)
{
while(~scanf("%d",&x),x)
{
e[i][x]=1; //出现的边权值赋为1
in[x]++;
}
}
tosort();
}
return 0;
}