This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
3
1 2 1
2 3 1
3 1 2
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int V[101010], C[101010], D[101010];
int Sc[101010], Sd[101010];
vector<int>adj[101010];
int pa[101010];
void dfs(int i, int p)
{
auto it = find(adj[i].begin(), adj[i].end(), p);
if (it != adj[i].end())
adj[i].erase(it);
pa[i] = p;
for (int j : adj[i])
{
dfs(j, i);
}
}
void initialize(int N, vector<int> A, vector<int> B)
{
for (int i = 0; i < N - 1; i++)
{
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
dfs(1, 0);
}
int ans = 0;
void recalc(int v)
{
int c = C[v];
int d = D[v];
Sc[pa[v]] -= C[v];
Sd[pa[v]] -= D[v];
C[v] = Sc[v];
D[v] = Sd[v];
if (V[v] == -1)
{
D[v] = C[v] + 1;
}
if (V[v] == 1)
{
C[v] = D[v] + 1;
}
C[v] = min(C[v], D[v] + 1);
D[v] = min(D[v], C[v] + 1);
Sc[pa[v]] += C[v];
Sd[pa[v]] += D[v];
if (v != 1 && (C[v] - D[v] != c - d))
recalc(pa[v]);
else if (v != 1)
{
ans += C[v] - c;
Sc[pa[v]] -= C[v] - c;
Sd[pa[v]] -= C[v] - c;
}
}
int cat(int v)
{
V[v] = -1;
recalc(v);
return ans + min(C[1], D[1]);
}
int dog(int v)
{
V[v] = 1;
recalc(v);
return ans + min(C[1], D[1]);
}
int neighbor(int v)
{
V[v] = 0;
recalc(v);
return ans + min(C[1], D[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... |