#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define f first
#define s second
struct edge{
int from, to;
};
const int MAXN = 1005, MAXR = 2e5+5;
edge allEdges[MAXR];
vector<int> edgeGraph[MAXR], cycle;
int connected[MAXN][MAXN];
bool vis[MAXN][MAXN], on_stack[MAXR], vis2[MAXR];
// int vis2[MAXR];
int n, r;
void dfs(int at, int pv){
vis[at][pv] = true;
// connect all possible edges
for (int i=0; i<n; i++){
if (pv != n && i != at && i != pv && connected[at][i] && !connected[i][pv]){
assert(connected[pv][at] != 0);
edgeGraph[connected[pv][at]].push_back(connected[at][i]);
}
}
for (int i=0; i<n; i++){
if (connected[at][i] && !vis[i][at]){
dfs(i, at);
}
}
}
bool dfs2(int at) {
vis2[at] = on_stack[at] = true;
for (int u : edgeGraph[at]) {
if(on_stack[u]) {
cycle.push_back(at); // start cycle
on_stack[at] = on_stack[u] = false;
return true;
}else if(!vis2[u]) {
if(dfs2(u)) { // continue cycle
if(on_stack[at]) {
cycle.push_back(at);
on_stack[at] = false;
return true;
} else { // found u again
cycle.push_back(at);
return false;
}
}
if(!cycle.empty()) // finished with cycle
return false;
}
}
on_stack[at] = false;
return false;
}
int main(){
cin.tie(0); ios_base::sync_with_stdio(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
cin >> n >> r;
for (int i=0; i<r; i++){
int a, b;
cin >> a >> b;
a--; b--;
allEdges[2*i+1] = {a, b};
allEdges[2*i+2] = {b, a};
connected[a][b] = 2*i+1;
connected[b][a] = 2*i+2;
}
dfs(0, n);
for (int i=1; i<=2*r; i++){
if (!vis2[i]){
fill(on_stack, on_stack+2*r, false);
dfs2(i);
}
if (!cycle.empty()) break;
}
if (cycle.empty()){
cout << "no" << "\n";
return 0;
}
reverse(cycle.begin(), cycle.end());
vector<int> ans;
for (int i=0; i<sz(cycle)-1; i++){
if (i == 0){
cout << allEdges[cycle[i]].from+1 << " ";
}
cout << allEdges[cycle[i]].to+1 << " ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5036 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5588 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5840 KB |
Output is correct |
2 |
Correct |
6 ms |
5972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
7420 KB |
Output is correct |
2 |
Correct |
12 ms |
7392 KB |
Output is correct |
3 |
Correct |
32 ms |
9900 KB |
Output is correct |
4 |
Correct |
31 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
8660 KB |
Output is correct |
2 |
Correct |
19 ms |
8744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
584 ms |
35792 KB |
Output is correct |
2 |
Correct |
167 ms |
17252 KB |
Output is correct |
3 |
Correct |
573 ms |
36176 KB |
Output is correct |
4 |
Correct |
172 ms |
16972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
59000 KB |
Output is correct |
2 |
Correct |
312 ms |
63136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
30456 KB |
Output is correct |
2 |
Execution timed out |
1084 ms |
31040 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |