#include <bits/stdc++.h>
/*
--> Author: Kazuki_Hoshino__8703 <--
I love Nanasaki Ai ☆*: .。. o(≧▽≦)o .。.:*☆
*/
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
const int mn = 5e5 + 5, bm = (1 << 15) + 1, mod = 1e9;
const int inf = 1e9 + 7;
int n, k, d[mn], par[mn];
vector <int> a[mn];
void dfs(int u, int p){
for(auto v : a[u]){
if(v == p) continue;
d[v] = d[u] + 1;
par[v] = u;
dfs(v, u);
}
}
vector <int> ilu;
int d1[mn], ok[mn];
vector <int> pre[mn], near[mn];
void bfs(){
for(int i = 1; i <= n; i++) d1[i] = inf;
queue <pii> q;
for(auto i : ilu){
d1[i] = 0;
q.push({i, i});
}
while(q.size()){
int u = q.front().fi;
int p = q.front().se;
q.pop();
for(auto v : a[u]){
if(d1[v] > d1[u] + 1){
d1[v] = d1[u] + 1;
q.push({v, p});
}
}
}
}
void spread(int x){
queue <int> q;
q.push(x);
while(q.size()){
int u = q.front();
q.pop();
for(auto v : a[u]){
if(ok[v] || d1[v] != d1[u] - 1) continue;
ok[v] = 1;
q.push(v);
}
}
}
bool cmp(int a, int b){
return d[a] > d[b];
}
void solve(){
cin >> n >> k;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
dfs(1, -1);
for(int i = 1; i <= k; i++){
int x; cin >> x;
ilu.push_back(x);
}
bfs();
sort(ilu.begin(), ilu.end(), cmp);
vector <int> res;
for(auto i : ilu){
if(ok[i]) continue;
int h = d[i];
int target = i;
while(par[target] != 0 && d1[par[target]] == d[i] - d[par[target]]){
target = par[target];
}
res.push_back(target);
spread(target);
}
cout << res.size() << '\n';
for(auto i : res) cout << i << ' ';
}
main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while(t --){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
pastiri.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
98 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |