比赛 20241022 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 猴猴吃苹果 最终得分 100
用户昵称 ┭┮﹏┭┮ 运行时间 0.458 s
代码语言 C++ 内存使用 6.23 MiB
提交时间 2024-10-22 09:59:57
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 5e4+10;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}


int n,rt;
vector<int>e[N];
int d[N],dep[N],fa[N],son[N],down[N];

struct node{
	int x,id;
	bool operator < (const node a)const{return x == a.x ? id < a.id : x > a.x;}
}a[N];
set<node>s;
bool v[N];

void dfs(int x){
	for(int y : e[x]){
		if(y == fa[x])continue;
		fa[y] = x,dep[y] = dep[x] + 1;
		dfs(y);
		if(!son[x])son[x] = y;
		else if(d[y] > d[son[x]])son[x] = y;
		else if(d[y] == d[son[x]] && down[y] < down[son[x]])son[x] = y;
	}
	d[x] = d[son[x]] + 1;
	if(son[x])down[x] = down[son[x]];
	else down[x] = x;
	s.insert({d[x],down[x]});
}

int main(){
	freopen("apple.in","r",stdin);
	freopen("apple.out","w",stdout);
	n = read(),rt = read();
	for(int i = 1;i < n;i++){
		int x = read(),y = read();
		e[x].pb(y),e[y].pb(x);
	}
	dep[rt] = 1,dfs(rt);
	printf("%d\n",rt);
	while(s.size()){
		int x = (*s.begin()).id;
		printf("%d\n",x);
		while(x){
			if(v[x])break;
			v[x] = 1;
			s.erase({d[x],down[x]});
			x = fa[x];
		}
	}
	

	return 0;

}