#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 1005, MAXR = 200'005;
int adjmat[MAXN][MAXN];
int vis[MAXR], p[MAXR];
pair<int, int> elist[MAXR];
vector<int> oadjlist[MAXN], adjlist[MAXR];
bool ans_found = 0;
void dfs(int x, int par) {
if (ans_found) return;
vis[x] = 2;
p[x] = par;
for (int nxt : adjlist[x]) {
if (!vis[nxt]) dfs(nxt, x);
else if (vis[nxt] == 2 && !ans_found) {
ans_found = 1;
for (int curr = x; curr != nxt; curr = p[curr]) cout << elist[curr].first << ' ';
cout << elist[nxt].first;
}
if (ans_found) return;
}
vis[x] = 1; // not useful anymore
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nodes, edges; cin >> nodes >> edges;
memset(adjmat, -1, sizeof(adjmat));
for (int i = 0; i < edges; i++) {
int a, b; cin >> a >> b;
elist[i] = { a, b }; elist[i + edges] = { b, a };
adjmat[a][b] = i; adjmat[b][a] = i + edges;
oadjlist[a].push_back(b); oadjlist[b].push_back(a);
}
for (int i = 1; i <= nodes; i++) {
int sz = oadjlist[i].size();
for (int j = 0; j < sz; j++)
for (int k = 0; k < j; k++) {
int a = oadjlist[i][j], b = oadjlist[i][k];
if (adjmat[a][b] == -1) {
adjlist[adjmat[a][i]].push_back(adjmat[i][b]);
adjlist[adjmat[b][i]].push_back(adjmat[i][a]);
}
}
}
for (int i = 0; i < edges * 2; i++)
if (!vis[i])
dfs(i, -1);
if (!ans_found) cout << "no";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
4 ms |
8892 KB |
Output is correct |
4 |
Correct |
4 ms |
8908 KB |
Output is correct |
5 |
Correct |
4 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8880 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9164 KB |
Output is correct |
2 |
Correct |
6 ms |
9292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9412 KB |
Output is correct |
2 |
Correct |
7 ms |
9396 KB |
Output is correct |
3 |
Correct |
12 ms |
11336 KB |
Output is correct |
4 |
Correct |
12 ms |
11340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10596 KB |
Output is correct |
2 |
Correct |
10 ms |
10700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
26204 KB |
Output is correct |
2 |
Correct |
26 ms |
12868 KB |
Output is correct |
3 |
Correct |
91 ms |
26180 KB |
Output is correct |
4 |
Correct |
27 ms |
12996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
54408 KB |
Output is correct |
2 |
Correct |
136 ms |
58452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
12716 KB |
Output is correct |
2 |
Correct |
63 ms |
13676 KB |
Output is correct |
3 |
Correct |
261 ms |
94268 KB |
Output is correct |
4 |
Correct |
286 ms |
94688 KB |
Output is correct |