이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
vector<int> adj[100005];
ll dp[100005][2][2]; // node, black/white, press
int status[100005];
ll inf = 1e9;
void dfs(int node, int par = -1) {
if (adj[node].size() == 1 && par != -1) {
dp[node][status[node]][0] = 0; dp[node][1 - status[node]][1] = 1;
return;
}
ll temp[2][2] = {{(ll) 1e9, (ll) 1e9}, {(ll) 1e9, (ll) 1e9}};
bool black = true, white = true;
for (auto nxt : adj[node]) {
if (nxt == par) continue;
dfs(nxt, node);
// dont press
if (min(dp[nxt][0][0], dp[nxt][0][1]) >= inf) {
black = false;
}
if (min(dp[nxt][1][0], dp[nxt][1][1]) >= inf) white = false;
ll t1 = (temp[0][0] >= inf && temp[0][1] >= inf ? dp[nxt][0][0] : min(temp[0][0] + dp[nxt][0][0], temp[0][1] + dp[nxt][0][1]));
ll t2 = (temp[0][0] >= inf && temp[0][1] >= inf ? dp[nxt][0][1] : min(temp[0][0] + dp[nxt][0][1], temp[0][1] + dp[nxt][0][0]));
ll t3 = (temp[1][0] >= inf && temp[1][1] >= inf ? dp[nxt][1][0] : min(temp[1][0] + dp[nxt][1][0], temp[1][1] + dp[nxt][1][1]));
ll t4 = (temp[1][0] >= inf && temp[1][1] >= inf ? dp[nxt][1][1] : min(temp[1][0] + dp[nxt][1][1], temp[1][1] + dp[nxt][1][0]));
temp[0][0] = t1; temp[0][1] = t2; temp[1][0] = t3; temp[1][1] = t4;
}
if (black) {
dp[node][status[node]][0] = temp[0][0];
dp[node][1 - status[node]][0] = temp[0][1];
}
if (white) {
dp[node][1 - status[node]][1] = temp[1][0] + 1;
dp[node][status[node]][1] = temp[1][1] + 1;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
int n; cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) cin >> status[i];
fill(&dp[0][0][0], &dp[0][0][0] + sizeof(dp) / sizeof(dp[0][0][0]), 1e9);
dfs(1);
if (dp[1][0][0] >= inf && dp[1][0][1] >= inf) cout << "impossible";
else cout << min(dp[1][0][0], dp[1][0][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... |