#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int maxn = 1000;
int n, m;
vector<int> graph[1 + maxn];
bool matrix[1 + maxn][1 + maxn];
bool neigh[1 + maxn];
int cur_mid;
int dist[1 + maxn];
int prv[1 + maxn];
void bfs(int start) {
queue<int> q;
queue<int> all;
q.push(start);
all.push(start);
prv[start] = -1;
while(!q.empty()) {
int cur = q.front();
q.pop();
for(int nei : graph[cur]) {
if(prv[nei] == 0 && (!neigh[cur] || (cur == start && !neigh[nei]))) {
q.push(nei);
all.push(nei);
dist[nei] = dist[cur] + 1;
prv[nei] = cur;
if(neigh[nei] && !matrix[start][nei]) {
while(nei != start) {
cout << nei << " ";
nei = prv[nei];
}
cout << start << " " << cur_mid << "\n";
exit(0);
}
}
}
}
while(!all.empty()) {
dist[all.front()] = prv[all.front()] = 0;
all.pop();
}
}
void try_for(int cur) {
cur_mid = cur;
if(graph[cur].empty()) {
return;
}
for(int nei : graph[cur]) {
neigh[nei] = true;
}
neigh[cur] = true;
for(int start : graph[cur]) {
bfs(start);
}
for(int nei : graph[cur]) {
neigh[nei] = false;
}
neigh[cur] = false;
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
matrix[a][b] = matrix[b][a] = true;
}
for(int i = 1; i <= n; i++) {
try_for(i);
}
cout << "no\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
4 ms |
860 KB |
Output is correct |
4 |
Correct |
67 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
33 ms |
836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
1884 KB |
Output is correct |
2 |
Correct |
11 ms |
1628 KB |
Output is correct |
3 |
Execution timed out |
1097 ms |
1920 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1756 KB |
Output is correct |
2 |
Execution timed out |
1050 ms |
1624 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
1884 KB |
Output is correct |
2 |
Correct |
133 ms |
1880 KB |
Output is correct |
3 |
Correct |
163 ms |
1984 KB |
Output is correct |
4 |
Execution timed out |
1039 ms |
2140 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |