拓扑排序例题 hdu1285 hdu3342

804人阅读 评论(0)

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

Problem Description

Input

Output

Sample Input
4 3
1 2
2 3
4 3

Sample Output
1 2 4 3
【解析】：

【代码】：
[cpp] view plain
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<string.h>
4. #include<iostream>
5. #include<algorithm>
6. #include<queue>
7. using namespace std;
8. struct NODE{
9.     int from,to;
10.     int next;
11.     NODE(int f=0,int t=0,int ne=0){
12.         from=f;to=t;next=ne;
13.     }
14. }edge[120000];
16. int in[520];//入度
17. int main()
18. {
19.     int n,m;
20.     while(cin>>n>>m)
21.     {
23.         memset(in,0,sizeof(in));
24.         for(int i=0;i<m;i++)
25.         {
26.             int f,t;
27.             cin>>f>>t;
28.             in[t]++;
31.         }
32.         int queue[520],tail=0;
33.         for(int i=1;i<=n;i++)//入度为0的点入队
34.             if(!in[i])
35.                 queue[tail++]=i;
36.         for(int i=0;i<tail;i++)
37.         {
38.             sort(queue+i,queue+tail);//排序取最小序号
39.             int s=queue[i];//此起点的边全删
41.             {
42.                 in[edge[j].to]--;
43.                 if(!in[edge[j].to])//新的入度为0的点
44.                 {
45.                     queue[tail++]=edge[j].to;
46.                 }
47.             }
48.         }
49.         for(int i=0;i<tail-1;i++)
50.             cout<<queue[i]<<' ';
51.         cout<<queue[tail-1]<<endl;
52.     }
53.     return 0;
54. }

hdu3342：

Legal or Not

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

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-1). 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
【解析】：判断给出的有向图，有没有环路。用拓扑排序，拍一遍吧，环路肯定排不进去。

【代码】：
[cpp] view plain
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<string.h>
4. #include<iostream>
5. #include<algorithm>
6. #include<queue>
7. using namespace std;
8. struct NODE{
9.     int from,to;
10.     int next;
11.     NODE(int f=0,int t=0,int ne=0){
12.         from=f;to=t;next=ne;
13.     }
14. }edge[120000];
16. int in[520];//入度
17. int main()
18. {
19.     int n,m;
20.     while(cin>>n>>m,n)
21.     {
23.         memset(in,0,sizeof(in));
24.         for(int i=0;i<m;i++)
25.         {
26.             int f,t;
27.             cin>>f>>t;
28.             in[t]++;
31.         }
32.         int queue[520],tail=0;
33.         for(int i=0;i<n;i++)//入度为0的点入队
34.             if(!in[i])
35.                 queue[tail++]=i;
36.         for(int i=0;i<tail;i++)
37.         {
38.             //sort(queue+i,queue+tail);//排序取最小序号
39.             int s=queue[i];//此起点的边全删
41.             {
42.                 in[edge[j].to]--;
43.                 if(!in[edge[j].to])//新的入度为0的点
44.                 {
45.                     queue[tail++]=edge[j].to;
46.                 }
47.             }
48.         }
49.         if(tail==n)puts("YES");
50.         else puts("NO");
51.     }
52.     return 0;
53. }

0
0

个人资料
• 访问：103698次
• 积分：3019
• 等级：
• 排名：第13770名
• 原创：197篇
• 转载：9篇
• 译文：0篇
• 评论：33条
博客专栏
 ACM算法 文章：17篇 阅读：7004
 ACM荣耀之路 文章：97篇 阅读：46059
阅读排行
最新评论