이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 25;
vector <int> adj[MAXN];
int n, m;
int dfs (int pos, int par, int dep = 0) {
if (dep == 3) {
return 1;
}
int cnt = 0;
for (auto j : adj[pos]) if (j != par) cnt += dfs(j, pos, dep + 1);
if (!dep) return cnt;
return cnt >= 1;
}
int dist[1 << 21];
pair <int, int> par[1 << 21];
queue <int> cur;
const int inf = 1e9;
int adjmask[1 << 21];
int main () {
cin >> n >> m;
bool flag = 1;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
flag &= a + 1 == b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if (m != n - 1) {
cout << "NO\n";
return 0;
}
if (flag) {
cout << "YES\n";
if (n == 1) {
cout << "1\n1\n";
return 0;
}
if (n == 2) {
cout << "2\n2 2\n";
return 0;
}
cout << 2 * (n - 2) << '\n';
for (int i = 2; i < n; i++) cout << i << " ";
for (int i = n - 1; i >= 2; i--) cout << i << " ";
cout << '\n';
return 0;
}
if (n <= 20) {
for (int i = 1; i <= n; i++) {
for (auto j : adj[i]) {
adjmask[i] += 1 << j;
}
}
for (auto &i : dist) i = inf;
dist[(1 << (n + 1)) - 2] = 0;
cur.push((1 << (n + 1)) - 2);
par[(1 << (n + 1)) - 2].first = -1;
while (!cur.empty()) {
auto k = cur.front();
cur.pop();
vector <int> t;
for (int i = 1; i <= n; i++) {
if (k & (1 << i)) {
t.push_back(i);
}
}
for (auto i : t) {
int newmask = 0;
for (auto j : t) {
if (j == i) continue;
newmask |= adjmask[j];
}
if (dist[newmask] == inf) {
dist[newmask] = 1 + dist[k];
par[newmask].first = k;
par[newmask].second = i;
cur.push(newmask);
}
}
}
if (dist[0] == inf) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
vector <int> ret;
int mask = 0;
while (mask != (1 << (n + 1)) - 2) {
ret.push_back(par[mask].second);
mask = par[mask].first;
}
reverse(ret.begin(), ret.end());
cout << (int)ret.size() << '\n';
for (auto i : ret) cout << i << " ";
cout << '\n';
return 0;
}
for (int i = 1; i <= n; i++) {
if (dfs(i, -1) >= 3) {
cout << "NO\n"; return 0;
}
}
cout << "YES\n";
cout << "1\n1\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |