hdu 5154 Harry and Magical Computer

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=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 number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.

Input

There are several test cases, you should process to the end of file.
For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies. $1 leq n leq 100,1 leq m leq 10000$
The next following m lines, each line contains two numbers a b, indicates a dependencies $(a, b). 1 leq a, b leq n$

Output

Output one line for each test case. 
If the computer can finish all the process print "YES" (Without quotes).
Else print "NO" (Without quotes).

Sample Input

3 2
3 1
2 1
3 3
3 2
2 1
1 3

Sample Output

YES
NO

先建立一张有向图,再遍历,若有顶点未访问到输出"NO",否则输出"YES"。。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::pair;
15 using std::vector;
16 using std::queue;
17 using std::multimap;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int N = 110;
28 int n, m, G[N][N];
29 bool vis[N], flag[N];
30 typedef unsigned long long ull;
31 void bfs() {
32     bool f = false;
33     queue<int> que;
34     rep(i, n) {
35         if (!flag[i]) {
36             vis[i] = true;
37             que.push(i);
38         }
39     }
40     while (!que.empty()) {
41         int p = que.front(); que.pop();
42         rep(i, n) {
43             if (G[p][i]) {
44                 if (vis[i]) continue;
45                 que.push(i);
46                 vis[i] = true;
47             }
48         }
49     }
50     rep(i, n) {
51         if (!vis[i]) { f = true; break; }
52     }
53     puts(f ? "NO" : "YES");
54 }
55 int main() {
56 #ifdef LOCAL
57     freopen("in.txt", "r", stdin);
58     freopen("out.txt", "w+", stdout);
59 #endif
60     int a, b;
61     while (~scanf("%d %d", &n, &m)) {
62         cls(vis, 0), cls(flag, 0), cls(G, 0);
63         rep(i, m) {
64             scanf("%d %d", &a, &b);
65             G[b - 1][a - 1] = 1;
66             flag[a - 1] = 1;
67         }
68         bfs();
69     }
70     return 0;
71 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4603743.html