답안 #622387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
622387 2022-08-04T08:34:30 Z aworue Newspapers (CEOI21_newspapers) C++17
8 / 100
1 ms 468 KB
#include <bits/stdc++.h>
using ll = long long;
#define int ll
using namespace std;

#define sz(x) (int)(x).size()
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define mod 998244353

#define db(x) cerr << __LINE__ << " " << #x << " " << x << "\n"
using vi = vector<int>;
using pi = pair<int, int>;

const int N = 1005;
const int inf = 3e18;

int n, m, par[N], used[N], ok[N];
vi adj[N], ret;

pi dfs(int x, int p = -1){
	pi res(0, x);
	for(auto i : adj[x]){
		if(i == p) continue;
		par[i] = x;
		auto j = dfs(i, x);
		j.fi++;
		res = max(res, j);
	}
	return res;
}

void solve(){
	cin >> n >> m;
	if(n == 1){
		cout << "YES\n1\n1"; return;
	}
	if(n == 2){
		cout << "YES\n2\n1 1"; return;
	}
	if(m != n - 1){
		cout << "NO"; return;
	}
	foru(i, 1, m){
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	int o = dfs(1).se;
	int z = dfs(o).se;
	for(int i = z; ; i = par[i]){
		ok[i] = 1; 
		used[i] = 1;
		for(auto j : adj[i]){
			ok[j] = 1;
			for(auto k : adj[j]) ok[k] = 1;
		}
		if(i == o) break;
	}
	for(auto i : ok) if(i == 0){
		break;
		cout << "NO"; return;
	}
	for(int i = par[z]; i != o; i = par[i]){
		ret.push_back(i);
		for(auto j : adj[i]){
			if(sz(adj[j]) == 1 or used[j]) continue;
			ret.push_back(j);
			ret.push_back(i);
		}
	}
	cout << "YES\n" << (sz(ret) << 1) << "\n";
	foru(i, 0, sz(ret) - 1) cout << ret[i] << " ";
	ford(i, sz(ret) - 1, 0) cout << ret[i] << " ";
}

signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t = 1; // cin >> t;
	while(t--){
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Incorrect 1 ms 340 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Incorrect 1 ms 340 KB Output isn't correct
10 Halted 0 ms 0 KB -