对一个有向无环图 (Directed Acyclic Graph 简称 DAG) G 进行拓扑排序,是将 G 中所有顶点排成一个线性序列,使得图中任意一对顶点 u 和 v,若边 <u,v> ∈ E(G),则 u 在线性序列中出现在 v 之前。通常,这样的线性序列称为满足拓扑次序 (Topological Order) 的序列,简称拓扑序列。简单的说,由某个集合上的一个偏序得到该集合上的一个全序,这个操作称之为拓扑排序。
int n, m, cnt; //n 个数, m 个关系, cnt: 存储ans数组的下标 bool mp[MAXN][MAXN]; int indeg[MAXN], ans[MAXN]; //indeg[i]: 第i个点的入度; ans[]: 答案队列 queue <int> q;
voidGetmap(void) { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); mp[x][y] = 1; indeg[y]++; //建图, 同时统计入度 } }
voidtopo_sort(void) { for (int i = 1; i <= n; i++) if (!indeg[i]) q.push(i); //将所有入度为 0 的点入队 while (!q.empty()) //开始搜索 { int u = q.front(); ans[++cnt] = u;//入度为0的点记得放到答案队列里, 判断环的核心, 看答案数组是否有 n 个 q.pop(); for (int i = 1; i <= n; i++) if (mp[u][i]) //删边的操作转化为入度减1 { indeg[i]--; if (!indeg[i]) //如果这个点变成入度为 0, 入队列 q.push(i); } } }
intmain() { Getmap(); topo_sort();
if (cnt < n)//判断是否有环, 答案队列不足即有环 { printf("Circle\n"); return0; } for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
You are given 5 different sizes of kitchen plates. Each plate is marked with a letter A, B, C, D, or E. You are given 5 statements comparing two different plates, you need to rearrange the plates from smallest size to biggest size. For example: the sizes of these plates.
Input
The input consist of 5 lines. In each line there will be 3 characters, the first and last character will be either A, B, C, D, or E and the middle character will be either > or < describing the comparison between two plates sizes. No two plates will be equal.
Output
The output consist of 55 characters, the sorted order of balls from smallest to biggest plate. Otherwise, if the statements are contradicting print impossibleimpossible. If there are multiple answers, print any of them.