poj 2367 Genealogical tree

题目连接

http://poj.org/problem?id=2367

Genealogical tree

Description

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.

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

拓扑排序。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 110;
const int INF = 0x3f3f3f3f;
struct TopSort {
    struct edge { int to, next; }G[(N * N) << 1];
    int tot, inq[N], head[N], topNum[N];
    inline void init() {
        tot = 0, cls(inq, 0), cls(head, -1);
    }
    inline void add_edge(int u, int v) {
        G[tot].to = v; G[tot].next = head[u]; head[u] = tot++;
    }
    inline void built(int n) {
        int v;
        for(int i = 1; i <= n; i++) {
            while(~scanf("%d", &v), v) {
                inq[v]++, add_edge(i, v);
            }
        }
    }
    inline void bfs(int n) {
        int k = 0;
        queue<int> q;
        rep(i, n) {
            if(!inq[i + 1]) {
                q.push(i + 1);
            }
        }
        while(!q.empty()) {
            int u = q.front(); q.pop();
            topNum[k++] = u;
            for(int i = head[u]; ~i; i = G[i].next) {
                if(--inq[G[i].to] == 0) {
                    q.push(G[i].to);
                }
            }
        }
        rep(i, k) printf("%d%c", topNum[i], i < k - 1 ? ' ' : '
');
    }
    inline void solve(int n) {
        init(), built(n), bfs(n);
    }
}go;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int n;
    while(~scanf("%d", &n)) {
        go.solve(n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/GadyPu/p/4773329.html