이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 1e5 + 7;
int a[N];
int dp[N][2][2];
vi G[N];
void dfs(int u, int p = 0) {
vi c;
for(int v:G[u]) if(v != p) {
dfs(v, u);
c.PB(v);
}
dp[u][a[u]][0] = 0;
for(int v:c) {
int zero = dp[u][0][0], one = dp[u][1][0];
dp[u][0][0] = min({dp[v][0][0] + zero, dp[v][0][1] + one, INF});
dp[u][1][0] = min({dp[v][0][0] + one, dp[v][0][1] + zero, INF});
}
dp[u][a[u] ^ 1][1] = 1;
for(int v:c) {
int zero = dp[u][0][1], one = dp[u][1][1];
dp[u][0][1] = min({dp[v][1][0] + zero, dp[v][1][1] + one, INF});
dp[u][1][1] = min({dp[v][1][0] + one, dp[v][1][1] + zero, INF});
}
}
signed main()
{
IO_OP;
memset(dp, 0x3f, sizeof dp);
int n;
cin >> n;
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].PB(v);
G[v].PB(u);
}
for(int i = 1; i <= n; i++)
cin >> a[i];
dfs(1);
int ans = min(dp[1][0][0], dp[1][0][1]);
if(ans > n) cout << "impossible\n";
else cout << ans << '\n';
}
# | 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... |