이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
const int INF = (int) 1e5 + 1;
vector <int> adj[N];
int state[N], dp[N][2][2];
int calcDP (int v, int flag1, int flag2, int p) {
if (dp[v][flag1][flag2] != -1)
return dp[v][flag1][flag2];
int curState = (state[v] + flag1 + flag2) % 2;
int minEven = 0, minOdd = INF;
for (auto u: adj[v]) {
if (u == p)
continue;
int r1 = calcDP (u, flag2, 0, v);
int r2 = calcDP (u, flag2, 1, v);
int copyMinEven = minEven;
minEven = min (minEven + r1, minOdd + r2);
minOdd = min (minOdd + r1, copyMinEven + r2);
}
int res = flag2;
res += (curState) ? minOdd : minEven;
return dp[v][flag1][flag2] = res;
}
int main () {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back (v);
adj[v].push_back (u);
}
for (int i = 1; i <= n; i++)
cin >> state[i];
memset (dp, -1, sizeof (dp));
int ans = min (calcDP (1, 0, 0, -1), calcDP (1, 0, 1, -1));
if (ans > n) {
puts ("impossible");
return 0;
}
cout << ans << "\n";
return 0;
}
# | 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... |