// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,popcnt")
#include <bits/stdc++.h>
#define lli long long int
#define ld long double
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define MP make_pair
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N = 5e5 + 5;
const int INF = 1e9 + 500;
const int MOD = 1e9 + 7;
vector<vector<int> > adj(N, vector<int>()), g(N, vector<int>());
vector<int> she;
vector<int> dist(N, -1), dep(N, 0);
vector<int> p(N, 0);
vector<int> vis(N, 0), a(N, 0);
void bfs() {
queue<int> qu;
for(auto c : she) {
dist[c] = 0;
qu.push(c);
}
while(qu.size()) {
auto cur = qu.front();
qu.pop();
for(auto c : adj[cur]) {
if(dist[c] != -1) continue;
dist[c] = dist[cur] + 1;
qu.push(c);
}
}
}
void dfs(int node, int par) {
for(auto c : adj[node]) {
if(c == par) continue;
dep[c] = dep[node] + 1;
p[c] = node;
dfs(c, node);
}
}
void dfs2(int node) {
vis[node] = 1;
for(auto c : g[node]) {
if(vis[c]) continue;
dfs2(c);
}
}
void solve() {
int n, k;
cin >> n >> k;
REP(i, n - 1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
she.resize(k);
REP(i, k) {
cin >> she[i];
}
dfs(1, 0);
bfs();
for(auto c : she) {
int last = c;
while(last != 1 && dist[last] + dep[last] < dist[p[last] + dep[p[last]]]) {
last = p[last];
}
a[c] = last;
}
for(int i = 1; i <= n; i++) {
for(int c : adj[i]) {
if(dist[c] == dist[i] + 1) {
g[c].pb(i);
}
}
}
sort(all(she), [](int x, int y) {
return dep[x] > dep[y];
});
vector<int> res;
for(auto c : she) {
if(vis[c]) continue;
res.pb(a[c]);
dfs2(a[c]);
}
cout << res.size() << "\n";
for(auto c : res) {
cout << c << " ";
}
cout << "\n";
}
signed main() {
fastio();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
90768 KB |
Output is correct |
2 |
Incorrect |
172 ms |
95076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
33880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
33628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
576 ms |
68964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |