#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int N=5e5+1;
vec<int> gr[N];
bool used[N];
vec<int> g[N];
int d[N],h[N];
vec<int> have[N],cand[N];
int up[N];
void dfs(int v,int p,pii mx){
// if(d[v])
// cerr<<"WI "<<d[v]+h[v]<<' '<<v<<' '<<up[v]<<endl;
if(mx.f<d[v]+h[v])
mx={d[v]+h[v],v};
up[v]=mx.s;
// if(d[v])
// cerr<<"WI "<<d[v]+h[v]<<' '<<v<<' '<<up[v]<<endl;
for(auto &z : g[v]){
if(z==p) continue;
h[z]=h[v]+1;
dfs(z,v,mx);
}
}
void dfs1(int v){
if(used[v])
return;
used[v]=1;
for(auto &z : gr[v])
dfs1(z);
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
fill(d,d+N,1e9);
// ifstream cin("input.txt");
int n,k;
cin>>n>>k;
for(int i=1;i<n;i++){
int v,u;
cin>>v>>u;
g[v].pb(u);g[u].pb(v);
}
queue<int>q;
vec<int> kek;
for(int i=0;i<k;i++){
int v;
cin>>v;
d[v]=0;
q.push(v);
kek.pb(v);
}
// vec<int> vc;
while(!q.empty()){
int v=q.front();q.pop();
for(auto &z : g[v]){
if(d[z]==1e9){
d[z]=d[v]+1;
q.push(z);
}
}
}
dfs(1,1,{-1,-1});
for(auto &z : kek){
// cerr<<"Z "<<z<<' '<<up[z]<<' '<<d[up[z]]<<endl;
cand[h[z]].pb(z);
}
for(int i=1;i<=n;i++){
for(auto &z : g[i]){
if(d[i]==d[z]+1)
gr[i].pb(z);
}
// have[d[i]].pb(i);
}
vec<int> ans;
// vecE<
for(int i=n-1;i>=0;i--){
for(auto &z : cand[i]){
// assert(d[z]==i);
if(!used[z]){
used[z]=1;
ans.pb(up[z]);
dfs1(up[z]);
// cout<<"SOME "<<z<<endl;
}
}
// for(auto &z : have[i]){
// if(used[z]){
// for(auto &u : gr[z])
// used[u]=1;
// }
//// for(auto &u : gr[z])
//// used[u]=1;
// }
}
cout<<sz(ans)<<endl;
for(auto &z : ans)
cout<<z<<' ';
return 0;
}
/*
857 20 648
3527 22 4748
4327 20 2203
3269 26 2435
4257 22 305
3325 20 4495
3689 18 4550
3030 24 2084
1980 20 3373
4251 24 1852
856 26 3184
4923 22 3812
4793 24 4786
4128 14 1
4463 18 3461
16 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
123564 KB |
Output is correct |
2 |
Correct |
259 ms |
124156 KB |
Output is correct |
3 |
Correct |
236 ms |
124052 KB |
Output is correct |
4 |
Correct |
356 ms |
128720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
49620 KB |
Output is correct |
2 |
Correct |
27 ms |
49624 KB |
Output is correct |
3 |
Correct |
531 ms |
84892 KB |
Output is correct |
4 |
Correct |
504 ms |
86652 KB |
Output is correct |
5 |
Correct |
647 ms |
85060 KB |
Output is correct |
6 |
Correct |
744 ms |
88424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
49356 KB |
Output is correct |
2 |
Correct |
27 ms |
49308 KB |
Output is correct |
3 |
Correct |
28 ms |
49316 KB |
Output is correct |
4 |
Correct |
28 ms |
49372 KB |
Output is correct |
5 |
Correct |
29 ms |
49364 KB |
Output is correct |
6 |
Correct |
28 ms |
49380 KB |
Output is correct |
7 |
Correct |
26 ms |
49292 KB |
Output is correct |
8 |
Correct |
27 ms |
49332 KB |
Output is correct |
9 |
Correct |
27 ms |
49324 KB |
Output is correct |
10 |
Correct |
28 ms |
49300 KB |
Output is correct |
11 |
Correct |
26 ms |
49356 KB |
Output is correct |
12 |
Correct |
27 ms |
49248 KB |
Output is correct |
13 |
Correct |
27 ms |
49492 KB |
Output is correct |
14 |
Correct |
27 ms |
49356 KB |
Output is correct |
15 |
Correct |
27 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
608 ms |
85772 KB |
Output is correct |
2 |
Correct |
613 ms |
85744 KB |
Output is correct |
3 |
Correct |
580 ms |
82148 KB |
Output is correct |
4 |
Correct |
641 ms |
84816 KB |
Output is correct |
5 |
Correct |
482 ms |
75708 KB |
Output is correct |
6 |
Correct |
581 ms |
85560 KB |
Output is correct |
7 |
Correct |
612 ms |
85344 KB |
Output is correct |
8 |
Correct |
628 ms |
85948 KB |
Output is correct |
9 |
Correct |
740 ms |
85068 KB |
Output is correct |
10 |
Correct |
625 ms |
85116 KB |
Output is correct |
11 |
Correct |
467 ms |
85964 KB |
Output is correct |
12 |
Correct |
390 ms |
84064 KB |
Output is correct |
13 |
Correct |
421 ms |
82948 KB |
Output is correct |
14 |
Correct |
414 ms |
87700 KB |
Output is correct |
15 |
Correct |
73 ms |
54756 KB |
Output is correct |
16 |
Correct |
698 ms |
81784 KB |
Output is correct |
17 |
Correct |
440 ms |
75840 KB |
Output is correct |
18 |
Correct |
584 ms |
78400 KB |
Output is correct |
19 |
Correct |
616 ms |
89616 KB |
Output is correct |
20 |
Correct |
646 ms |
95748 KB |
Output is correct |
21 |
Correct |
577 ms |
85044 KB |
Output is correct |
22 |
Correct |
587 ms |
85836 KB |
Output is correct |