#include<bits/stdc++.h>
#define DEBUG false
using namespace std;
vector<vector<int> > g, h;
int n, r;
vector<int> par;
int find(int cur){
if(par[cur] == cur) return cur;
return par[cur] = find(par[cur]);
}
void merge(int a, int b){
a = find(a);
b = find(b);
if(a == b) return;
par[b] = a;
}
vector<vector<int> > l;
vector<vector<pair<int, int> > >el;
void bfs(int m, int u, int v){
vector<int> dist(n, INT_MAX);
vector<int> dpar(n, -1);
dist[u] = 0;
queue<int> q; q.push(u);
while(!q.empty()){
int top = q.front(); q.pop();
for(int ad: g[top]){
if(!((ad == u || ad == v) || (ad != m && !h[ad][m]))) continue;
if(dist[ad] == INT_MAX){
dist[ad] = dist[top]+1;
q.push(ad);
dpar[ad] = top;
}
}
}
assert(dist[v] != INT_MAX);
vector<int> path = {u, m};
int cur = v;
while(cur != u){
path.push_back(cur);
cur = dpar[cur];
}
for(int k: path) cout<<k+1<<" ";
cout<<endl;
exit(0);
}
vector<bool> ne;
void rec(vector<int> v, vector<pair<int, int> > e){
if(v.size() < 4) return;
if(DEBUG) cout<<"REC: ";
if(DEBUG) for(int u: v) cout<<u<<" ";
if(DEBUG) cout<<endl;
//reset all the dsu of v
for(int u: v) par[u] = u;
//select main node, and find neighbors
int m = v[0];
for(int u: v){
if(h[u][m]) ne[u] = true;
else ne[u] = false;
}
ne[m] = true;
if(DEBUG) for(int i = 0; i < n; i++) cout<<i<<" : "<<ne[i]<<endl;
for(pair<int, int> ed: e){
if(ne[ed.first] || ne[ed.second]) continue;
merge(ed.first, ed.second);
if(DEBUG) cout<<"MERGE "<<ed.first<<" , "<<ed.second<<endl;
}
for(int u: v) l[u].clear();
for(pair<int, int> ed: e){
if(ne[ed.first] && !ne[ed.second]){
int t = find(ed.second);
l[t].push_back(ed.first);
if(DEBUG) cout<<"PUSH "<<t<<" "<<ed.first<<endl;
}else if(!ne[ed.first] && ne[ed.second]){
int t = find(ed.first);
l[t].push_back(ed.second);
if(DEBUG) cout<<"PUSH "<<t<<" "<<ed.second<<endl;
}
}
if(DEBUG) cout<<"REACHED"<<endl;
//for each l find
for(int u: v) el[u].clear();
for(int u: v){
if(l[u].size() < 2) continue;
if(DEBUG) cout<<"DOING for "<<u<<endl;
//do a double loop on all pairs to check if they are adjacent
for(int i = 0; i < l[u].size(); i++){
for(int j = i+1; j < l[u].size(); j++){
if(h[l[u][i]][l[u][j]] == false){
//found an answer, output it somehow
//and then exit
//find shortest path from i to j without having an edge to m
bfs(m, l[u][i], l[u][j]);
}else{
el[u].push_back({l[u][i], l[u][j]});
if(DEBUG) cout<<"PUSH "<<l[u][i]<<" "<<l[u][j]<<endl;
}
}
}
}
if(DEBUG) cout<<"REACHED"<<endl;
vector<int> nel;
vector<pair<int, int> > edl;
for(int u: v){
if(ne[u] && u != m){
nel.push_back(u);
}else{
l[find(u)].push_back(u);
}
}
for(pair<int, int>& ed: e){
if(ne[ed.first] == true && ne[ed.second] == false){
el[find(ed.second)].push_back(ed);
}else if(ne[ed.first] == false && ne[ed.first] == true){
el[find(ed.first)].push_back(ed);
}else if(ne[ed.first] && ne[ed.second]){
if(ed.first != m && ed.second != m) {
edl.push_back(ed);
}
}else{
el[find(ed.first)].push_back(ed);
}
}
if(DEBUG) cout<<"REACHED 1"<<endl;
for(int u: v){
if(l[u].size() >= 4) rec(l[u], el[u]);
}
rec(nel, edl);
if(DEBUG) cout<<"REACHED 2"<<endl;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>r;
g.resize(n);
h = vector<vector<int> > (n, vector<int>(n));
par.resize(n);
l.resize(n); el.resize(n);
ne.resize(n);
vector<pair<int, int> > elist(r);
for(int i = 0; i < r; i++){
int u, v; cin>>u>>v; u--, v--;
g[u].push_back(v);
g[v].push_back(u);
elist[i] = {u, v};
h[u][v] = 1; h[v][u] = 1;
}
vector<int> v;
for(int i = 0; i < n; i++) v.push_back(i);
rec(v, elist);
cout<<"no"<<endl;
}
Compilation message
indcyc.cpp: In function 'void rec(std::vector<int>, std::vector<std::pair<int, int> >)':
indcyc.cpp:97:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < l[u].size(); i++){
~~^~~~~~~~~~~~~
indcyc.cpp:98:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = i+1; j < l[u].size(); j++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Too short sequence |
2 |
Incorrect |
2 ms |
384 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Too short sequence |
2 |
Incorrect |
2 ms |
384 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
896 KB |
Wrong answer on graph without induced cycle |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
896 KB |
Too short sequence |
2 |
Incorrect |
4 ms |
896 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
6016 KB |
Too short sequence |
2 |
Correct |
13 ms |
5220 KB |
Too short sequence |
3 |
Incorrect |
22 ms |
6016 KB |
Wrong answer on graph without induced cycle |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5120 KB |
Too short sequence |
2 |
Incorrect |
11 ms |
5248 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
50980 KB |
Too short sequence |
2 |
Correct |
328 ms |
227756 KB |
Output is correct |
3 |
Correct |
27 ms |
6784 KB |
Too short sequence |
4 |
Incorrect |
27 ms |
6784 KB |
Wrong answer on graph without induced cycle |