답안 #590933

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
590933 2022-07-06T14:57:27 Z cheissmart Newspapers (CEOI21_newspapers) C++14
0 / 100
1 ms 468 KB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
	cerr << to_string(h);
	if(sizeof ...(t)) cerr << ", ";
	DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif

const int INF = 1e9 + 7, N = 1003;

vi G[N];
int d[N], p[N];
bool onpath[N];

void dfs(int u, int pa = -1, int depth = 0) {
	d[u] = depth, p[u] = pa;
	for(int v:G[u]) if(v != pa)
		dfs(v, u, depth + 1);
}

int mx_depth(int u, int pa) {
	int mx = 0;
	for(int v:G[u]) if(v != pa)
		mx = max(mx, mx_depth(v, u) + 1);
	return mx;
}

signed main()
{
	IO_OP;

	int n, m;
	cin >> n >> m;

	if(m != n - 1) {
		cout << "NO" << '\n';
		return 0;
	}
	if(n == 2) {
		cout << "YES\n" << 2 << '\n' << "1 1\n";
		return 0;
	}

	for(int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		G[u].PB(v);
		G[v].PB(u);
	}

	dfs(1);
	int s = max_element(d + 1, d + n + 1) - d;
	dfs(s);
	int t = max_element(d + 1, d + n + 1) - d;

	vi path;
	while(SZ(path) == 0 || path.back() != s) {
		path.PB(t);
		onpath[t] = true;
		t = p[t];
	}
	assert(SZ(path) >= 3);
	vi ans;

	for(int i = 1; i < SZ(path) - 1; i++) {
		int u = path[i];
		vi aux;
		for(int v:G[u]) if(!onpath[v]) {
			int tt = mx_depth(v, u);
			if(tt > 1) {
				cout << "NO\n";
				return 0;
			}
			if(tt == 1)
				aux.PB(v);
		}
		if(aux.empty()) {
			ans.PB(u);
		} else {
			for(int v:aux)
				ans.PB(v), ans.PB(u);
		}
	}

	ans.PB(path[SZ(path) - 2]);

	cout << "YES\n";
	cout << SZ(ans) << '\n';
	for(int u:ans) cout << u << " ";
	cout << '\n';

}

# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -