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;
#define ll long long
#define INF 1000000
#define DP dp[p][s][v]
vector<bool> state;
vector<vector<int>> adj;
vector<vector<vector<int>>> dp(2, vector<vector<int>>(2, vector<int>(100005, -1)));
ll solve(int p, int s, int v, int parent) {
if(DP != -1) return DP;
vector<vector<ll>> temp(2, vector<ll>(2));
temp[0][0] = 0;
temp[1][0] = 0;
temp[0][1] = INF;
temp[1][1] = INF;
for(auto &u : adj[v]) {
if(u == parent) continue;
vector<vector<ll>> ntemp(2,vector<ll>(2));
ntemp[0][0] = min(solve(0, 0, u, v) + temp[0][0], solve(0, 1, u, v) + temp[0][1]);
ntemp[0][1] = min(solve(0, 0, u, v) + temp[0][1], solve(0, 1, u, v) + temp[0][0]);
ntemp[1][0] = min(solve(1, 0, u, v) + temp[1][0], solve(1, 1, u, v) + temp[1][1]);
ntemp[1][1] = min(solve(1, 0, u, v) + temp[1][1], solve(1, 1, u, v) + temp[1][0]);
temp = ntemp;
}
if((state[v] + s + p)&1 == 1) {
DP = s + temp[s][1];
} else {
DP = s + temp[s][0];
}
return DP;
}
int main() {
int n; cin >> n;
adj.resize(n+2);
state.resize(n+2);
for(int i = 1; i < n; i++) {
int a,b; cin >> a >> b;
a--; b--;
adj[b].push_back(a);
adj[a].push_back(b);
}
for(int i = 0; i < n; i++) {
int a; cin >> a;
if(a) state[i] = true;
else state[i] = false;
}
long long ans = min(solve(0, 0, 0, -1), solve(0, 1, 0, -1));
if(ans >= INF) {
cout << "impossible";
} else {
cout << ans;
}
}
Compilation message (stderr)
xanadu.cpp: In function 'long long int solve(int, int, int, int)':
xanadu.cpp:25:29: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
25 | if((state[v] + s + p)&1 == 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |