Genealogical tree
Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format:
%lld Java class name:
Main
The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
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.
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.
Input
The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.
Output
The standard output should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.
Sample Input
5 0 4 5 1 0 1 0 5 3 0 3 0
Sample Output
2 4 5 3 1
题目很长。。大意大概就是 火星人根我们人类不一样 他们会不管血缘关系 胡乱交配。。。
input里 第一行代表 有N个火星人 后面的N行每一行都分别代表 第i个火星人的孩子
output是让我们按照火星人按我们人类的辈分大小输出一串辈分从大到小的序列
当然,输出的序列不唯一
思路:
把父母和孩子的关系用一个有向图表示,然后用topo排序,从起点(辈分最大)往终点(辈分最小)方向输出答案
关于topo排序,总结了四个需要注意的关键点:
1. 把所有点的入度用 in[size] 储存;
2.找出所有入度为0的点,作为答案储存在ans[size] 中
3.入度为0的点应该先作一个储存 可以储存在队列中(也可以是栈中)
4.一一输出队列中的点,并同时把新的入度为0的点 “push”到队列中
#include <cstdio>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
int in[1000]; //对应每个点的入度
int ans[1000]; //答案保存在这里
int n;
queue<int> q; //用来保存入度为0的点
vector<int> v[20]; //用来保存输入值
void toposort()
{
int k = 1;
for(int i =1; i <= n; i++ ){
if(in[i] == 0)
q.push(i);
}
while(!q.empty()){
int t = q.front();
int s = v[t].size();
ans[k++] = t;
q.pop();
for(int i = 0; i < s; i++ ){
int p = v[t][i];
in[p]--;
if(in[p] == 0)
q.push(p);
}
}
}
int main()
{
memset(in, 0, sizeof(in));
scanf("%d", &n);
for(int i = 1; i <= n; i++){
while(1){
int t;
scanf("%d", &t);
if(t == 0) break;
v[i].push_back(t);
in[t]++;
}
}
toposort();
for(int i = 1; i <= n; i++ )
printf("%d ", ans[i]);
return 0;
}