#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = (int)(5e5) + 5;
vector<int> adj[MAXN];
int up[MAXN][21];
int timer = 0;
int tin[MAXN];
int tout[MAXN];
int S[MAXN];
int last[MAXN];
int lcm[MAXN];
int cnt[MAXN];
void root(int n, int p) {
tin[n] = timer++;
up[n][0] = p;
for (int k = 1; k <= 20; k++) {
up[n][k] = up[up[n][k-1]][k-1];
}
for (int e : adj[n]) {
if (e != p) {
root(e, n);
}
}
tout[n] = timer++;
}
vector<int> dfs(int n, int p) {
int paths = 0;
vector<int> arr(3, 0);
arr[2] = (int)(1e6);
arr[1] = (int)(1e6);
for (int e : adj[n]) {
if (e != p) {
vector<int> next = dfs(e, n);
vector<int> old = arr;
fill(arr.begin(), arr.end(), (int)(1e6));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i + j == 4) {
arr[2] = min(old[i] + next[j] + 1, arr[2]);
arr[0] = min(old[i] + next[j] + 2, arr[0]);
}
else if (i + j == 3) {
arr[1] = min(old[i] + next[j] + 1, arr[1]);
}
else if (i + j == 2) {
if (i == 2 || j ==2) {
arr[2] = min(arr[2], old[i] + next[j]);
}
else {
arr[2] = min(arr[2], old[i] + next[j]);
arr[0] = min(arr[0], old[i] + next[j] + 1);
}
}
else if (i + j == 1) {
arr[1] = min(arr[1], old[i] + next[j]);
}
else {
arr[0] = min(arr[0], old[i] + next[j]);
}
}
}
cnt[n] += cnt[e];
}
}
if (cnt[n] == 0) {
if (n != p) {
arr[1] = min(arr[0], arr[1]);
arr[0] = (int)(1e6);
}
}
return arr;
}
bool is_ancestor(int a, int b) {
return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int a, int b) {
if (is_ancestor(a, b)) {
return a;
}
if (is_ancestor(b, a)) {
return b;
}
int u = a;
for (int k = 20; k >= 0; k--) {
if (!is_ancestor(up[u][k], b)) {
u = up[u][k];
}
}
return up[u][0];
}
int main() {
string asdf = "input.in";
int N, s;
cin >> N >> s;
for (int i = 0; i < N-1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> df;
for (int i = 0; i < N; i++) {
df.push_back(i);
last[i] = -1;
cin >> S[i];
S[i]--;
}
root(0, 0);
sort(df.begin(), df.end(), [](const int& lhs, const int& rhs) {
return tin[lhs] < tin[rhs];
}
);
for (int i : df) {
if (last[S[i]] == -1) {
last[S[i]] = i;
lcm[S[i]] = i;
}
else {
if (is_ancestor(lcm[S[i]], i)) {
cnt[i]++;
cnt[lcm[S[i]]]--;
}
else {
int lc = lca(lcm[S[i]], i);
cnt[lcm[S[i]]]++;
cnt[lc]-=2;
cnt[i]++;
lcm[S[i]] = lc;
}
}
}
vector<int> ans = dfs(0, 0);
cout << min(ans[0], min(ans[1], ans[2])+1);
return 0;
}
Compilation message
mergers.cpp: In function 'std::vector<int> dfs(int, int)':
mergers.cpp:29:9: warning: unused variable 'paths' [-Wunused-variable]
29 | int paths = 0;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12160 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12012 KB |
Output is correct |
16 |
Incorrect |
8 ms |
12140 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12160 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12012 KB |
Output is correct |
16 |
Incorrect |
8 ms |
12140 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12160 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12012 KB |
Output is correct |
16 |
Incorrect |
8 ms |
12140 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
26340 KB |
Output is correct |
2 |
Incorrect |
165 ms |
26340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12160 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12012 KB |
Output is correct |
16 |
Incorrect |
8 ms |
12140 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |