#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 500005;
int n, k;
vector<int> g[mxN];
bool mark[mxN];
int d[mxN];
int id[mxN];
bool cmp(int i, int j){
return d[i] > d[j];
}
bool los[mxN];
bool dbr[mxN];
bool dodat[mxN];
vector<int> vec[mxN];
vector<int> svi[mxN];
int r = 0;
void dfs(int v, int p, int duz){
if(duz == 0){
if(mark[v]){
vec[v].pb(r);
svi[r].pb(v);
}
return;
}
for(auto u : g[v]){
if(u != p){
dfs(u, v, duz - 1);
}
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
vector<pii> edge;
ff(i,1,n - 1){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
edge.pb({u, v});
}
ff(i,1,k){
int X;
cin >> X;
mark[X] = 1;
}
ff(i,1,n)d[i] = inf;
queue<int> que;
ff(i,1,n){
if(mark[i] == 1){
que.push(i);
d[i] = 0;
}
}
while(sz(que)){
int v = que.front(); que.pop();
for(auto u : g[v]){
if(d[u] == inf){
d[u] = d[v] + 1;
que.push(u);
}
}
}
ff(i,1,n)id[i] = i;
sort(id + 1, id + n + 1, cmp);
ff(i,1,n){
int u = id[i];
int br = 0; bool ok = 0;
for(auto c : g[u]){
if(!los[c]){
br += (d[c] == d[u] - 1);
ok |= (d[c] == d[u] + 1);
}
}
if(br == 1 || ok == 1){
los[u] = 1;
continue;
}
r = u; dfs(u, -1, d[u]);
}
vector<int> res;
ff(i,1,n){
if(mark[i] && sz(vec[i]) == 1){
int X = vec[i][0];
if(dodat[X] == 0){
for(auto c : svi[X])dbr[c] = 1;
res.pb(X); dodat[X] = 1;
}
}
}
ff(i,1,n){
if(mark[i] && !dbr[i]){
int X = vec[i][0];
if(dodat[X] == 0){
for(auto c : svi[X])dbr[c] = 1;
res.pb(X); dodat[X] = 1;
}
}
}
cout << sz(res) << '\n';
for(auto c : res)cout << c << " ";
cout << '\n';
return 0;
}
/*
20 9
1 2
2 3
2 4
4 5
4 6
5 7
7 8
8 9
7 10
10 11
6 12
6 13
6 17
13 14
14 15
14 16
17 18
18 19
18 20
1 3 9 11 12 15 16 19 20
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
66228 KB |
Output is correct |
2 |
Incorrect |
172 ms |
67776 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
35936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
35676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
322 ms |
68744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |