#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[MAXN], cycle;
int connected[MAXN][MAXN];
bool vis[MAXN][MAXN];
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);
}
}
}
int dfs2(int at, int id){
vis2[at] = id;
for (int i : edgeGraph[at]){
if (vis2[i] == id){
cycle.push_back(at);
return i;
}
if (vis2[i] > 0){
continue;
}
int inCycle = dfs2(i, id);
if (inCycle > 0){
cycle.push_back(at);
if (inCycle == at){
return -1;
}else{
return inCycle;
}
}else if (inCycle == -1){
return -1;
}
}
return 0;
}
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]){
dfs2(i, 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 << " ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
1364 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
3012 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
3156 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
10000 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
9172 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
15 ms |
7252 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |