This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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;
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |