poj 2367 拓扑排序入门
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
直接套模板
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1e5 + ; int n, du[maxn], head[maxn], tot;
struct node {
int v, next;
} edge[maxn];
queue<int>q;
void add(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void init() {
tot = ;
memset(du, , sizeof(du));
memset(head, -, sizeof(head));
}
void solve() {
while(!q.empty()) {
int u = q.front();
q.pop();
printf("%d ", u);
for (int i = head[u] ; i != - ; i = edge[i].next) {
du[edge[i].v]--;
if (!du[edge[i].v]) q.push(edge[i].v);
}
}
}
int main() {
scanf("%d", &n);
init();
for (int i = ; i <= n ; i++) {
int x;
while(scanf("%d", &x), x != ) {
add(i, x);
du[x]++;
}
}
for (int i = ; i <= n ; i++)
if (!du[i]) q.push(i);
solve();
return ;
}
poj 2367 拓扑排序入门的更多相关文章
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- poj 3249 拓扑排序 and 动态规划
思路:我们首先来一遍拓扑排序,将点按先后顺序排列于一维数组中,然后扫描一遍数组,将每个点的出边所连接的点进行更新,即可得到最优解. #include<iostream> #include& ...
- poj 2585 拓扑排序
这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...
- Sorting It All Out POJ - 1094 拓扑排序
题意:给N个字母,和M个偏序关系 求一个可确定的全序,可确定是指没有其他的可能例如A>B D>B 那么有ADB DAB两种,这就是不可确定的其中,M个偏序关系可以看做是一个一个按时间给出的 ...
- HDU 1285 经典拓扑排序入门题
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- 拓扑排序入门详解&&Educational Codeforces Round 72 (Rated for Div. 2)-----D
https://codeforces.com/contest/1217 D:给定一个有向图,给图染色,使图中的环不只由一种颜色构成,输出每一条边的颜色 不成环的边全部用1染色 ps:最后输出需要注意, ...
- POJ 2367 Genealogical tree 拓扑排序入门题
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8003 Accepted: 5184 ...
随机推荐
- Linux nohup 关闭终端的时候,程序依然能在后台运行( linux重定向及nohup不输出的方法)
先说一下linux重定向: 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出.在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标号 ...
- input属性总结
<input type="text" readonly="readonly" /> 这个是不能输入的 readonly="readonly ...
- VMware中Ubuntu开机时停在启动界面,不进入X-window的解决办法
启动Ubuntu虚拟机时,停在这个画面不动: 试了若干次,都是这样.尝试了新建一个虚拟机然后把.vmdk文件拷过去启动,无法解决. 尝试重启,在这个界面按esc进入grub: 选择恢复模式 recov ...
- webpack4 单独抽离打包 css 的新实现
webpack4 单独抽离打包 css 的新实现 前言 之前我们使用的打包 css 无非两种方式:① 将 css 代码打包进 入口 js 文件中:② 使用第三方插件(extract-text-webp ...
- yii2深入理解之内核解析
一.前言 首先,yii2最为为数不多的PHP主流开源框架,受欢迎程度不亚于laravel和TP.个人认为,研究这些框架底层代码是非常有助于自身代码编程思想的提升和代码简化程度和质量的提升的. 那么,话 ...
- C语言进阶——goto 和 void 的分析08
遭人遗弃的goto: 高手潜规则:禁止使用goto 项目经验:程序质量与goto的出现次数成反比 最后的判决:将goto打入冷宫 程序示例1:(goto副作用分析) #include <stdi ...
- Android 第三方库RxLifecycle使用
1.简单介绍RxLifecycle 1.1.使用原因. 在使用rxjava的时候,如果没有及时解除订阅,在退出activity的时候,异步线程还在执行. 对activity还存在引用,此时就会产生内存 ...
- BZOJ 3027: [Ceoi2004]Sweet
容斥 #include<cstdio> using namespace std; int a,b,n,m[15]; long long ans=0,mod=2004; long long ...
- nodejs基础1
nodejs学习网站: https://github.com/alsotang/node-lessons 1.全局对象 (1)node中没有window对象,有global对象替代window对象 g ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目2
2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...