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 std::vector;
using std::array;
using std::pair;
using std::tuple;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
vector<vector<int>> graph(N);
for (int i = 0; i < M; ++i) {
int a, b;
std::cin >> a >> b;
a -= 1, b -= 1;
graph[a].push_back(b);
graph[b].push_back(a);
}
if (M != N - 1) {
std::cout << "NO\n";
return 0;
}
vector<int> adj(1 << N);
for (int set = 0; set < (1 << N); ++set) {
for (int i = 0; i < N; ++i) {
if (set >> i & 1) {
for (const int j : graph[i]) {
adj[set] |= 1 << j;
}
}
}
}
vector transit(1 << N, vector<int>(N));
for (int set = 0; set < (1 << N); ++set) {
for (int i = 0; i < N; ++i) {
// set -> adj[set] & ~(1 << i)
transit[adj[set] & ~(1 << i)][i] = set;
}
}
vector<int> dist(1 << N, -1), last(1 << N, -1);
std::queue<int> que;
const auto push = [&](const int u, const int d, const int l) {
if (dist[u] == -1) {
dist[u] = d;
last[u] = l;
que.push(u);
}
};
push(0, 0, -1);
while (!que.empty()) {
const int u = que.front();
que.pop();
for (int i = 0; i < N; ++i) {
push(transit[u][i], dist[u] + 1, i);
}
}
int set = (1 << N) - 1;
if (dist[set] == -1) {
std::cout << "NO\n";
return 0;
}
std::cout << "YES\n";
std::cout << dist[set] << '\n';
while (set != 0) {
std::cout << last[set] + 1;
set = adj[set] & ~(1 << last[set]);
std::cout << " \n"[set == 0];
}
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... |