#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 2000000000
const int N =1010 , M = 100010;
bitset< N > con[N] , con2[N] , can2;
int n, m , vi = 0 , vis[N] , pre[N] , prnt[N] , a , b;
vector<int> g[N];
pair<int,int> edges[M];
int find(int u){
return (u == prnt[u] ? u : prnt[u] = find(prnt[u]));
}
bool BFS(int s,int e){
vi++;
queue < pair<int,int> > q;
vis[s] = vi;
pre[s] = s;
for(int i=0;i<g[s].size();i++){
int node = g[s][i];
if(node == e || (con[node][e])) continue;
vis[node] = vi;
q.push(make_pair(node,s));
}
while(!q.empty()){
int node = q.front().first;
int last = q.front().second;
q.pop();
pre[node] = last;
if(node == e) return true;
for(int i=0;i<g[node].size();i++){
int newnode = g[node][i];
if(vis[newnode] == vi || (con[newnode][s] && con[newnode][e])) continue;
vis[newnode] = vi;
q.push(make_pair(newnode,node));
}
}
return false;
}
void make(int u ,int v){
BFS(u,v);
int cur = v;
while(pre[cur] != cur){
printf("%d ",cur);
cur = pre[cur];
}
printf("%d\n",cur);
exit(0);
}
int main() {
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int u ,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
con[u][v] = con[v][u] = true;
edges[i] = make_pair(u,v);
}
for(int i=1;i<=n;i++){
for(int i=1;i<=n;i++){
prnt[i] = i;
can2[i] = false;
}
for(int j=0;j<m;j++){
if(edges[j].first == i || edges[j].second == i) continue;
a = find(edges[j].first);
b = find(edges[j].second);
if(a == b) continue;
if((con[a][i] && con[b][i]) || (!con[a][i] && !con[b][i])){
prnt[a] = b;
}
}
for(int j=0;j<m;j++){
if(edges[j].first == i || edges[j].second == i) continue;
a = find(edges[j].first);
b = find(edges[j].second);
if(a == b) continue;
if(con2[a][b]) continue;
con2[a][b] = con2[b][a] = true;
if(con[b][i]){
if(can2[a]){
make(i,edges[j].second);
}
else
can2[a] = true;
}
else{
if(can2[b]){
make(i,edges[j].first);
}
else
can2[b] = true;
}
}
for(int j=0;j<m;j++){
a = find(edges[j].first);
b = find(edges[j].second);
con2[a][b] = con2[b][a] = false;
}
}
puts("no");
return 0;
}
Compilation message
indcyc.cpp: In function 'bool BFS(int, int)':
indcyc.cpp:20:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++){
^
indcyc.cpp:32:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
^
indcyc.cpp: In function 'int main()':
indcyc.cpp:55:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
indcyc.cpp:58:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&u,&v);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3088 KB |
Output is correct |
2 |
Correct |
0 ms |
3088 KB |
Output is correct |
3 |
Correct |
0 ms |
3088 KB |
Output is correct |
4 |
Correct |
0 ms |
3088 KB |
Output is correct |
5 |
Correct |
0 ms |
3088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
3088 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3088 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
3220 KB |
Output is correct |
2 |
Correct |
3 ms |
3220 KB |
Output is correct |
3 |
Correct |
0 ms |
3220 KB |
Output is correct |
4 |
Correct |
46 ms |
3220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
3088 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
259 ms |
3616 KB |
Output is correct |
2 |
Correct |
236 ms |
3484 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
3616 KB |
Execution timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
573 ms |
3352 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
993 ms |
4012 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |