[POJ 2367] Genealogical tree【拓扑序】
Problem:
Time Limit: 1000MS | Memory Limit: 65536K | 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
Solution:
本题除了题目描述有些珂怕之外,其他一切正常,像个拓扑排序模板题的样子。
据说拓扑排序有两种实现方法,分别是较好理解的 Kahn 算法(Code #1)和实现较简单的 DFS 法(Code #2)。
个人推荐前者。
Wikipedia: https://en.wikipedia.org/wiki/Topological_sorting
Code #1: O(N2) [184K, 0MS]
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<algorithm> using namespace std; int N; struct Edge{ int np; Edge *nxt; }; struct Graph{ Edge *V[102], E[10002]; int tope; inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];} } G; int indeg[102]; int deg0[102], topd = 0; int topo[102], topt = 0; int main(){ scanf("%d", &N); memset(indeg, 0, sizeof(indeg)); for(register int i = 1; i <= N; i++){ int v; while(scanf("%d", &v) != EOF && v){ G.addedge(i, v); indeg[v]++; } } for(register int i = 1; i <= N; i++) if(!indeg[i]) deg0[++topd] = i; // Push 0-indegree points into the stack of nodes to be removed while(topd){ topo[++topt] = deg0[topd--]; // Remove a point from the graph for(register Edge *ne = G.V[topo[topt]]; ne; ne = ne->nxt) if(!--indeg[ne->np]) deg0[++topd] = ne->np; // Update the indegrees, and the stack } assert(topt == N); for(register int i = 1; i < topt; i++) printf("%d ", topo[i]); printf("%d\n", topo[topt]); return 0; }
Code #2: O(N2) [184K, 16MS]
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<algorithm> using namespace std; int N; struct Edge{ int np; Edge *nxt; }; struct Graph{ Edge *V[102], E[10002]; int tope; inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];} } G; int vis[102]; // -1: visited, 0: not visited, 1: visiting int topostack[102], topt = 0; inline void toposort_DFS(int u){ vis[u] = 1; for(register Edge *ne = G.V[u]; ne; ne = ne->nxt){ assert(vis[ne->np] != 1); // It must be true unless there're hoops if(!vis[ne->np]) toposort_DFS(ne->np); } vis[u] = -1; topostack[++topt] = u; // The answer is reversed } int main(){ scanf("%d", &N); for(register int i = 1; i <= N; i++){ int v; while(scanf("%d", &v) != EOF && v) G.addedge(i, v); } for(register int i = 1; i <= N; i++) if(!vis[i]) toposort_DFS(i); while(topt > 1) printf("%d ", topostack[topt--]); printf("%d\n", topostack[topt--]); return 0; }
发表评论