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;
typedef long long llint;
typedef pair <int, int> pi;
const int MAXN = 100005;
int n;
llint dp[MAXN][2][2], par[MAXN], col[MAXN];
vector <int> v[MAXN];
void dfs (int x, int rod) {
par[x] = rod;
for (auto sus : v[x]) {
if (sus != rod) dfs(sus, x);
}
}
llint calc (int x, int f, int p) {
if (dp[x][f][p] != -1) return dp[x][f][p];
llint res = 1e9;
llint sum = 0, br = 0, mn = 1e9;
for (auto sus : v[x]) {
if (sus == par[x]) continue;
llint a = calc(sus, 0, f);
llint b = calc(sus, 1, f);
sum += min(a, b);
if (b <= a) br ^= 1;
mn = min(mn, abs(a - b));
}
if (br != f ^ p ^ col[x]) res = sum + mn; else res = sum;
res += f;
return dp[x][f][p] = res;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(dp, -1, sizeof dp);
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, 0);
llint sol = min(calc(1, 0, 0), calc(1, 1, 0));
if (sol >= 1e9) cout << "impossible"; else cout << sol;
return 0;
}
Compilation message (stderr)
xanadu.cpp: In function 'llint calc(int, int, int)':
xanadu.cpp:33:12: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
33 | if (br != f ^ p ^ col[x]) res = sum + mn; else res = sum;
| ~~~^~~~
# | 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... |