拓扑排序 POJ2367Genealogical tree[topo-sort]

---恢复内容开始---

Genealogical tree

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4875   Accepted: 3236   Special Judge

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

Source

Ural State University Internal Contest October‘2000 Junior Session


题意:给出孩子列表,求一个序列父亲在孩子前


裸的拓扑排序

vis-1 正在访问

只有DAG才有

//
//  main.cpp
//  poj2367
//
//  Created by Candy on 9/10/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=105;
int n,ch[N][N],t;
int topo[N],vis[N];
bool dfs(int u){
    vis[u]=-1;int cnt=ch[u][0];
    for(int i=1;i<=cnt;i++){
        int v=ch[u][i];
        if(vis[v]==-1) return false;
        if(!vis[v]&&!dfs(v)) return false;
    }
    vis[u]=1;topo[t--]=u;
    return true;
}
bool toposort(){
    t=n;
    for(int i=1;i<=n;i++) if(!vis[i])
        if(!dfs(i)) return false;
    return true;
}
int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,cnt=0;
        while(true){
            scanf("%d",&a);if(a==0) break;
            ch[i][++cnt]=a;
        }
        ch[i][0]=cnt;
    }
    toposort();
    for(int i=1;i<=n;i++) printf("%d ",topo[i]);
    return 0;
}
时间: 2024-08-29 07:22:39

拓扑排序 POJ2367Genealogical tree[topo-sort]的相关文章

算法总结之拓扑排序

拓扑排序 1.一般应用       拓扑排序常用来确定一个依赖关系集中,事物发生的顺序.例如,在日常工作中,可能会将项目拆分成A.B.C.D四个子部分来完成,但A依赖于B和D,C依赖于D.为了计算这个项目进行的顺序,可对这个关系集进行拓扑排序,得出一个线性的序列,则排在前面的任务就是需要先完成的任务. 2.实现的基本方法 (1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它. (2)从网中删去该顶点,并且删去从该顶点发出的全部有向边. (3)重复上述两步,直到剩余的网中不再存在没有前趋

[2016-02-18][拓扑排序]

[2016-02-18][拓扑排序] 拓扑排序 思路    dfs 分析 从树的角度来看,就是不同层的节点,顺序是一定的,同层的节点,顺序是不定的, 但是拓扑排序的图不一定是树,可以有环(不能是同向的环,即1->2->3->1 这种) 为了避免漏了节点,必须枚举每一个点跑一次dfs 为了避免出现通向环的情况,必须加上标志,在每次dfs时候,标记非0值表示访问过,但是进入下一层dfs时,标记为-1,如果下层dfs访问到这个节点时的标记为-1,说明有环,出来dfs才设置为1,表示这个点后面没

图论之拓扑排序 poj 2367 Genealogical tree

题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn=200; int ans; int n; int in[maxn]; //记录入度

hiho一下&#183;47 拓扑排序&#183;一(判断是否能够Topo排序)

题意  中文 简单的Topo排序  用vector实现邻接表比较方便 #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; vector<int> e[N]; vector<int>::iterator it; int n, m, ideg[N]; bool topo() { int cur; queue<int> q; for(int i = 1; i <= n; +

POJ 2367:Genealogical tree(拓扑排序)

Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They ga

POJ 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 surpris

timus 1022 Genealogical Tree(拓扑排序)

Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background 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

Toplogical Sort 拓扑排序

Toplogical Sort 拓扑排序是对有向图的顶点的一种排序,它使得如果存在一条从Vm到Vn的路径,那么在排序中Vn出现在Vm后面. 如果图含有圈,或者初始入度没有为0的节点,那么拓扑排序是不可能完成的. 理论介绍去看<DSAA>或者<算法导论>,老话,这里还是介绍如何实现. tls.h /************************************************************ code file : tls.h code writer : E

拓扑排序(Topological Sort)

Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap.c.heap.h 等等,且这些文件没有其他自定义库的依赖:另外还有一些基于上述自定义库的库:bfs.c.bfs.h.dfs.c.dfs.h.dijkstra.c.dijkstra.h.tcpSocket.c.tcpSocket.h 等等:基于以上的库,你开发了一些爬虫程序 scrawlYoutub