#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 7;
const int INF = (int) 1e9 + 7;
int n, m, dist[N], par[N];
bool is_edge[N][N];
bool valid[N], vis[N];
vector<int> g[N], g2[N], gather;
int last_time[N], current_time;
void dfs(int a) {
gather.push_back(a);
vis[a] = 1;
for (auto &b : g2[a]) {
if (!vis[b] && valid[b]) {
dfs(b);
}
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
is_edge[a][b] = is_edge[b][a] = 1;
}
for (int i = 1; i <= n; i++) {
sort(g[i].begin(), g[i].end());
}
for (int root = 1; root <= n; root++) {
for (int i = 1; i <= n; i++) {
valid[i] = 1;
vis[i] = 0;
g2[i].clear();
}
valid[root] = 0;
for (auto &v : g[root]) {
valid[v] = 0;
}
for (int i = 1; i <= n; i++) {
if (valid[i]) {
for (auto &j : g[i]) {
if (valid[j]) {
g2[i].push_back(j);
}
}
}
}
int v1_sol, v2_sol;
bool found = 0;
for (int zd = 1; zd <= n; zd++) {
if (vis[zd] == 0 && valid[zd]) {
gather.clear();
dfs(zd);
vector<int> verts;
current_time++;
for (auto &member : gather) {
for (auto &copil : g[member]) {
if (copil != root && !valid[copil] && last_time[copil] < current_time) {
last_time[copil] = current_time;
verts.push_back(copil);
}
}
}
if ((int) verts.size() >= 2) {
for (auto &v1 : verts) {
for (auto &v2 : verts) {
if (found) break;
if (v1 == v2) break;
if (is_edge[v1][v2]) continue;
v1_sol = v1;
v2_sol = v2;
found = 1;
}
}
}
}
}
if (!found) continue;
int v1 = v1_sol, v2 = v2_sol;
for (int i = 1; i <= n; i++) {
dist[i] = INF;
par[i] = 0;
}
dist[root] = -1;
for (auto &v : g[root]) {
if (v != v1 && v != v2) {
dist[v] = -1;
}
}
queue<int> q;
dist[v1] = 0;
q.push(v1);
while (!q.empty()) {
int a = q.front();
q.pop();
for (auto &b : g[a]) {
if (dist[b] == INF) {
dist[b] = 1 + dist[a];
par[b] = a;
q.push(b);
}
}
}
while (v2 != v1) {
cout << v2 << " ";
v2 = par[v2];
}
cout << v1 << " " << root << "\n";
return 0;
}
cout << "no\n";
}
Compilation message
indcyc.cpp: In function 'int main()':
indcyc.cpp:115:15: warning: 'v1_sol' may be used uninitialized in this function [-Wmaybe-uninitialized]
115 | while (v2 != v1) {
| ~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
780 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
20 ms |
824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
19 ms |
764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2420 KB |
Output is correct |
2 |
Correct |
9 ms |
1876 KB |
Output is correct |
3 |
Correct |
533 ms |
2432 KB |
Output is correct |
4 |
Correct |
250 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1876 KB |
Output is correct |
2 |
Correct |
342 ms |
2016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1788 KB |
Output is correct |
2 |
Correct |
22 ms |
1824 KB |
Output is correct |
3 |
Correct |
30 ms |
2764 KB |
Output is correct |
4 |
Correct |
587 ms |
2824 KB |
Output is correct |