UVA10305 Ordering Tasks
題目鏈接:https://cn.vjudge.net/problem/UVA-10305(忍不住uva連接滿)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.
Input
The input will consist of several instances of the problem. Each instance begins with a line containing two integers,?1 <= n <= 100?and?m.?n?is the number of tasks (numbered from?1?to?n) and?m?is the number of direct precedence relations between tasks. After this, there will be?m?lines with two integers?i?and?j, representing the fact that task?i?must be executed before task?j. An instance with?n = m = 0?will finish the input.
Output
For each instance, print a line with?n?integers representing the tasks in a possible order of execution.
Sample Input
5 4 1 2 2 3 1 3 1 5 0 0Sample Output
1 4 2 5 3?
***************************************************************************************
題意:給你n個(gè)點(diǎn)(從1開(kāi)始),還有要求一些點(diǎn)i必須在j前面。輸出任意一種排序。
題解:裸的拓?fù)渑判?#xff0c;直接套模板。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <sstream> 11 #include <algorithm> 12 using namespace std; 13 #define pb push_back 14 #define mp make_pair 15 #define ms(a, b) memset((a), (b), sizeof(a)) 16 //#define LOCAL 17 typedef long long LL; 18 const int inf = 0x3f3f3f3f; 19 const int maxn = 100+10; 20 const int mod = 1e9+7; 21 int gap[maxn][maxn]; 22 int topo[maxn], c[maxn], t; 23 int n, m; 24 bool dfs(int u){ 25 c[u] = -1; 26 for(int v = 1; v<=n; v++) if(gap[u][v]){ 27 if(c[v]< 0) return false; 28 else if(!c[v] && !dfs(v) ) return false; 29 } 30 c[u] = 1; 31 topo[--t] = u; 32 return true; 33 } 34 bool toposort(){ 35 t = n+1; 36 ms(c, 0); 37 for(int u = 1; u<=n ;u++) if(!c[u]) 38 if(!dfs(u)) return false; 39 return true; 40 } 41 int main() 42 { 43 #ifdef LOCAL 44 freopen("input.txt" , "r", stdin); 45 #endif // LOCAL 46 while(~scanf("%d%d", &n, &m)){ 47 if(n==0 && m==0) break; 48 ms(gap, 0); 49 for(int i = 0;i<m;i++){ 50 int a, b; 51 scanf("%d%d", &a, &b); 52 gap[a][b] = 1; 53 } 54 if(toposort()){ 55 for(int i =1 ; i<=n ;i++) 56 if(i==1) printf("%d", topo[i]); 57 else printf(" %d", topo[i]); 58 printf("\n"); 59 } 60 } 61 return 0; 62 } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/denghaiquan/p/6668417.html
總結(jié)
以上是生活随笔為你收集整理的UVA10305 Ordering Tasks的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux_《Linux命令行与shel
- 下一篇: php阿里云oss文件上传