#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 1000;
int n, m;
int p[mxn], r[mxn];
bool a[mxn][mxn];
vector<int> ans;
vector<int> g[mxn];
queue<int> q;
bool bfs(int x){
for(int s1 : g[x])
for(int s2 : g[x]){
if(a[s1][s2]) continue;
memset(r, -1, sizeof(r));
p[x] = -1, r[x] = x;
p[s1] = x, r[s1] = s1;
p[s2] = x, r[s2] = s2;
q.push(s1), q.push(s2);
while(!q.empty()){
int c = q.front();
q.pop();
for(int i : g[c]){
if(!~r[i]){
p[i] = c, r[i] = c == x ? i : r[c];
q.push(i);
}else if(!a[r[i]][r[c]]){
for(int j = i; ~j; j = p[j]) ans.push_back(j + 1);
reverse(ans.begin(), ans.end());
for(int j = c; j != x; j = p[j]) ans.push_back(j + 1);
return 1;
}
}
}
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) a[i][i] = 1;
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
u--, v--;
a[u][v] = a[v][u] = 1;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 0; i < n; i++) if(bfs(i)) break;
if(!ans.empty()){
cout << ans[0];
for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
cout << endl;
}else{
cout << "no" << endl;
}
return 0;
}
Compilation message
indcyc.cpp: In function 'int main()':
indcyc.cpp:70:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Wrong adjacency |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
512 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
768 KB |
Wrong answer on graph without induced cycle |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
768 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
1920 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
1664 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
1792 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |