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;
const int MAXN = 112345;
long long INF = 1123456789;
vector <int> grafo[MAXN];
int tp[MAXN];
long long dp[MAXN][2][2];
void dfs(int v, int p) {
for(int i = 0; i < grafo[v].size(); i++) {
int viz = grafo[v][i];
if(viz == p) continue;
dfs(viz, v);
}
long long cur[2][2];
bool fl = 0;
for(int i = 0; i < grafo[v].size(); i++) {
int viz = grafo[v][i];
if(viz == p) continue;
//printf("%d\n", v);
if(fl == 0) {
//printf("%d\n", fl);
cur[0][0] = dp[viz][0][0]; cur[0][1] = dp[viz][0][1];
cur[1][0] = dp[viz][1][0]; cur[1][1] = dp[viz][1][1];
fl = 1;
}
else {
//printf("oi\n");
long long temp[2][2];
temp[0][0] = min(cur[0][0] + dp[viz][0][0], cur[0][1] + dp[viz][0][1]);
temp[0][1] = min(cur[0][1] + dp[viz][0][0], cur[0][0] + dp[viz][0][1]);
temp[1][0] = min(cur[1][0] + dp[viz][1][0], cur[1][1] + dp[viz][1][1]);
temp[1][1] = min(cur[1][1] + dp[viz][1][0], cur[1][0] + dp[viz][1][1]);
cur[0][0] = temp[0][0]; cur[0][1] = temp[0][1];
cur[1][0] = temp[1][0]; cur[1][1] = temp[1][1];
//printf("%d %d %d %d %d\n", v, temp[0][0], temp[0][1], temp[1][0], temp[1][1]);
}
}
if(fl == 0) {
//printf("%d\n", v);
if(tp[v] == 0) {
//printf("%d\n", v);
dp[v][0][0] = 0LL;
dp[v][0][1] = INF;
dp[v][1][0] = INF;
dp[v][1][1] = 1LL;
}
else {
dp[v][0][0] = INF;
dp[v][0][1] = 1LL;
dp[v][1][0] = 0LL;
dp[v][1][1] = INF;
}
//printf("%d %lld %lld %lld %lld\n", v, dp[v][0][0], dp[v][1][0], dp[v][0][1], dp[v][1][1]);
}
else {
if(tp[v] == 0) {
dp[v][0][0] = cur[0][0];
dp[v][0][1] = cur[1][1] + 1;
dp[v][1][0] = cur[0][1];
dp[v][1][1] = cur[1][0] + 1;
}
else {
dp[v][0][0] = cur[0][1];
dp[v][0][1] = cur[1][0] + 1;
dp[v][1][0] = cur[0][0];
dp[v][1][1] = cur[1][1] + 1;
}
}
//printf("%d %lld %lld %lld %lld\n", v, dp[v][0][0], dp[v][1][0], dp[v][0][1], dp[v][1][1]);
}
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
grafo[a].push_back(b); grafo[b].push_back(a);
}
for(int i = 1; i <= n; i++) scanf("%d", &tp[i]);
dfs(1, 1);
if(min(dp[1][0][0], dp[1][0][1]) < INF) printf("%lld\n", min(dp[1][0][0], dp[1][0][1]));
else printf("impossible\n");
}
Compilation message (stderr)
xanadu.cpp: In function 'void dfs(int, int)':
xanadu.cpp:9:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for(int i = 0; i < grafo[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
xanadu.cpp:16:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i = 0; i < grafo[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
xanadu.cpp: In function 'int main()':
xanadu.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
xanadu.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
xanadu.cpp:79:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | for(int i = 1; i <= n; i++) scanf("%d", &tp[i]);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |