Submission #769193

# Submission time Handle Problem Language Result Execution time Memory
769193 2023-06-29T09:41:40 Z RushB Newspapers (CEOI21_newspapers) C++17
0 / 100
1 ms 340 KB
#include "bits/stdc++.h"
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long INF = 1ll << 60;
const int N = 1000 + 5;
vector<int> adj[N], V[N], order;
bitset<N> leaf;
 
void dfs(int v, int p) {
	V[v] = {0};
	for (auto u: adj[v]) if (u != p) {
		dfs(u, v);
		V[v].push_back(V[u][0] + 1);
	}
	sort(V[v].rbegin(), V[v].rend());
}

void dfs2(int v, int p) {
	order.push_back(v);
	for (auto u: adj[v]) if (u != p && adj[u].size() == 1)
		order.push_back(u);
	for (auto u: adj[v]) if (u != p && adj[u].size() > 1)
		dfs(u, v);
}
 
signed main() {
	ios::sync_with_stdio(0), cin.tie(0);
	int n, m;
	cin >> n >> m;
	if (m >= n) {
		cout << "NO\n";
		return 0;
	}
	if (n <= 2) {
		cout << "YES\n";
		cout << n << '\n';
		FOR(i, 0, n) cout << 1 << ' ';
		return 0;
	}
	FOR(i, 1, n) {
		int u, v; cin >> u >> v;	
		u--, v--;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	FOR(i, 0, n) leaf[i] = adj[i].size() == 1;
	FOR(i, 0, n) if (!leaf[i]) {
		vector<int> t;
		for (auto u: adj[i]) if (!leaf[u]) t.push_back(u);
		adj[i] = t;
	}
	//is caterpillar?
	FOR(i, 0, n) {
		FOR(j, 0, n) V[j].clear();
		dfs(i, i);
		if (V[i].size() >= 3 && V[i][2] >= 2) {
			cout << "NO\n";
			return 0;
		}
	}
	int rt = -1;
	FOR(i, 0, n) if (!leaf[i] && adj[i].size() <= 1) rt = i;

	dfs2(rt, rt);
	cout << order.size() * 2 << '\n';
	for (auto u: order) cout << u + 1 << ' ';
	reverse(order.begin(), order.end());
	for (auto u: order) cout << u + 1 << ' ';
	cout << '\n';
	return 0;
}
//Reading this fills you with ....
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 0 ms 340 KB Token "2" doesn't correspond to pattern "YES|NO"
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 0 ms 340 KB Token "2" doesn't correspond to pattern "YES|NO"
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 0 ms 340 KB Token "2" doesn't correspond to pattern "YES|NO"
4 Halted 0 ms 0 KB -