#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], al2[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) {
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();
for (int v : al[u]) if (!block[v] && dist[v] == -1) {
dist[v] = dist[u]+1, pa[v] = u;
q.push(v);
}
}
}
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(u,1,N) al2[u].clear();
FOR(u,1,N){
int x = uf.finds(u);
for (int v : al[u]) {
int y = uf.finds(v);
if (x != y) {
al2[x].push_back(y);
al2[y].push_back(x);
}
}
}
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]) block[v] = 0;
}
bfs(u);
FOR(j,i+1,SZ(al[s])-1){
int v = al[s][j];
if (!am[u][v]) {
if (dist[v] != -1) {
for (int x = v; x != -1; x = pa[x]) ans.push_back(x);
ans.push_back(s);
return true;
}
block[v] = 1;
}
}
}
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 |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 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 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
16 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
896 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
10 ms |
1024 KB |
Output is correct |
4 |
Correct |
154 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
1024 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
2716 KB |
Wrong adjacency |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
2168 KB |
Output is correct |
2 |
Execution timed out |
1085 ms |
2936 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
3908 KB |
Output is correct |
2 |
Execution timed out |
1092 ms |
3840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |