#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(a[i][x]) continue;
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:71: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 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
22 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
543 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
2 ms |
768 KB |
Output is correct |
4 |
Correct |
286 ms |
796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
768 KB |
Output is correct |
2 |
Correct |
410 ms |
792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1082 ms |
1920 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
198 ms |
1664 KB |
Output is correct |
2 |
Execution timed out |
1095 ms |
1792 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
1792 KB |
Output is correct |
2 |
Correct |
118 ms |
2432 KB |
Output is correct |
3 |
Correct |
578 ms |
2680 KB |
Output is correct |
4 |
Execution timed out |
1093 ms |
2560 KB |
Time limit exceeded |