#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int n,m;
vector<int> adj[1010];
int mat[1010][1010];
int depth[1010],p[1010];
int relabel[1010];
int counter;
int visited[1010];
int rmap[1010];
int comp[1010];
void dfs(int u,int pa){
visited[u] = 1;
//cout << u << pa << "hi\n";
for (int i = 0; i < adj[u].size(); i++){
int v = adj[u][i];
if (!visited[v]){
p[v] = u;
depth[v] = depth[u]+1;
visited[v] = 1;
comp[v] = comp[u];
dfs(v,u);
}
}
relabel[u] = ++counter;
rmap[relabel[u]] = u;
}
vector<int> lowestjump[1010];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++){
int x,y;
cin >> x >> y;
mat[x][y] = mat[y][x] = 1;
adj[x].push_back(y);
adj[y].push_back(x);
}
int num = 0;
for (int i = 1; i <= n; i++){
if (!visited[i]){
depth[i] = 1;
p[i] = -1;
comp[i] = num++;
dfs(i,-1);
}
}
for (int i = 1; i <= n; i++){
if (p[i] != -1) assert(relabel[p[i]] > relabel[i]);
for (int j = 0; j < adj[i].size(); j++){
int ni = adj[i][j];
if (p[ni] == i || p[i] == ni) continue;
if (depth[i] < depth[ni]) continue;
lowestjump[i].push_back(relabel[ni]);
}
lowestjump[i].push_back(relabel[p[i]]);
sort(lowestjump[i].begin(),lowestjump[i].end());
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (i == j) continue;
if (comp[i] != comp[j] || depth[i]-depth[j] < 3) continue;
if (mat[i][j] && comp[i] == comp[j]){
int start = i, fin = j;
//cout << start << " " << fin << "\n";
int counter = 1;
int cur = start;
vector<int> temp;
temp.push_back(cur);
while (relabel[cur] < relabel[fin]){
int relabelval = *(upper_bound(lowestjump[cur].begin(),lowestjump[cur].end(),relabel[fin])-1);
if (cur == start) relabelval = *(lower_bound(lowestjump[cur].begin(),lowestjump[cur].end(),relabel[fin])-1);
int pos = rmap[relabelval];
cur = pos;
temp.push_back(cur);
//start++;
}
if (cur != fin || temp.size() < 4) continue;
for (int k = 0; k < temp.size(); k++) cout <<temp[k] << " ";
return 0;
}
else if (n <= 300){
int prev[n];
for (int k = 0; k < n; k++){
prev[k] = -1;
}
prev[i] = 0;
queue<int> q;
q.push(i);
vector<int> temp;
while (!q.empty()){
int u = q.front();
q.pop();
if (mat[u][j]){
temp.push_back(u);
continue;
}
for (int k = 0; k < adj[u].size(); k++){
int v = adj[u][k];
if (prev[v] == -1){
prev[v] = u;
q.push(v);
}
}
}
if (temp.size() > 1){
vector<int> v1,v2;
int cur = temp[0];
while (cur != i){
v1.push_back(cur);
cur = prev[cur];
}
cur = temp[1];
while (cur != i){
v2.push_back(cur);
cur = prev[cur];
}
cout << i << " ";
for (int k = v1.size()-1; k >= 0; k--) cout << v1[k] << " ";
cout << j << " ";
for (int k = 0; k < v2.size(); k++) cout << v2[k] << " ";
return 0;
}
}
}
}
cout << "no";
return 0;
}
Compilation message
indcyc.cpp: In function 'void dfs(int, int)':
indcyc.cpp:16:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
indcyc.cpp: In function 'int main()':
indcyc.cpp:52:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < adj[i].size(); j++){
~~^~~~~~~~~~~~~~~
indcyc.cpp:81:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < temp.size(); k++) cout <<temp[k] << " ";
~~^~~~~~~~~~~~~
indcyc.cpp:68:21: warning: unused variable 'counter' [-Wunused-variable]
int counter = 1;
^~~~~~~
indcyc.cpp:100:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < adj[u].size(); k++){
~~^~~~~~~~~~~~~~~
indcyc.cpp:123:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < v2.size(); k++) cout << v2[k] << " ";
~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Wrong adjacency |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
512 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
896 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1664 KB |
Wrong answer on graph without induced cycle |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1664 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5248 KB |
Output is correct |
2 |
Correct |
17 ms |
4864 KB |
Output is correct |
3 |
Correct |
41 ms |
5120 KB |
Output is correct |
4 |
Correct |
20 ms |
4736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
4736 KB |
Expected integer, but "no" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
3584 KB |
Output is correct |
2 |
Correct |
55 ms |
3580 KB |
Output is correct |
3 |
Correct |
37 ms |
5504 KB |
Output is correct |
4 |
Correct |
42 ms |
5504 KB |
Output is correct |