This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 500101
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
vector<int> adj[MAX];
int dis[MAX];
int sheep[MAX];
vector<int> v;
int vis[MAX];
int dep[MAX];
int prt[MAX];
void dfs(int x, int p = 0) {
prt[x] = p;
for (auto v : adj[x]) {
if (v == p) continue;
dep[v] = dep[x] + 1;
dfs(v, x);
}
}
void down(int x, int p = 0) {
for (auto v : adj[x]) if (v != p) {
dis[v] = min(dis[v], dis[x] + 1);
down(v, x);
}
}
void chk(int x, int d, int p = 0) {
sheep[x] = 0;
if (!d) return;
for (auto v : adj[x]) if (v != p) chk(v, d - 1, x);
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N, K;
cin >> N >> K;
int i, a, b;
for (i = 1; i < N; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> sv;
for (i = 1; i <= K; i++) {
cin >> a;
sv.push_back(a);
sheep[a] = 1;
}
dfs(1);
down(1);
sort(sv.begin(), sv.end(), [&](int a, int b) {return dep[a] > dep[b]; });
int cnt = 0;
vector<int> ans;
for (i = 1; i <= N; i++) vis[i] = 0;
for (auto x : sv) {
if (!sheep[x]) continue;
cnt++;
int v = x;
while (1) {
int p = prt[v];
if (!p) break;
if (dis[p] < dep[x] - dep[p]) break;
v = p;
//if (vis[v]) break;
}
ans.push_back(v);
chk(v, dis[v]);
}
cout << ans.size() << ln;
for (auto v : ans) cout << v << bb;
}
# | 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... |