-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathLA6195.cc
41 lines (38 loc) · 1.08 KB
/
LA6195.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// LA6195 The Dueling Philosophers Problem
// 陈锋
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <functional>
#include <algorithm>
#include <queue>
#include <string>
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
using namespace std;
const int MAXN = 1024;
vector<int> G[MAXN]; // 有向图
int IND[MAXN]; // IND[u]是u的入度
int main(){
int n, m, u, d;
queue<int> q;
while(cin>>n>>m && n && m){
_for(i, 0, n) G[i].clear();
fill_n(IND, n, 0);
_for(i, 0, m) cin>>d>>u, G[d-1].push_back(u-1), IND[u-1]++;
int cnt = 0, ans = 1;
bool flag = false;
_for(i, 0, n) if(IND[i] == 0) q.push(i);
while(!q.empty()){
cnt++;
if(q.size() > 1) flag = true;
u = q.front(); q.pop();
for(int v : G[u]){ IND[v]--; if(IND[v] == 0) q.push(v); }
}
if(cnt != n) ans = 0;
else if(flag) ans = 2;
cout<<ans<<endl;
}
return 0;
}
// 1674618 6195 The Dueling Philosophers Problem Accepted C++ 0.476 2015-04-11 14:04:21