# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
467334 | phathnv | Pastiri (COI20_pastiri) | C++11 | 0 ms | 0 KiB |
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>
#include "testlib.h"
using namespace std;
const int N = 500007;
int n, k, d[N], trace[N], numNode;
vector<int> adj[N], sheep, child[2 * N];
bool vst[N];
int main() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
sheep.assign(k, 0);
for (int &x : sheep)
cin >> x;
for (int i = 1; i <= n; i++)
d[i] = 1e9;
queue<int> qu;
for (int x : sheep) {
d[x] = 0;
trace[x] = ++numNode;
qu.push(x);
}
while (!qu.empty()) {
int u = qu.front();
qu.pop();
for (int v : adj[u]) {
if (d[v] > d[u] + 1) {
d[v] = d[u] + 1;
trace[v] = trace[u];
qu.push(v);
} else if (d[v] == d[u] + 1) {
int newNode = ++numNode;
child[newNode].push_back(trace[u]);
child[newNode].push_back(trace[v]);
trace[v] = newNode;
}
}
}
map<int, int> nodeToVex;
for (int i = 1; i <= n; i++)
nodeToVex[trace[i]] = i;
vector<int> answer;
for (int i = 1; i <= numNode; i++)
for (int v : child[i])
vst[v] = true;
for (int i = 1; i <= numNode; i++)
if (!vst[i])
answer.push_back(nodeToVex[i]);
cout << answer.size() << '\n';
for (int x : answer)
cout << x << ' ';
cout << '\n';
}