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>
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], ans[1]+1);
return 0;
}
Compilation message (stderr)
mergers.cpp: In function 'std::vector<int> dfs(int, int)':
mergers.cpp:29:9: warning: unused variable 'paths' [-Wunused-variable]
29 | int paths = 0;
| ^~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |