#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) {
if (sheep[x]) dis[x] = 0;
else dis[x] = 10101010;
prt[x] = p;
for (auto v : adj[x]) {
if (v == p) continue;
dep[v] = dep[x] + 1;
dfs(v, x);
dis[x] = min(dis[x], dis[v] + 1);
}
}
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 |
1 |
Correct |
127 ms |
74956 KB |
Output is correct |
2 |
Correct |
145 ms |
76360 KB |
Output is correct |
3 |
Correct |
137 ms |
76452 KB |
Output is correct |
4 |
Correct |
215 ms |
81304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12244 KB |
Output is correct |
2 |
Correct |
7 ms |
12244 KB |
Output is correct |
3 |
Correct |
323 ms |
37412 KB |
Output is correct |
4 |
Correct |
316 ms |
46412 KB |
Output is correct |
5 |
Correct |
497 ms |
44216 KB |
Output is correct |
6 |
Execution timed out |
1073 ms |
47564 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12244 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12152 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12244 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12096 KB |
Output is correct |
14 |
Correct |
6 ms |
12148 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
406 ms |
38488 KB |
Output is correct |
2 |
Correct |
429 ms |
45008 KB |
Output is correct |
3 |
Correct |
403 ms |
47876 KB |
Output is correct |
4 |
Correct |
436 ms |
44248 KB |
Output is correct |
5 |
Correct |
318 ms |
41800 KB |
Output is correct |
6 |
Correct |
400 ms |
52952 KB |
Output is correct |
7 |
Correct |
487 ms |
51168 KB |
Output is correct |
8 |
Correct |
493 ms |
50684 KB |
Output is correct |
9 |
Execution timed out |
1090 ms |
44228 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |