/*
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[100001], v[100001], adj[1001][1001];
vector<int> graph[100001];
int prv[100001];
void dfs(int node, int parent = -1) {
prv[node] = parent;
for (int i : graph[node]) if (i != parent) {
if (prv[i]) {
vector<int> edges;
int curr = node;
while (curr != prv[i]) {
edges.push_back(curr);
curr = prv[curr];
}
int m = edges.size();
for (int j = 0; j < m; j++) {
if (u[edges[j]] != u[edges[(j + 1) % m]] &&
u[edges[j]] != v[edges[(j + 1) % m]])
cout << u[edges[j]] << ' ';
else
cout << v[edges[j]] << ' ';
}
exit(0);
} else dfs(i, node);
}
}
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];
adj[u[i]][v[i]] = adj[v[i]][u[i]] = i;
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= n; j++) if (j != u[i] && j != v[i]) {
if (adj[u[i]][j] && !adj[v[i]][j])
graph[i].push_back(adj[u[i]][j]);
if (!adj[u[i]][j] && adj[v[i]][j])
graph[i].push_back(adj[v[i]][j]);
}
}
for (int i = 1; i <= r; i++) if (!prv[i]) dfs(i);
cout << "no";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Too short sequence |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Repeated vertex |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3020 KB |
Too short sequence |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3276 KB |
Too short sequence |
2 |
Incorrect |
4 ms |
3328 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
4104 KB |
Wrong answer on graph without induced cycle |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5324 KB |
Too short sequence |
2 |
Incorrect |
9 ms |
5440 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
205 ms |
21548 KB |
Repeated vertex |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
51268 KB |
Too short sequence |
2 |
Incorrect |
129 ms |
55128 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
6224 KB |
Too short sequence |
2 |
Correct |
102 ms |
6212 KB |
Output is correct |
3 |
Incorrect |
339 ms |
89852 KB |
Wrong adjacency |
4 |
Halted |
0 ms |
0 KB |
- |