// #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 = 1e5 + 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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
21564 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
14000 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |