#include <bits/stdc++.h>
using namespace std;
int N,M, AM[1005][1005], visited[200005], p[200005];
pair<int,int> EL[200005];
vector<int> lst[200005], ans;
void dfs (int x, int par) {
visited[x] = 1, p[x] = par;
for (int v: lst[x]) {
if (visited[v] == 0) dfs(v,x);
if (visited[v] == 1 && ans.empty()) {
for (int i = x; i != v; i = p[i]) ans.push_back(EL[i].first);
ans.push_back(EL[v].first);
}
}
visited[x] = 2;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> M;
memset(AM,-1,sizeof(AM));
for (int i = 0; i < M; ++i) {
cin >> EL[i].first >> EL[i].second;
EL[i].first--, EL[i].second--;
EL[i+M].first = EL[i].second, EL[i+M].second = EL[i].first;
AM[EL[i].first][EL[i].second] = i, AM[EL[i].second][EL[i].first] = i+M;
}
for (int i = 0; i < M*2; ++i) for (int j = 0; j < N; ++j)
if (j != EL[i].first && j != EL[i].second && AM[EL[i].second][j] != -1 && AM[EL[i].first][j] == -1)
lst[i].push_back(AM[EL[i].second][j]);
memset(visited,0,sizeof(visited));
for (int i = 0; i < M*2 && ans.empty(); ++i) if (visited[i] == 0) dfs(i,-1);
if (ans.empty()) cout << "no";
else {
int l = 0, r = ans.size()-1;
for (int i = 0; i < ans.size(); ++i) for (int j = i+2; j < ans.size(); ++j) if (AM[ans[i]][ans[j]] != -1 && j-i < r-l) l = i, r = j;
for (int i = l; i <= r; ++i) cout << ans[i]+1 << ' ';
}
return 0;
}
Compilation message
indcyc.cpp: In function 'int main()':
indcyc.cpp:38:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0; i < ans.size(); ++i) for (int j = i+2; j < ans.size(); ++j) if (AM[ans[i]][ans[j]] != -1 && j-i < r-l) l = i, r = j;
| ~~^~~~~~~~~~~~
indcyc.cpp:38:66: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0; i < ans.size(); ++i) for (int j = i+2; j < ans.size(); ++j) if (AM[ans[i]][ans[j]] != -1 && j-i < r-l) l = i, r = j;
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9760 KB |
Output is correct |
3 |
Correct |
4 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9760 KB |
Output is correct |
5 |
Correct |
5 ms |
9636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9760 KB |
Output is correct |
2 |
Correct |
4 ms |
9760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9896 KB |
Output is correct |
2 |
Correct |
6 ms |
10024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
10076 KB |
Output is correct |
2 |
Correct |
14 ms |
10060 KB |
Output is correct |
3 |
Correct |
25 ms |
11944 KB |
Output is correct |
4 |
Correct |
26 ms |
12100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11212 KB |
Output is correct |
2 |
Correct |
14 ms |
11340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
376 ms |
26088 KB |
Output is correct |
2 |
Correct |
114 ms |
13164 KB |
Output is correct |
3 |
Correct |
325 ms |
26072 KB |
Output is correct |
4 |
Correct |
136 ms |
13208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
54644 KB |
Output is correct |
2 |
Correct |
169 ms |
58668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
12300 KB |
Output is correct |
2 |
Correct |
185 ms |
12512 KB |
Output is correct |
3 |
Correct |
538 ms |
93948 KB |
Output is correct |
4 |
Correct |
529 ms |
94180 KB |
Output is correct |