HDU 5154 Harry and Magical Computer 有向图判环

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5154

题解:

有向图判环。

1、用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访问过的节点标记为0,如果访问到-1的节点说明说有环。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 typedef long long LL;
 7
 8 int n,m;
 9
10 const int maxm=10000+10;
11 const int maxn=111;
12
13 struct Edge{
14     int v,ne;
15     Edge(int v,int ne):v(v),ne(ne){}
16     Edge(){}
17 }egs[maxm];
18
19 int head[maxn],tot;
20 int vis[maxn];
21
22 void addEdge(int u,int v){
23     egs[tot]=Edge(v,head[u]);
24     head[u]=tot++;
25 }
26
27 bool dfs(int cur){
28     vis[cur]=-1;
29     int p=head[cur];
30     while(p!=-1){
31         Edge &e=egs[p];
32         if(vis[e.v]==0){
33             if(dfs(e.v)) return true;
34         }
35         else if(vis[e.v]==-1){
36             return true;
37         }
38         p=e.ne;
39     }
40     vis[cur]=1;
41     return false;
42 }
43
44 void init(){
45     memset(head,-1,sizeof(head));
46     memset(vis,0,sizeof(vis));
47     tot=0;
48 }
49
50 int main(){
51     while(scanf("%d%d",&n,&m)==2&&n){
52         init();
53         while(m--){
54             int u,v;
55             scanf("%d%d",&u,&v);
56             addEdge(u,v);
57         }
58         int su=1;
59         for(int i=1;i<=n;i++){
60             if(vis[i]==0){
61                 if(dfs(i)){
62                     su=0; break;
63                 }
64             }
65         }
66         if(su) puts("YES");
67         else puts("NO");
68     }
69     return 0;
70 }

2、拓扑排序。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 using namespace std;
 7 typedef long long LL;
 8
 9 int n, m;
10
11 const int maxm = 10000 + 10;
12 const int maxn = 111;
13
14 struct Edge {
15     int v, ne;
16     Edge(int v, int ne) :v(v), ne(ne) {}
17     Edge() {}
18 }egs[maxm];
19
20 int head[maxn], tot;
21 int ind[maxn];
22
23 void addEdge(int u, int v) {
24     egs[tot] = Edge(v, head[u]);
25     head[u] = tot++;
26 }
27
28 void init() {
29     memset(head, -1, sizeof(head));
30     memset(ind, 0, sizeof(ind));
31     tot = 0;
32 }
33
34 int main() {
35     while (scanf("%d%d", &n, &m) == 2 && n) {
36         init();
37         while (m--) {
38             int u, v;
39             scanf("%d%d", &u, &v);
40             addEdge(u, v);
41             ind[v]++;
42         }
43         queue<int> q;
44         for (int i = 1; i <= n; i++) {
45             if (ind[i] == 0) q.push(i);
46         }
47         while (!q.empty()) {
48             int u = q.front(); q.pop();
49             //printf("v:%d\n", v);
50             int p = head[u];
51             while (p != -1) {
52                 Edge& e = egs[p];
53                 ind[e.v]--;
54                 if (ind[e.v] == 0) q.push(e.v);
55                 p = e.ne;
56             }
57         }
58         int su = 1;
59         for (int i = 1; i <= n; i++) {
60             if (ind[i]) { su = 0; break; }
61         }
62         if (su) puts("YES");
63         else puts("NO");
64     }
65     return 0;
66 }
时间: 2024-07-29 15:45:47

HDU 5154 Harry and Magical Computer 有向图判环的相关文章

HDOJ 5154 Harry and Magical Computer floyd判环

floyd判环 Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 404 Problem Description In reward of being yearly outstanding magic student, H

hdu 5154 Harry and Magical Computer 拓扑排序

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal wi

(简单) HDU 5154 Harry and Magical Computer,图论。

Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We num

HDU 5154 Harry and Magical Computer bfs

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 499    Accepted Submission(s): 233 Problem Description In reward of being yearly outstanding magic student, Harry gets

hdu 5154 Harry and Magical Computer(BestCoder Round #25)

Harry and Magical Computer                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 472    Accepted Submission(s): 222 Problem Description In reward

hdu 5154 Harry and Magical Computer

http://acm.hdu.edu.cn/showproblem.php?pid=5154 思路:有向图判断有没有环,可以用floyd.. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int inf=1<<20; 6 7 int g[200][200]; 8 int main() 9 { 10 int n,m;

拓扑序列 之 hdu 5154 Harry and Magical Computer

/* AOV网(Activity On Vertex Network): 用图来表示工程:其中,用顶点表示活动:弧表示活动之间的制约关系. 工程是否顺利进行?---->AOV网是否存在有向回路 ******************************************* 用产生(包含所有) 顶点序列的方法,顶点序列满足: 在图中,若顶点vi到顶点vj存在路径, 则在序列中,顶点vi领先于顶点vj. 满足上述条件的顶点序列称为拓扑序列, 产生这一序列的过程称为拓扑排序. ********

COJ 3012 LZJ的问题 (有向图判环)

传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如他写了这样几个函数: void f1(){   f2();   f3();}void f2(){   f3();}void f3(){   f1();}LZJ发现他无论怎么调换函数的位置,编译器总是不能通过编译,因为编译器规定调用的函数必须在当前函数之前写.还有一种情况是这样的:void f4(){

拓扑排序 杭电5154 Harry and Magical Computer

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 265    Accepted Submission(s): 123 Problem Description In reward of being yearly outstanding magic student, Harry gets