HDU 3342 Legal or Not(拓扑排序判断成环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342

题目大意:n个点,m条有向边,让你判断是否有环。

解题思路:裸题,用dfs版的拓扑排序直接套用即可。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=1e2+5;
 6
 7 int n,m;
 8 int vis[N];
 9 bool G[N][N];
10
11 bool dfs(int u){
12     vis[u]=-1;
13     for(int i=0;i<n;i++){
14         if(G[u][i]){
15             if(vis[i]==-1)
16                 return false;
17             else if(!vis[i]&&!dfs(i))
18                 return false;
19         }
20     }
21     vis[u]=1;
22     return true;
23 }
24
25 bool toposort(){
26     memset(vis,0,sizeof(vis));
27     for(int i=0;i<n;i++){
28         if(!vis[i]){
29             if(!dfs(i))
30                 return false;
31         }
32     }
33     return true;
34 }
35
36 int main(){
37     while(~scanf("%d%d",&n,&m)){
38         memset(G,false,sizeof(G));
39         if(n==0&&m==0)
40             break;
41         for(int i=1;i<=m;i++){
42             int a,b;
43             scanf("%d%d",&a,&b);
44             G[a][b]=true;
45         }
46         if(toposort())
47             puts("YES");
48         else
49             puts("NO");
50     }
51     return 0;
52 }
时间: 2024-10-10 14:13:52

HDU 3342 Legal or Not(拓扑排序判断成环)的相关文章

hdu 3342 Legal or Not - 拓扑排序

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

hdu 3342 Legal or Not 拓扑排序判断环。 现在的我,除了刷水题,,还能干什么

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4960    Accepted Submission(s): 2270 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

[ACM] hdu 3342 Legal or Not (拓扑排序)

Legal or Not 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 excha

HDU 1258 确定比赛名次 &amp;&amp;HDU 3342 Legal or Not 【临接表+拓扑排序】

HDU 1258 链接:click here HDU 3342 链接:click here 题意: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14142    Accepted Submission(s): 5667 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,

HDU 3213 Box Relations(拓扑排序构造)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题意:有n个长方体,四种限制条件.(1)I x y x和y有相交:(2)X/Y/Z  x y x的最大X/Y/Z坐标小于y的最大X/Y/Z.构造出这样的n个长方体. 思路:首先,XYZ三个方向是可以分开考 虑的.那么我们可以一个个分别求解.将每个长方体拆成左上角右下角两个点,我们假设现在考虑X方向,也即是一个长方体对应两个X方向的点,共2*n个点, 边<i,j>表示i小于j,那么首先有边&l

拓扑排序 --- 判断是否有回路

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3823    Accepted Submission(s): 1738 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

HDU 2467 Reward(逆拓扑排序)

拓扑排序的变形,逆序建图就好了 Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3951    Accepted Submission(s): 1203 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival

HDU 4324 Triangle LOVE (拓扑排序)

Triangle LOVE Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don't love B, then B must love A, vice versa. And there is no possibility that two people love each other, wh

hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3566    Accepted Submission(s): 1395 Problem Description Recently, scientists find that there is love between any of two people. For