2016-03-18 Problem Solving►UVa UVa 10305 - Ordering Tasks Contents 1. Problem2. Solution3. Code Problem題目網址中文網址 找出做事情的順序,事情有一定的相依關係,如:在做 B 之前,必須先完成 A。p.s. 答案可能不是唯一解。 Solution關於 拓撲排序 直接邊做拓撲排序邊印出即可。 CodeUVa 10305UVa 10305 - Ordering Tasks1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#include<cstdio>#include<deque>#define N 101using namespace std;deque<int> DQ[N];void topo(int n);int main(){ int n, i, j; while (scanf("%d", &n) && n) { for (i = 1; i <= n; i++) DQ[i].clear(); int m, a, b; scanf("%d", &m); for (j = 0; j < m; j++) { scanf("%d%d", &a, &b); DQ[a].push_back(b); } topo(n); } return 0;}void topo(int n){ int ref[N] = {}, i; //計算每個點,有幾個指向他 for (i = 1; i <= n; i++) for (int j : DQ[i]) ref[j]++; deque<int> Q; //將沒有被指的放進 queue ,也就是可以先做的事 for (i = 1; i <= n; i++) { if (!ref[i]) Q.push_back(i); } bool first = true; while (!Q.empty()) { if (!first) putchar(' '); else first = false; int s = Q.front(); Q.pop_front(); ref[s] = -1; printf("%d", s); //將 s 所指的點都減 1 for (int j : DQ[s]) { ref[j]--; if (!ref[j]) Q.push_back(j); } } putchar('\n');} Newer UVa 10290 - {Sum+=i++} to Reach N Older UVa 10487 - Closest Sums