-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheulerian.cpp
113 lines (102 loc) · 2.51 KB
/
eulerian.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <climits>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
using namespace std;
using ll = long long;
using ii = pair<ll,ll>;
unordered_map<ll, vector<ll>> IN;
unordered_map<ll, vector<ll>> OUT;
vector<ll> visited;
unordered_set<ll> bfs(int i) {
queue<ll> q;
unordered_set<ll> s;
visited[i] = 1; q.push(i); s.insert(i);
while (!q.empty()) {
ll u = q.front(); q.pop();
for (auto& v:IN[u]) {
if (visited[v]) continue;
visited[v] = 1;
q.push(v);
s.insert(v);
}
for (auto& v:OUT[u]) {
if (visited[v]) continue;
visited[v] = 1;
q.push(v);
s.insert(v);
}
}
return s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("1.in","r",stdin);
ll n,m; cin >> n >> m;
if (m==0) {
cout << "anywhere"; return 0;
}
while (m--) {
ll u,v; cin >> u >> v;
IN[v].push_back(u);
OUT[u].push_back(v);
}
bool isConnected = true;
int cc = 0; visited.assign(n+1,0);
for (ll i=1; i<=n; i++) {
if (visited[i]) continue;
unordered_set<ll> s = bfs(i); cc++;
if (cc > 1) {
if (s.size() > 1) isConnected = false;
else {
ll x = *s.begin();
if (!IN[x].empty() || !OUT[x].empty()) isConnected = false;
}
}
}
bool hasCircuit = true;
bool hasPath = true;
ll start = 0; ll end = 0;
ll startc = 0; ll endc = 0;
for (ll i=1; i<=n; i++) {
if (abs((int)IN[i].size() - (int)OUT[i].size()) > 1) {
hasPath = false; break;
}
if ((int)OUT[i].size() - (int)IN[i].size() == 1) {
hasCircuit = false;
start = i;
startc++;
}
if ((int)IN[i].size() - (int)OUT[i].size() == 1) {
hasCircuit = false;
end = i;
endc++;
}
}
if (!isConnected) {
cout << "no"; return 0;
}
if (!hasPath) {
cout << "no"; return 0;
}
if (hasCircuit) {
cout << "anywhere"; return 0;
} else {
if (startc == 1 && endc == 1) {
cout << start << " " << end; return 0;
}
cout << "no"; return 0;
}
}