This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
bool ok = (m == n - 1);
for (int i = 0; i < n; i++) {
for (int j : g[i]) {
if (j != i - 1 && j != i + 1) {
ok = false;
}
}
}
if (ok) {
if (n <= 2) {
cout << "YES" << '\n';
cout << n << '\n';
for (int i = 1; i <= n; i++) {
cout << 1 << " ";
}
return 0;
}
cout << "YES" << '\n';
cout << 2 * (n - 2) << '\n';
for (int iter = 0; iter < 2; iter++) {
for (int i = n - 1; i > 1; i--) {
cout << i << " ";
}
}
return 0;
}
const int inf = (int) 1e9;
vector<int> dist(1 << n, inf);
vector<int> que(1, (1 << n) - 1);
dist[que.back()] = 0;
vector<pair<int, int>> prv(1 << n, {-1, -1});
for (int b = 0; b < (int) que.size(); b++) {
int mask = que[b];
for (int i = 0; i < n; i++) {
if (!(mask >> i & 1)) {
continue;
}
int nmask = 0;
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
if (!(mask >> j & 1)) {
continue;
}
for (int v : g[j]) {
nmask |= (1 << v);
}
}
if (dist[nmask] > dist[mask] + 1) {
dist[nmask] = dist[mask] + 1;
prv[nmask] = {mask, i};
que.push_back(nmask);
}
}
}
if (dist[0] == inf) {
cout << "NO" << '\n';
return 0;
}
int c = 0;
vector<int> res;
while (c != (1 << n) - 1) {
int nc = prv[c].first;
res.push_back(prv[c].second);
c = nc;
}
cout << "YES" << '\n';
cout << (int) res.size() << '\n';
for (int i : res) {
cout << i + 1 << " ";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |