#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <complex>
#include <stack>
#include <set>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double>
#define ld long double
#define pld pair<ld,ld>
#define iii pair<ii,int>
using namespace std;
const int INF = 1e9+10;
const int MAXN = 1000+10;
int n,m;
bool mat[MAXN][MAXN];
bool vis[MAXN][MAXN];
bool validEdge[MAXN][MAXN];
stack<ii> cyc;
set<int> allnode;
bool done = 0;
inline bool isok(int a,int b,int c){
return (!(mat[a][b] and mat[b][c] and mat[a][c])) and mat[b][c];
}
void dfs1(ii node){
if(done)return;
cyc.push(node);
//cout << node.ff << " " << node.ss << endl;
if(vis[node.ff][node.ss]){
// we got our cycle
allnode.insert(node.ff);
allnode.insert(node.ss);
cyc.pop();
while(cyc.size()>0){
ii x = cyc.top();cyc.pop();
validEdge[x.ff][x.ss] = validEdge[x.ss][x.ff] = 1;
allnode.insert(x.ff);
allnode.insert(x.ss);
if(x == node)break;
}
done = 1;
return;
}
vis[node.ff][node.ss] = 1;
FOR(nxt,n){
if(nxt == node.ff)continue;
if(isok(node.ff,node.ss,nxt)){
// this is a valid edge;
dfs1({node.ss,nxt});
if(done)return;
}
}
if(cyc.size()>0)cyc.pop();
}
bool impNode[MAXN];
bool vis2[MAXN];
stack<int> st;
vector<int> cc;
void dfs2(int node,int p = -1){
if(done)return;
st.push(node);
//cout << node << endl;
vis2[node] = 1;
FOR(i,n){
if(node == i)continue;
if(p == i)continue;
if(done)return;
if(mat[node][i] and vis2[i] and impNode[i] and !validEdge[node][i]){
st.pop();
while(!st.empty()){
int x = st.top();st.pop();
cc.pb(x);
if(x == i)break;
}
done = 1;
return;
}
if(validEdge[node][i]){
if(vis2[i]){
//st.pop();
while(!st.empty()){
int x = st.top();st.pop();
cc.pb(x);
if(x == i)break;
}
done = 1;
}else{
dfs2(i,node);
}
}
if(done)return;
}
if(st.size()>0)st.pop();
}
int main(){
cin >> n >> m;
FOR(i,m){
int a,b;
cin >> a >> b;
a--;b--;
mat[a][b] = mat[b][a] = 1;
}
// input done
FOR(i,n){
dfs1({i,i});
}
if(!done){
cout << "no" << endl;
return 0;
}
int x;
for(auto e:allnode){
impNode[e] = 1;
x = e;
}
for(auto e:allnode){
//cout << e << endl;
}
done = 0;
dfs2(x);
if(cc.empty()){
cout << "no" << endl;
}else{
for(auto e:cc){
cout << e+1 << " ";
}
cout << endl;
}
return 0;
}
Compilation message
indcyc.cpp: In function 'int main()':
indcyc.cpp:150:11: warning: unused variable 'e' [-Wunused-variable]
for(auto e:allnode){
^
indcyc.cpp:154:6: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
dfs2(x);
~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
300 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
Wrong adjacency |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
768 KB |
Output is correct |
2 |
Incorrect |
3 ms |
684 KB |
Expected integer, but "no" found |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
768 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
1368 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
1508 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
824 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |