-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF915D.cpp
38 lines (34 loc) · 811 Bytes
/
CF915D.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
#include <bits/stdc++.h>
using namespace std;
vector<int> g[500], tpsort;
int nth[500];
int n, m, u, v;
bool testcyclicshift(bool i) {
int ans = 0;
for (int v = 0; v < n; ++v) {
for (int u : g[v]) {
int k = (nth[v] + i) % n, j = (nth[u] + i) % n;
ans += k > j;
}
}
return ans <= 1;
}
bool vis[500] = {0};
void dfs(int v) {
vis[v] = 1;
for (int u : g[v])
if (!vis[u]) dfs(u);
tpsort.push_back(v);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &u, &v), --u, --v;
g[u].push_back(v);
}
for (int i = 0; i < n; ++i) if (!vis[i]) dfs(i);
reverse(tpsort.begin(), tpsort.end());
for (int i = 0; i < n; ++i) nth[tpsort[i]] = i;
for (int i = 0; i < n; ++i) if (testcyclicshift(i)) return 0 * puts("YES");
puts("NO");
}