日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVA10305 Ordering Tasks

發布時間:2025/3/21 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 0

Sample Output

1 4 2 5 3

?

***************************************************************************************

題意:給你n個點(從1開始),還有要求一些點i必須在j前面。輸出任意一種排序。

題解:裸的拓撲排序,直接套模板。

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

?



轉載于:https://www.cnblogs.com/denghaiquan/p/6668417.html

總結

以上是生活随笔為你收集整理的UVA10305 Ordering Tasks的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。