题目大意:
给定有向图求任意一种拓扑排序即可(输入保证一定有解),只有一个测例,图由邻接表给出,元素个数为N(1 ≤ N ≤ 100),接下来第一行给出1号元素所连向的点,连向的点以0结束,接下来一行为2号点所连向的点,最后一行为N号连向的点,都是以0表示结束,输出给出任意一种拓扑排序即可。
代码:
/*
* Problem ID : POJ 2367 Genealogical tree
* Author : Lirx.t.Una
* Language : C++
* Run Time : 47 ms
* Run Memory : 156 KB
*/
#include <iostream>
#include <cstdio>
#include <queue>
#define MAXN 100
using namespace std;
bool g[MAXN + 1][MAXN + 1];
char deg[MAXN + 1];
queue<char> que;
queue<char> ans;
int
main() {
int n;
int i, j;
int u, v;
scanf("%d", &n);
for ( i = 1; i <= n; i++ )
while ( scanf("%d", &j), j ) {
g[i][j] = true;
deg[j]++;
}
for ( i = 1; i <= n; i++ )
if ( !deg[i] ) que.push(i);
for ( i = 1; i <= n; i++ ) {
ans.push(u = que.front());
que.pop();
for ( v = 1; v <= n; v++ )
if ( g[u][v] && !(--deg[v]) )
que.push(v);
}
while ( !ans.empty() ) {
printf("%d ", ans.front());
ans.pop();
}
return 0;
}
单词解释:
arbitrary:adj, 任意的
enumerate:vt, 列举,枚举
descendant:n, 后裔
grandson:n, 孙子,外孙
trivial:adj, 不重要的,琐碎的
assessor:n, 评审员
childless:adj, 无子女的
give the floor to:vt, 给予发言权
embarrassment:n, 窘迫,难堪
planetary:adj, 行星的
Martian:adj, 火星的; n, 火星人
bud:n, 萌芽,蓓蕾
genealogical:adj, 系谱的,家系的
tournament:n, 锦标赛,联赛
specification:n, 规格,说明书
be consistent with:vt, 与...一致
consistent:adj, 相一致的,相符合的
fix-point:n, 定点,不动点
reason:vt, 推论,辩论,推断
partially:adv, 部分地,偏袒地
upper bound:n, 上界,最大值
semantic:adj, 语义的,语义学的
constrain:vt, 束缚,约束
inconsistency:n, 不一致,矛盾