#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
const int MX = 2000;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;
int n, m;
int inc(int u, int v) {
return MX * u + v;
}
bool neg[1000][1000];
vi fin[MX * MX];
bool vis[MX * MX];
bool done[MX * MX];
vector<pi> ed;
int anc[MX * MX];
bool found = false;
vi ret;
int cnt = 0;
void dfs(int v, int p) {
if (found) return;
vis[v] = true;
vi togo;
for (int to : fin[v]) {
if (found) return;
if (!done[to]) {
if (!found && vis[to]) {
found = true;
int curr = v;
while(curr != to) {
ret.pb((curr % 2000) + 1);
curr = anc[curr];
}
ret.pb((to % 2000) + 1);
return;
} else {
togo.pb(to);
}
}
}
for (auto x : togo) {
anc[x] = v;
dfs(x, v);
}
done[v] = true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
u--, v--;
neg[u][v] = true;
neg[v][u] = true;
ed.pb(mp(u, v));
ed.pb(mp(v, u));
}
for (int i = 0; i < sz(ed); i++) {
int u = ed[i].f, v = ed[i].s;
for (int j = 0; j < n; j++) {
if (j != u && j != v && neg[v][j] && !neg[u][j]) {
fin[inc(u, v)].pb(inc(v, j));
fin[inc(v, u)].pb(inc(u, j));
}
}
}
for (auto &x : ed) {
if (found) break;
if (!done[inc(x.f, x.s)]) dfs(inc(x.f, x.s), -1);
}
if (!found) cout << "no" << '\n';
else {
for (int i = 0; i < sz(ret); i++) cout << ret[i] << " ";
cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
94328 KB |
Output is correct |
2 |
Correct |
64 ms |
94328 KB |
Output is correct |
3 |
Correct |
64 ms |
94328 KB |
Output is correct |
4 |
Correct |
70 ms |
94456 KB |
Output is correct |
5 |
Correct |
64 ms |
94328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
94456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
94328 KB |
Output is correct |
2 |
Correct |
64 ms |
94328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
94584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
95352 KB |
Output is correct |
2 |
Correct |
68 ms |
95736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
97784 KB |
Output is correct |
2 |
Correct |
74 ms |
96760 KB |
Output is correct |
3 |
Correct |
93 ms |
100344 KB |
Output is correct |
4 |
Correct |
94 ms |
101624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
100216 KB |
Output is correct |
2 |
Correct |
83 ms |
100472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
134252 KB |
Output is correct |
2 |
Correct |
182 ms |
107892 KB |
Output is correct |
3 |
Correct |
470 ms |
136204 KB |
Output is correct |
4 |
Correct |
198 ms |
112500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
195700 KB |
Output is correct |
2 |
Correct |
409 ms |
203892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
99180 KB |
Output is correct |
2 |
Correct |
197 ms |
98156 KB |
Output is correct |
3 |
Correct |
857 ms |
266732 KB |
Output is correct |
4 |
Correct |
917 ms |
272752 KB |
Output is correct |