#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 5e5 + 5;
vector<int> g[nmax];
int dist[nmax];
int taken[nmax];
vector<int> rez;
namespace Tree {
int p[nmax], jump[nmax], d[nmax];
namespace DS {
void fill(int node, int f, int D) {
//if(taken[node]) return;
taken[node] = 1;
if(D == 0) return;
for(auto x : g[node]) if(x != f) fill(x, node, D - 1);
}
void toggle() {
return;
}
}
void init(int node, int f) {
d[node] = d[f] + 1;
p[node] = jump[node] = f;
if(d[jump[jump[f]]] + d[f] == 2 * d[jump[f]]) jump[node] = jump[jump[f]];
for(auto x : g[node])
if(x != f)
init(x, node);
return;
}
void fill(int node) {
int targetfather = node;
auto isgood = [&](int x) {
return dist[x] == d[node] - d[x];
};
while(isgood(p[targetfather]) && targetfather != 1) {
if(isgood(jump[targetfather])) targetfather = jump[targetfather];
if(isgood(p[targetfather])) targetfather = p[targetfather];
}
//if(!taken[targetfather]) {
rez.emplace_back(targetfather);
DS::fill(targetfather, targetfather, dist[targetfather]);
//}
return;
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, k;
cin >> n >> k;
for(int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
Tree::init(1, 1);
for(int i = 1; i <= n; i++) dist[i] = n + 5;
vector<int> v(k);
queue<int> que;
for(auto &x : v) cin >> x, que.emplace(x), dist[x] = 0;
while(!que.empty()) {
int node = que.front();
que.pop();
for(auto x : g[node])
if(dist[x] == n + 5) dist[x] = dist[node] + 1, que.emplace(x);
}
//cerr << "AA\n";
sort(all(v), [&](int a, int b) { return Tree::d[a] > Tree::d[b]; });
for(auto x : v) {
if(taken[x]) continue;
Tree::fill(x);
}
cout << sz(rez) << '\n';
for(auto x : rez)
cout << x << ' ';
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
61056 KB |
Output is correct |
2 |
Correct |
150 ms |
61100 KB |
Output is correct |
3 |
Correct |
143 ms |
61008 KB |
Output is correct |
4 |
Correct |
222 ms |
65828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21852 KB |
Output is correct |
2 |
Correct |
5 ms |
21852 KB |
Output is correct |
3 |
Correct |
295 ms |
38040 KB |
Output is correct |
4 |
Correct |
296 ms |
40488 KB |
Output is correct |
5 |
Correct |
426 ms |
37680 KB |
Output is correct |
6 |
Execution timed out |
1051 ms |
39508 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21848 KB |
Output is correct |
2 |
Correct |
5 ms |
21852 KB |
Output is correct |
3 |
Correct |
5 ms |
21852 KB |
Output is correct |
4 |
Correct |
5 ms |
21852 KB |
Output is correct |
5 |
Correct |
5 ms |
22104 KB |
Output is correct |
6 |
Correct |
5 ms |
21852 KB |
Output is correct |
7 |
Correct |
5 ms |
21852 KB |
Output is correct |
8 |
Correct |
5 ms |
21852 KB |
Output is correct |
9 |
Correct |
5 ms |
21680 KB |
Output is correct |
10 |
Correct |
5 ms |
21852 KB |
Output is correct |
11 |
Correct |
5 ms |
21852 KB |
Output is correct |
12 |
Correct |
4 ms |
21852 KB |
Output is correct |
13 |
Correct |
5 ms |
21852 KB |
Output is correct |
14 |
Correct |
5 ms |
21852 KB |
Output is correct |
15 |
Correct |
5 ms |
21852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
38592 KB |
Output is correct |
2 |
Correct |
379 ms |
38320 KB |
Output is correct |
3 |
Correct |
405 ms |
40872 KB |
Output is correct |
4 |
Correct |
398 ms |
37608 KB |
Output is correct |
5 |
Correct |
271 ms |
37576 KB |
Output is correct |
6 |
Correct |
367 ms |
43788 KB |
Output is correct |
7 |
Correct |
422 ms |
42580 KB |
Output is correct |
8 |
Correct |
422 ms |
42320 KB |
Output is correct |
9 |
Execution timed out |
1054 ms |
37460 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |