拓扑排序 POJ2367Genealogical tree[topo-sort]
---恢复内容开始---
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4875 | Accepted: 3236 | Special Judge |
Description
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
Output
Sample Input
5
0
4 5 1 0
1 0
5 3 0
3 0
Sample Output
2 4 5 3 1
Source
题意:给出孩子列表,求一个序列父亲在孩子前
裸的拓扑排序
//
// 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=;
int n,ch[N][N],t;
int topo[N],vis[N];
bool dfs(int u){
vis[u]=-;int cnt=ch[u][];
for(int i=;i<=cnt;i++){
int v=ch[u][i];
if(vis[v]==-) return false;
if(!vis[v]&&!dfs(v)) return false;
}
vis[u]=;topo[t--]=u;
return true;
}
bool toposort(){
t=n;
for(int i=;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=;i<=n;i++){
int a,cnt=;
while(true){
scanf("%d",&a);if(a==) break;
ch[i][++cnt]=a;
}
ch[i][]=cnt;
}
toposort();
for(int i=;i<=n;i++) printf("%d ",topo[i]);
return ;
}
拓扑排序 POJ2367Genealogical tree[topo-sort]的更多相关文章
- 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort
选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...
- 拓扑排序(topo sort)之 最大食物链计数( 洛谷P4017)
前言: 复习复习拓扑排序,自己把自己弄没了/kk 题目传送门 简化题意: 在一个DAG中,求从所有入度为0的点到所有出度为0的点路径的条数 md理解错题意把自己卡了半天,生物学的好的就可以直接理解为求 ...
- [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序
一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...
- timus 1022 Genealogical Tree(拓扑排序)
Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- 拓扑排序 Topological Sort
2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
随机推荐
- JavaScript 随机链接
<html> <body> <script type="text/javascript"> var r=Math.random() if (r& ...
- SharePoint解决The security validation for this page is invalid.
我是在一个service后台用object model去check in一个spfile的时候报的这个错.这是SharePoint的一种保护机制,在处理不能确定是安全的请求时,sharepoint就会 ...
- yii 的网址收藏
http://blog.csdn.net/yuhui_fish/article/details/7656929 YII框架多子域名同步登录问题 http://blog.csdn.net/yuhui_f ...
- PHP学习之登录以及后台商品展示
1.3用户登录 用户登录成功后跳转到商品显示页面 1.3.1设计界面 1.新建一个login.php页面,用来做用户的登录 2.登录业务原理 通过输入的用户名和密码查询对应的记录,表示登陆成功,否则登 ...
- GDataXMLNode应用
一.GDataXMLNode的安装配置过程 1.将GDataXMLNode文件加入至工程中 2.向Frameworks文件中添加libxml2.dylib库 3.在Croups & Files ...
- 修改 Android 5.x 系统默认音量大小
修改系统默认音量需要改两处地方: 1. frameworks\base\media\java\android\media\AudioManager.java /** @hide Default vol ...
- Android中各种Drawable总结
在Android中,Drawable使用广泛,但是种类也多,基于<Android开发艺术探索>中对Drawable的讲解,总结了如下表格.
- 【代码笔记】iOS-评分,支持我们
一,效果图. 二,工程图. 三,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additi ...
- php并发编程相关扩展
Stream:PHP内核提供的socket封装Sockets:对底层Socket API的封装Libevent:对libevent库的封装Event:基于Libevent更高级的封装,提供了面向对象接 ...
- 源码编译安装screen
OS:Amazon Linux AMI 2015.09.2 (HVM) #sudo su #wget http://ftp.gnu.org/gnu/screen/screen-4.3.1.tar.gz ...