多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 圖論:拓撲排序

圖論:拓撲排序

來源:程序員人生   發布時間:2015-01-17 09:49:28 閱讀次數:4558次

10305 - Ordering Tasks

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 andmn 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 = 0will 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
裸的,輸出順序任意,所以DFS做法可以求得。

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=110; int mp[maxn][maxn]; int toposort[maxn]; int vis[maxn]; int n,m,cnt; void dfs(int x) { vis[x]=1; for(int i=1;i<=n;i++) if(!vis[i]&&mp[x][i]&&i!=x) dfs(i); toposort[cnt++]=x; } void solve() { CLEAR(vis,0); for(int i=1;i<=n;i++) if(!vis[i]) dfs(i); } int main() { int x,y; std::ios::sync_with_stdio(false); while(cin>>n>>m&&(n+m)) { CLEAR(mp,0); while(m--) { cin>>x>>y; mp[x][y]=1; } cnt=0; solve(); for(int i=cnt⑴;i>=0;i--) printf(i==0?"%d ":"%d ",toposort[i]); } return 0; }
HDU 1285

肯定比賽名次

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13115    Accepted Submission(s): 5264


Problem Description
有N個比賽隊(1<=N<=500),編號順次為1,2,3,。。。。,N進行比賽,比賽結束后,裁判委員會要將所有參賽隊伍從前往后順次排名,但現在裁判委員會不能直接取得每一個隊的比賽成績,只知道每場比賽的結果,即P1贏P2,用P1,P2表示,排名時P1在P2之前。現在請你編程序肯定排名。
 

Input
輸入有若干組,每組中的第1行動2個數N(1<=N<=500),M;其中N表示隊伍的個數,M表示接著有M行的輸入數據。接下來的M行數據中,每行也有兩個整數P1,P2表示即P1隊贏了P2隊。
 

Output
給出1個符合要求的排名。輸出時隊伍號之間有空格,最后1名后面沒有空格。

其他說明:符合條件的排名可能不是唯1的,此時要求輸出時編號小的隊伍在前;輸入數據保證是正確的,即輸入數據確保1定能有1個符合要求的排名。
 

Sample Input
4 3 1 2 2 3 4 3
 

Sample Output
1 2 4 3

字典序最小的。。

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=550; int mp[maxn][maxn],vis[maxn]; int in[maxn],que[maxn]; int n,m,k; void toposort() { k=0;int i; while(k<n) { for(i=1;i<=n;i++) { if(!vis[i]&&!in[i]) { que[++k]=i; vis[i]=1; break; } } for(int j=1;j<=n;j++) { if(mp[i][j]) --in[j]; } } } void output() { for(int i=1;i<=k;i++) printf(i==k?"%d ":"%d ",que[i]); } int main() { int x,y; std::ios::sync_with_stdio(false); while(~scanf("%d%d",&n,&m)) { CLEAR(in,0); CLEAR(mp,0); CLEAR(vis,0); while(m--) { scanf("%d%d",&x,&y); if(!mp[x][y]) { mp[x][y]=1; in[y]++; } } toposort(); output(); } return 0; }

HDU 3342 

Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4619    Accepted Submission(s): 2106


Problem Description
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?

We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. 

Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
 

Input
The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N⑴). We use their numbers instead of their names.
 

Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".
 

Sample Input
3 2 0 1 1 2 2 2 0 1 1 0 0 0
 

Sample Output
YES NO

判斷能不能拓撲排序。

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=110; int mp[maxn][maxn],vis[maxn]; int in[maxn]; int n,m,k; int toposort() { k=0;int i; for(int t=0;t<n;t++) { for(i=0;i<n;i++) { if(!vis[i]&&!in[i]) { vis[i]=1; in[i]--; k++; for(int j=0;j<n;j++) if(mp[i][j]) in[j]--; break; } } } // cout<<"2333 "<<k<<endl; return k==n?1:0; } int main() { int x,y; std::ios::sync_with_stdio(false); while(cin>>n>>m&&(n+m)) { CLEAR(mp,0); CLEAR(vis,0); CLEAR(in,0); while(m--) { cin>>x>>y; if(!mp[x][y]) { mp[x][y]=1; in[y]++; } } if(toposort()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日本免费一级片 | freemovies性老女人| 欧美精品一区二区三区视频 | 国产欧美日韩在线一区二区不卡 | 波多野结衣一区免费作品 | 国产 日韩 欧美 亚洲 | 一区二区三区四区无限乱码在线观看 | 国产精品60岁老女人 | 国产乱人乱精一区二区视频密 | 在线观看 a国v | 欧美一区二区三区视频 | 亚洲欧美国产视频 | 成人免费精品视频 | 欧美又粗又硬又黄又爽视频 | 久久伊人免费视频 | 欧美最猛性xxxxx图片 | 国产亚洲欧美久久久久 | 国产成人精品三区 | 欧美国产成人精品一区二区三区 | 欧美日韩亚洲一区二区精品 | 欧美成人全部免费观看1314色 | 国产精品毛片在线更新 | 国产成人久久精品麻豆二区 | 国产精品久久久免费视频 | 狂野欧美性猛交xxxx乱大交 | 日本欧美一区二区三区免费不卡 | 亚洲欧美色中文字幕 | 日韩精品一区二区三区毛片 | 国产精品高清全国免费观看 | 国产免费资源高清小视频在线观看 | 日韩福利网站 | 久久久久亚洲国产 | 亚洲人成777在线播放 | 欧美三级免费观看 | 夜夜精品视频一区二区 | 亚洲成人在线免费视频 | 免费淫片 | 美女网站免费看 | 亚洲成人自拍网 | 在线资源你懂的 | 最近的中文字幕免费视频1 最近的中文字幕免费完整 最近的中文字幕视频大全高清 |