#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int maxn = 5e5+10;
const int inf = 1e18+10;
int n, k, down[maxn], dist[maxn];
vector<int> g[maxn];
set<int> st[maxn];
int ans;
vector<int> ansv;
int lazy[maxn];
void dfs(int u, int ant) {
down[u] = -1;
if(dist[u] == 0) down[u] = 0;
vector<pair<int,int>> ord;
for(auto v : g[u]) if(v != ant) {
dfs(v,u);
ord.pb(mp(down[v],v));
if(st[v].size() < st[u].size()) {
for(auto x : st[v]) st[u].insert(x-1+lazy[v]-lazy[u]);
}
else {
lazy[v]--;
for(auto x : st[u]) st[v].insert(x-lazy[v]+lazy[u]);
lazy[u] = 0;
swap(st[u],st[v]);
swap(lazy[u],lazy[v]);
}
}
sort(all(ord),greater<pair<int,int>>());
for(auto V : ord) {
int v = V.sc;
if(down[v] != -1 && st[u].count(down[v]+1-lazy[u]) == 0) {
if(dist[u] != down[v]+1) {
// cout << " coloca em " << v << endl;
ansv.pb(v);
ans++;
st[u].insert(dist[v]-1-lazy[u]);
down[v] = -1;
}
else {
down[u] = down[v]+1;
}
}
}
if(down[u] != -1) {
if(st[u].count(0-lazy[u])) {
down[u] = -1;
}
}
// cout << u << " " << down[u] << " " << dist[u] << " " << ans << " " << st[u].count(1) << endl;
}
int32_t main() {
// #ifndef ONLINE_JUDGE
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
// #endif
cin >> n >> k;
for(int i = 1; i <= n-1; i++) {
int u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
queue<int> q;
for(int i = 1; i <= n; i++) {
dist[i] = inf;
}
for(int i = 0; i < k; i++) {
int x;
cin >> x;
dist[x] = 0;
q.push(x);
}
while(q.size()) {
int u = q.front();
q.pop();
for(auto v : g[u]) {
if(dist[v] == inf) {
q.push(v);
dist[v] = dist[u]+1;
}
}
}
dfs(1,0);
if(down[1] != -1) ansv.pb(1);
cout << ansv.size() << endl;
for(auto x : ansv) cout << x << " ";
cout << endl;
// cout << ans + (down[1] != -1) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
156764 KB |
Output is correct |
2 |
Correct |
287 ms |
163416 KB |
Output is correct |
3 |
Correct |
301 ms |
163664 KB |
Output is correct |
4 |
Correct |
437 ms |
179804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35928 KB |
Output is correct |
2 |
Correct |
14 ms |
35932 KB |
Output is correct |
3 |
Correct |
398 ms |
72676 KB |
Output is correct |
4 |
Incorrect |
412 ms |
77612 KB |
Sheep 99854 not protected |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
35672 KB |
Sheep 208 not protected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
449 ms |
67976 KB |
Sheep 680 not protected |
2 |
Halted |
0 ms |
0 KB |
- |