/*
CEOI 2016 Potemkin Cycle
- Let each edge (u, v) represent a node (uv) in graph H
- There is an edge between (uv) and (vw) in H iff (uw) doesn't exist
- This means that there are no cycles of length 3 in H
- We can construct H in O(NR) time
- An answer exists iff a cycle exists in H; run DFS to find a cycle
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int u[200001], v[200001], adj[1001][1001];
vector<int> graph[200001], stck;
bool visited[200001], active[200001];
void dfs(int node, int parent = -1) {
if (active[node]) {
while (stck.back() != node) {
cout << v[stck.back()] << ' ';
stck.pop_back();
}
cout << v[node];
exit(0);
}
if (visited[node]) return;
stck.push_back(node);
visited[node] = active[node] = true;
for (int i : graph[node]) if (i != parent) dfs(i, node);
stck.pop_back();
active[node] = false;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, r;
cin >> n >> r;
for (int i = 1; i <= r; i++) {
cin >> u[i] >> v[i];
u[i + r] = v[i], v[i + r] = u[i];
adj[u[i]][v[i]] = i, adj[v[i]][u[i]] = i + r;
}
for (int i = 1; i <= 2 * r; i++) {
for (int j = 1; j <= n; j++) if (j != u[i]) {
if (adj[v[i]][j] && !adj[u[i]][j])
graph[i].push_back(adj[v[i]][j]);
}
}
for (int i = 1; i <= 2 * r; i++) if (!visited[i]) dfs(i);
cout << "no";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5452 KB |
Wrong adjacency |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5580 KB |
Output is correct |
2 |
Correct |
6 ms |
5708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
6476 KB |
Output is correct |
2 |
Correct |
13 ms |
6416 KB |
Output is correct |
3 |
Correct |
24 ms |
8400 KB |
Output is correct |
4 |
Correct |
27 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
7736 KB |
Output is correct |
2 |
Correct |
15 ms |
7800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
24748 KB |
Output is correct |
2 |
Correct |
115 ms |
12156 KB |
Output is correct |
3 |
Correct |
369 ms |
24644 KB |
Output is correct |
4 |
Correct |
118 ms |
12216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
53572 KB |
Output is correct |
2 |
Correct |
244 ms |
57760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
8736 KB |
Output is correct |
2 |
Correct |
207 ms |
8640 KB |
Output is correct |
3 |
Correct |
625 ms |
92364 KB |
Output is correct |
4 |
Correct |
717 ms |
92544 KB |
Output is correct |