#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}
size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#endif
typedef long long ll;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void solve() {
//ifstream cin("nazwa.in");
//ofstream cout("nazwa.out");
int n, k;
cin >> n >> k;
vector<vi>g(n + 1);
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].eb(b);
g[b].eb(a);
}
vi dep(n + 1, 0);
int lg = 0;
while((1 << (lg + 1)) <= n - 1) {
lg++;
}
vector<vi>nxt(n + 1, vi(lg + 1, 0));
auto calc = [&](auto &&calc, int v, int o) -> void {
nxt[v][0] = o;
for(int i = 1; i <= lg; i++) {
nxt[v][i] = nxt[nxt[v][i - 1]][i - 1];
}
for(auto x : g[v]) {
if(x != o) {
dep[x] = dep[v] + 1;
calc(calc, x, v);
}
};
};
calc(calc, 1, 1);
vector<pi>kt;
for(int i = 1; i <= k; i++) {
int x;
cin >> x;
kt.eb(dep[x], x);
}
sort(all(kt));
reverse(all(kt));
vi dist(n + 1, 1e9);
queue<int>q;
for(auto [D, v] : kt) {
dist[v] = 0;
q.emplace(v);
}
while(!q.empty()) {
int v = q.front();
q.pop();
for(auto x : g[v]) {
if(dist[x] == 1e9) {
dist[x] = dist[v] + 1;
q.emplace(x);
}
}
}
vector<vi>g2(n + 1);
for(int i = 1; i <= n; i++) {
for(auto x : g[i]) {
if(dist[x] == dist[i] - 1) {
g2[i].eb(x);
}
}
}
vi vis(n + 1, 0);
auto mark = [&](auto &&mark, int v) -> void {
vis[v] = 1;
for(auto x : g2[v]) {
if(!vis[x]) {
mark(mark, x);
}
}
};
vi res;
for(auto [D, v] : kt) {
if(vis[v]) {
continue;
}
int sum = 0;
for(int i = lg; i >= 0; i--) {
if(dist[nxt[v][i]] == sum + (1 << i)) {
sum += (1 << i);
v = nxt[v][i];
}
}
res.eb(v);
mark(mark, v);
}
sort(all(res));
cout << sz(res) << '\n';
for(auto x : res) {
cout << x << ' ';
}
cout << '\n';
}
int main() {
fastio();
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
# | 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... |