hdu 5154 Harry and Magical Computer

來源:互聯網
上載者:User

標籤:

題目串連

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

Harry and Magical ComputerDescription

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 LOCAL57     freopen("in.txt", "r", stdin);58     freopen("out.txt", "w+", stdout);59 #endif60     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

hdu 5154 Harry and Magical Computer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.