#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
vector<int> naive(const vector<vector<int>>& graph) {
const int N = graph.size();
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<vector<pair<int, int>>> transit(1 << N);
for (int set = 0; set < (1 << N); ++set) {
for (int i = 0; i < N; ++i) {
transit[adj[set] & ~(1 << i)].emplace_back(set, i);
}
}
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 (const auto& [v, i] : transit[u]) {
push(v, dist[u] + 1, i);
}
}
int set = (1 << N) - 1;
if (dist[set] == -1) {
return {};
}
vector<int> ans;
while (set != 0) {
ans.push_back(last[set] + 1);
set = adj[set] & ~(1 << last[set]);
}
return ans;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
if (M != N - 1) {
std::cout << "NO\n";
return 0;
}
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 (N <= 20) {
const auto ans = naive(graph);
const int n = ans.size();
std::cout << "YES\n" << n << '\n';
for (int i = 0; i < n; ++i) {
std::cout << ans[i] << " \n"[i + 1 == n];
}
} else {
std::cout << "YES\n";
if (N % 2 == 0) {
std::cout << 2 * (N - 2) << '\n';
for (int i = 2; i < N; ++i) {
std::cout << i << ' ';
}
for (int i = N - 1; i >= 2; --i) {
std::cout << i << " \n"[i == 2];
}
} else {
std::cout << 2 * (N - 2) << '\n';
for (int i = 2; i < N; ++i) {
std::cout << i << ' ';
}
for (int i = 2; i < N; ++i) {
std::cout << i << " \n"[i == N - 1];
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Incorrect |
1 ms |
460 KB |
Integer parameter [name=k] equals to 0, violates the range [1, 50] |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Incorrect |
1 ms |
460 KB |
Integer parameter [name=k] equals to 0, violates the range [1, 50] |
10 |
Halted |
0 ms |
0 KB |
- |