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 "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 1e5 + 7;
const int INF = (int) 1e9 + 7;
int n;
int par[N];
int dp1[N];
int dp2[N];
int cost1[N];
int cost2[N];
vector<int> g[N];
void build(int a, int p = 0) {
par[a] = p;
vector<int> kids;
for (auto &b : g[a]) {
if (b != p) {
kids.push_back(b);
build(b, a);
}
}
g[a] = kids;
}
void dfs(int a) {
dp1[a] = 0;
dp2[a] = 0;
for (auto &b : g[a]) {
dfs(b);
dp1[a] += min(dp1[b], 1 + dp2[b]);
dp2[a] += min(dp2[b], 1 + dp1[b]);
}
dp1[a] += cost1[a];
dp2[a] += cost2[a];
}
void initialize(int nn, std::vector<int> edgesA, std::vector<int> edgesB) {
n = nn;
for (int i = 0; i < n - 1; i++) {
int a = edgesA[i];
int b = edgesB[i];
g[a].push_back(b);
g[b].push_back(a);
}
build(1);
}
void upd(int a, int sgn) {
if (!a) {
return;
}
int p = par[a];
dp1[p] += sgn * min(dp1[a], 1 + dp2[a]);
dp2[p] += sgn * min(dp2[a], 1 + dp1[a]);
upd(p, sgn);
}
void change(int v, int c1, int c2) {
cost1[v] = c1;
cost2[v] = c2;
dfs(1);
}
int cat(int v) {
change(v, INF, 0);
return min(dp1[1], dp2[1]);
}
int dog(int v) {
change(v, 0, INF);
return min(dp1[1], dp2[1]);
}
int neighbor(int v) {
change(v, 0, 0);
return min(dp1[1], dp2[1]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |