#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 ok = 0;
bool bio[mxN];
void dfs(int v, int p, int duz){
if(duz == 0){
if(mark[v] && !bio[v]){
ok = 1;
bio[v] = 1;
}
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);
vector<int> res;
ff(i,1,n){
int u = id[i];
int br = 0;
for(auto c : g[u])br += (d[c] == d[u] - 1);
if(br == 1)continue;
ok = 0; dfs(u, -1, d[u]);
if(ok == 1){
res.pb(u);
}
}
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
42600 KB |
Output is correct |
2 |
Incorrect |
145 ms |
43248 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
13144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
12888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
293 ms |
43624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |