#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ii = pair<int,int>;
const int mxN = 1e3+5;
const int mxR = 1e5+5;
int N, R;
vector<int> al[mxN];
bool am[mxN][mxN];
bool block[mxN];
vector<int> ans;
struct UFDS {
int p[mxN], s[mxN];
UFDS() { FOR(i,1,N) p[i] = i, s[i] = 1; }
int finds(int i) { return (p[i] == i) ? i : (p[i] = finds(p[i])); }
bool sames(int i, int j) { return finds(i) == finds(j); }
bool unions(int i, int j) {
int x = finds(i), y = finds(j);
if (x == y) return 0;
if (s[x] < s[y]) swap(x,y);
p[y] = x, s[x] += s[y];
return 1;
}
};
int dist[mxN], pa[mxN];
queue<int> q;
void bfs(int s, int t) {
memset(dist,-1,sizeof dist);
memset(pa,-1,sizeof pa);
dist[s] = 0; q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
if (u == t) return;
for (int v : al[u]) if (!block[v]) {
if (dist[v] == -1) {
dist[v] = dist[u]+1;
pa[v] = u;
q.push(v);
}
}
}
return;
}
bool solve(int s) {
block[s] = 1;
for (int v : al[s]) block[v] = 1;
UFDS uf;
FOR(u,1,N) for (int v : al[u]) if (!block[u] || !block[v]) {
uf.unions(u,v);
}
FOR(i,0,SZ(al[s])-1){
int u = al[s][i];
FOR(j,i+1,SZ(al[s])-1){
int v = al[s][j];
if (!am[u][v] && uf.sames(u,v)) {
block[u] = block[v] = 0;
bfs(u,v);
for (int x = v; x != -1; x = pa[x]) ans.push_back(x);
ans.push_back(s);
return true;
}
}
}
block[s] = 0;
for (int v : al[s]) block[v] = 0;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> R;
FOR(i,1,R){
int A, B; cin >> A >> B;
al[A].push_back(B);
al[B].push_back(A);
am[A][B] = am[B][A] = 1;
}
FOR(i,1,N){
if (solve(i)) {
for (int x : ans) {
cout << x << ' ';
}
cout << '\n';
return 0;
}
}
cout << "no" << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Incorrect |
5 ms |
512 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Too short sequence |
4 |
Incorrect |
6 ms |
768 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
768 KB |
Too short sequence |
2 |
Incorrect |
5 ms |
768 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
2304 KB |
Too short sequence |
2 |
Correct |
11 ms |
1792 KB |
Too short sequence |
3 |
Correct |
478 ms |
2304 KB |
Output is correct |
4 |
Correct |
202 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
1792 KB |
Too short sequence |
2 |
Incorrect |
10 ms |
1792 KB |
Wrong answer on graph without induced cycle |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2552 KB |
Output is correct |
2 |
Correct |
163 ms |
2432 KB |
Output is correct |
3 |
Correct |
20 ms |
2560 KB |
Too short sequence |
4 |
Incorrect |
18 ms |
2560 KB |
Wrong answer on graph without induced cycle |