#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
vector<int> g[N];
int val[N];
int dp[N][2][2];
/*
dp[i][j][k] => currently in the ith node, the ith subtree is safe (except the last layer, maybe)
j => current condition of the node (0 = off, 1 = on)
k => whether the parents value has been altered or not (0 = no, 1 = yes)
*/
void dfs(int node, int par) {
if(g[node].size() == 1) {
dp[node][val[node]][0] = 0;
dp[node][1LL - val[node]][1] = 1;
return;
}
vector<int> children[2];
int sum0 = 0, sum1 = 0;
for(int i = 0; i < g[node].size(); i++) {
if(g[node][i] != par) {
dfs(g[node][i], node);
sum0 += dp[g[node][i]][0][0];
sum1 += dp[g[node][i]][1][0];
if(dp[g[node][i]][0][1] != INT_MAX) {
children[0].push_back(dp[g[node][i]][0][1] - dp[g[node][i]][0][0]);
}
if(dp[g[node][i]][1][1] != INT_MAX) {
children[1].push_back(dp[g[node][i]][1][1] - dp[g[node][i]][1][0]);
}
}
}
for(int i = 0; i < 2; i++) {
sort(children[i].begin(), children[i].end());
}
/*cout << "node = " << node << "\n";
cout << "sum0 = " << sum0 << ", sum1 = " << sum1 << "\n";
for(int i = 0; i < 2; i++) {
cout << "iteration = " << i << "\n";
for(int j = 0; j < children[i].size(); j++) {
cout << children[i][j] << " ";
}
cout << "\n";
}*/
int prev0 = sum0, prev1 = sum1;
/* if all the children have their cameras off */
//Subpart 1: not to change the value of node
dp[node][val[node]][0] = min(dp[node][val[node]][0], sum0);
for(int i = 0; i < children[0].size(); i += 2) {
if(i + 1 == children[0].size())
break;
sum0 += (children[0][i] + children[0][i + 1]);
dp[node][val[node]][0] = min(dp[node][val[node]][0], sum0);
}
//Subpart 2: change the value of the node
if(children[0].size() != 0) {
sum0 = prev0 + children[0][0];
dp[node][1LL - val[node]][0] = min(dp[node][1LL - val[node]][0], sum0);
}
for(int i = 1; i < children[0].size(); i += 2) {
if(i + 1 == children[0].size())
break;
sum0 += (children[0][i] + children[0][i + 1]);
dp[node][1LL - val[node]][0] = min(dp[node][1LL - val[node]][0], sum0);
}
/* if all the children have their cameras on
Note: parent and node have to be tempered, so its reverse */
//Subpart 1: not to change the value of node
if(children[1].size() != 0) {
sum1 = prev1 + children[1][0] + 1;
dp[node][val[node]][1] = min(dp[node][val[node]][1], sum1);
}
for(int i = 1; i < children[1].size(); i += 2) {
if(i + 1 == children[1].size())
break;
sum1 += children[1][i] + children[1][i + 1];
dp[node][val[node]][1] = min(dp[node][val[node]][1], sum1);
}
//Subpart 2: change the value of node
sum1 = prev1 + 1;
dp[node][1LL - val[node]][1] = sum1;
for(int i = 0; i < children[1].size(); i += 2) {
if(i + 1 == children[1].size())
break;
sum1 += children[1][i] + children[1][i + 1];
dp[node][1LL - val[node]][1] = min(dp[node][1LL - val[node]][1], sum1);
}
}
void Solve() {
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] = INT_MAX;
}
}
}
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
g[v].push_back(u);
g[u].push_back(v);
}
g[1].push_back(-1);
for(int i = 1; i <= n; i++) {
cin >> val[i];
}
dfs(1, -1);
int ans = min(dp[1][0][0], dp[1][0][1]);
if(ans == INT_MAX) {
cout << "impossible\n";
}
else {
cout << ans << "\n";
}
//cout << min(dp[1][0][0], dp[1][0][1]) << "\n";
/*for(int i = 1; i <= n; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
cout << "dp[" << i << "][" << j << "][" << k << "] = " << dp[i][j][k] << "\n";
}
cout << "\n\n";
}
cout << "\n\n\n\n";
}*/
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
return 0;
}
Compilation message
xanadu.cpp: In function 'void dfs(long long int, long long int)':
xanadu.cpp:24:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
xanadu.cpp:56:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0; i < children[0].size(); i += 2) {
| ~~^~~~~~~~~~~~~~~~~~~~
xanadu.cpp:57:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(i + 1 == children[0].size())
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:68:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i = 1; i < children[0].size(); i += 2) {
| ~~^~~~~~~~~~~~~~~~~~~~
xanadu.cpp:69:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if(i + 1 == children[0].size())
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:85:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i = 1; i < children[1].size(); i += 2) {
| ~~^~~~~~~~~~~~~~~~~~~~
xanadu.cpp:86:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | if(i + 1 == children[1].size())
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:95:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i = 0; i < children[1].size(); i += 2) {
| ~~^~~~~~~~~~~~~~~~~~~~
xanadu.cpp:96:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | if(i + 1 == children[1].size())
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5796 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5796 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
6 ms |
5800 KB |
Output is correct |
7 |
Correct |
3 ms |
5764 KB |
Output is correct |
8 |
Correct |
5 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5800 KB |
Output is correct |
12 |
Correct |
3 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5708 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
28064 KB |
Output is correct |
2 |
Correct |
91 ms |
27760 KB |
Output is correct |
3 |
Correct |
82 ms |
28168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
28048 KB |
Output is correct |
2 |
Correct |
79 ms |
27844 KB |
Output is correct |
3 |
Correct |
80 ms |
28208 KB |
Output is correct |
4 |
Correct |
84 ms |
11228 KB |
Output is correct |
5 |
Correct |
99 ms |
11692 KB |
Output is correct |
6 |
Correct |
96 ms |
11932 KB |
Output is correct |
7 |
Correct |
4 ms |
5836 KB |
Output is correct |
8 |
Correct |
24 ms |
7844 KB |
Output is correct |
9 |
Correct |
90 ms |
11608 KB |
Output is correct |
10 |
Correct |
102 ms |
11716 KB |
Output is correct |
11 |
Correct |
83 ms |
12808 KB |
Output is correct |
12 |
Correct |
93 ms |
13196 KB |
Output is correct |
13 |
Correct |
90 ms |
11792 KB |
Output is correct |
14 |
Correct |
82 ms |
12172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5796 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
6 ms |
5800 KB |
Output is correct |
7 |
Correct |
3 ms |
5764 KB |
Output is correct |
8 |
Correct |
5 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5800 KB |
Output is correct |
12 |
Correct |
3 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5708 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
84 ms |
28064 KB |
Output is correct |
18 |
Correct |
91 ms |
27760 KB |
Output is correct |
19 |
Correct |
82 ms |
28168 KB |
Output is correct |
20 |
Correct |
76 ms |
28048 KB |
Output is correct |
21 |
Correct |
79 ms |
27844 KB |
Output is correct |
22 |
Correct |
80 ms |
28208 KB |
Output is correct |
23 |
Correct |
84 ms |
11228 KB |
Output is correct |
24 |
Correct |
99 ms |
11692 KB |
Output is correct |
25 |
Correct |
96 ms |
11932 KB |
Output is correct |
26 |
Correct |
4 ms |
5836 KB |
Output is correct |
27 |
Correct |
24 ms |
7844 KB |
Output is correct |
28 |
Correct |
90 ms |
11608 KB |
Output is correct |
29 |
Correct |
102 ms |
11716 KB |
Output is correct |
30 |
Correct |
83 ms |
12808 KB |
Output is correct |
31 |
Correct |
93 ms |
13196 KB |
Output is correct |
32 |
Correct |
90 ms |
11792 KB |
Output is correct |
33 |
Correct |
82 ms |
12172 KB |
Output is correct |
34 |
Correct |
3 ms |
5708 KB |
Output is correct |
35 |
Correct |
3 ms |
5708 KB |
Output is correct |
36 |
Correct |
3 ms |
5708 KB |
Output is correct |
37 |
Correct |
3 ms |
5708 KB |
Output is correct |
38 |
Correct |
4 ms |
5852 KB |
Output is correct |
39 |
Correct |
4 ms |
5708 KB |
Output is correct |
40 |
Correct |
4 ms |
5752 KB |
Output is correct |
41 |
Correct |
4 ms |
5860 KB |
Output is correct |
42 |
Correct |
4 ms |
5708 KB |
Output is correct |
43 |
Correct |
4 ms |
5708 KB |
Output is correct |
44 |
Correct |
4 ms |
5812 KB |
Output is correct |
45 |
Correct |
84 ms |
28100 KB |
Output is correct |
46 |
Correct |
87 ms |
27812 KB |
Output is correct |
47 |
Correct |
77 ms |
28136 KB |
Output is correct |
48 |
Correct |
68 ms |
11204 KB |
Output is correct |
49 |
Correct |
89 ms |
11696 KB |
Output is correct |
50 |
Correct |
81 ms |
11816 KB |
Output is correct |
51 |
Correct |
4 ms |
5836 KB |
Output is correct |
52 |
Correct |
26 ms |
7808 KB |
Output is correct |
53 |
Correct |
93 ms |
11608 KB |
Output is correct |
54 |
Correct |
95 ms |
11732 KB |
Output is correct |
55 |
Correct |
81 ms |
12816 KB |
Output is correct |
56 |
Correct |
84 ms |
13152 KB |
Output is correct |
57 |
Correct |
73 ms |
11816 KB |
Output is correct |
58 |
Correct |
81 ms |
12200 KB |
Output is correct |
59 |
Correct |
24 ms |
7716 KB |
Output is correct |
60 |
Correct |
70 ms |
10948 KB |
Output is correct |
61 |
Correct |
79 ms |
11540 KB |
Output is correct |
62 |
Correct |
85 ms |
11696 KB |
Output is correct |
63 |
Correct |
79 ms |
11708 KB |
Output is correct |
64 |
Correct |
98 ms |
11680 KB |
Output is correct |
65 |
Correct |
60 ms |
12100 KB |
Output is correct |
66 |
Correct |
62 ms |
12100 KB |
Output is correct |
67 |
Correct |
53 ms |
12984 KB |
Output is correct |
68 |
Correct |
53 ms |
13068 KB |
Output is correct |
69 |
Correct |
50 ms |
13104 KB |
Output is correct |
70 |
Correct |
53 ms |
13084 KB |
Output is correct |