#include <bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)
using ll = long long;
const int mxn = 9 + 1e6;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
int n, m;
vector<vector<int>> adj;
namespace SUB1 {
bool check() {
return (n <= 10 && m <= 45);
}
vector<int> par;
vector<char> used;
void dfs(int u, int s, int h) {
if (h >= 4) {
vector<int> res;
int cur = u;
while (cur) {
res.push_back(cur);
cur = par[cur];
}
reverse(begin(res), end(res));
if (binary_search(begin(adj[res.back()]), end(adj[res.back()]), res.front())) {
bool f = false;
for (int i = 0; i < (int)res.size() && !f; ++i) {
for (int j = i + 1; j < (int)res.size(); ++j) {
if (j == i + 1 || (i == 0 && j == (int)res.size() - 1)) continue;
if (binary_search(begin(adj[res[i]]), end(adj[res[i]]), res[j])) {
f = true;
break;
}
}
}
if (!f) {
for (auto &v : res) {
cout << v << " ";
}
cout << "\n";
exit(0);
}
}
}
used[u] = true;
for (auto &v : adj[u]) {
if (!used[v]) {
par[v] = u;
dfs(v, s, h + 1);
}
}
used[u] = false;
}
void solve() {
par.assign(n + 1, 0);
used.assign(n + 1, 0);
for (int i = 1; i <= n; ++i) {
sort(begin(adj[i]), end(adj[i]));
}
for (int i = 1; i <= n; ++i) {
par[i] = 0;
dfs(i, i, 1);
}
cout << "no\n";
}
}
namespace SUBFULL {
/*
Ta gọi danh sách chu trình là : c[1], c[2], ..., c[n], c[1]
* Nếu tồn tại cặp (c[i], c[i + 2])
-> sẽ không thỏa mãn
* Ngược lại, nếu tồn tại cặp (c[i], c[i + 3])
-> Dãy c[i], c[i + 1], c[i + 2], c[i + 3] lại là chu trình thỏa mãn đề bài
* Dễ dàng chứng minh với các trường hợp còn lại (c[i], c[i + 4]), ...
=> Ta thấy chỉ cần kiểm soát xem có cặp (c[i], c[i + 2]) hay không
*/
void solve() {
vector<vector<bool>> check(n + 1, vector<bool>(n + 1));
for (int i = 1; i <= n; ++i) {
check[i][i] = true;
for (auto &j : adj[i]) {
check[i][j] = true;
}
}
vector<vector<int>> used(n + 1, vector<int>(n + 1)), par(n + 1, vector<int>(n + 1));
auto dfs = [&] (auto &&self, int u, int v, int p) -> void {
used[u][v] = 1;
par[u][v] = p;
for (auto &x : adj[v]) {
if (check[u][x]) {
continue;
}
if (used[v][x] == 1) {
while (v ^ x) {
cout << v << " ";
int mid = u;
u = par[u][v];
v = mid;
}
cout << x;
exit(0);
} else if (!used[v][x]) {
self(self, v, x, u);
}
}
used[u][v] = 2;
};
for (int i = 1; i + 1 <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (check[i][j] && !used[i][j]) {
dfs(dfs, i, j, 0);
}
}
}
cout << "no\n";
}
}
void _27augain(void) {
cin >> n >> m;
adj.resize(n + 1);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (SUB1::check()) {
SUB1::solve();
return;
}
SUBFULL::solve();
}
int32_t main(void) {
#define task "27augain"
cin.tie(0)->sync_with_stdio(0);
for (string iext : {"in", "inp"}) {
if (fopen((task "." + iext).c_str(), "r")) {
freopen((task "." + iext).c_str(), "r", stdin);
freopen(task ".out", "w", stdout);
}
}
int testcase = 1;
// cin >> testcase;
while (testcase--) {
_27augain();
}
return 0;
}
Compilation message (stderr)
indcyc.cpp: In function 'int32_t main()':
indcyc.cpp:150:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen((task "." + iext).c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
indcyc.cpp:151:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(task ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |