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 i64 = long long;
constexpr int N = 1e5;
i64 dp[N][2][2], a[N];
std::vector<int> e[N];
struct opt{
std::vector<i64> pr;
i64 sum;
opt(std::vector<i64> a, std::vector<i64> b) : pr(a.size() + 1), sum(0) {
std::vector<i64> v;
for (int i = 0; i < int(a.size()); i++) {
v.push_back(-a[i] + b[i]);
}
sort(v.begin(), v.end());
for (int i = 0; i < int(v.size()); i++) {
sum += a[i];
pr[i + 1] = pr[i] + v[i];
}
}
i64 odd() {
i64 res = 1e9;
for (int i = 1; i < pr.size(); i += 2) {
res = std::min(res, pr[i] + sum);
}
return res;
}
i64 even() {
i64 res = 1e9;
for (int i = 0; i < pr.size(); i += 2) {
res = std::min(res, pr[i] + sum);
}
return res;
}
};
void dfs(int u, int p) {
if (e[u].size() == 1 && p != -1) {
if (a[u] == 0) {
dp[u][0][0] = 0;
dp[u][1][1] = 1;
} else {
dp[u][1][0] = 0;
dp[u][0][1] = 1;
}
return;
}
std::vector<int> c;
for (auto v : e[u]) {
if (v == p) continue;
dfs(v, u);
c.push_back(v);
}
int k = c.size();
if (k == 1) {
int l = c[0];
if (a[u] == 0) {
dp[u][0][0] = dp[l][0][0];
dp[u][0][1] = dp[l][1][1] + 1;
dp[u][1][0] = dp[l][0][1];
dp[u][1][1] = dp[l][1][0] + 1;
} else {
dp[u][1][0] = dp[l][0][0];
dp[u][1][1] = dp[l][1][1] + 1;
dp[u][0][0] = dp[l][0][1];
dp[u][0][1] = dp[l][1][0] + 1;
}
} else {
if (a[u] == 0) {
{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][0][0]);
for (auto x : c) B.push_back(dp[x][0][1]);
opt p(A, B);
dp[u][0][0] = p.even();
}{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][1][0]);
for (auto x : c) B.push_back(dp[x][1][1]);
opt p(A, B);
dp[u][0][1] = p.odd() + 1;
}
{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][0][0]);
for (auto x : c) B.push_back(dp[x][0][1]);
opt p(A, B);
dp[u][1][0] = p.odd();
}{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][1][0]);
for (auto x : c) B.push_back(dp[x][1][1]);
opt p(A, B);
dp[u][1][1] = p.even() + 1;
}
} else {
{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][0][0]);
for (auto x : c) B.push_back(dp[x][0][1]);
opt p(A, B);
dp[u][1][0] = p.even();
}{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][1][0]);
for (auto x : c) B.push_back(dp[x][1][1]);
opt p(A, B);
dp[u][1][1] = p.odd() + 1;
}
{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][0][0]);
for (auto x : c) B.push_back(dp[x][0][1]);
opt p(A, B);
dp[u][0][0] = p.odd();
}{
std::vector<i64> A, B;
for (auto x : c) A.push_back(dp[x][1][0]);
for (auto x : c) B.push_back(dp[x][1][1]);
opt p(A, B);
dp[u][0][1] = p.even() + 1;
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
dp[i][j][k] = 1e9;
}
}
}
dfs(0, -1);
i64 ans = 1e9;
for (int j = 0; j < 2; j++) {
ans = std::min(ans, dp[0][0][j]);
}
if (ans == 1e9) {
std::cout << "impossible\n";
} else {
std::cout << ans << "\n";
}
return 0;
}
Compilation message (stderr)
xanadu.cpp: In member function 'i64 opt::odd()':
xanadu.cpp:22:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 1; i < pr.size(); i += 2) {
| ~~^~~~~~~~~~~
xanadu.cpp: In member function 'i64 opt::even()':
xanadu.cpp:29:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0; i < pr.size(); i += 2) {
| ~~^~~~~~~~~~~
# | 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... |