-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamiltonian-path.cpp
50 lines (46 loc) · 1.03 KB
/
hamiltonian-path.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<bool> used;
vector<vector<int>> g;
vector<int> sorted;
void dfs (int v) {
used[v] = true;
for (auto i = g[v].begin(); i != g[v].end(); i++) {
if (!used[*i]) {
dfs(*i);
}
}
sorted.push_back(v);
}
int main () {
freopen("hamiltonian.in", "r", stdin);
freopen("hamiltonian.out", "w", stdout);
int n, m;
cin >> n >> m;
used.resize(n);
g.resize(n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
}
for (int i = 0; i < n; i++) {
if (!used[i]) {
dfs(i);
}
}
bool have = true;
for (int i = 1; i < sorted.size(); i++) {
bool tmp = false;
for (int j = 0; j < g[sorted[i]].size(); j++) {
if (g[sorted[i]][j] == sorted[i - 1]) {
tmp = have;
}
}
have = min (have, tmp);
}
cout << ((have) ? ("YES") : ("NO"));
}