이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0)
#define pii pair<int, int>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 7;
int n, m, t, tmp, tmp2, tmp3, tmp4, u, v, w, flag, k, q, ans;
int arr[MAXN], dp[MAXN][2][2], pd[MAXN][2][2];
vector<int> adj[MAXN];
void DFS(int v, int p=-1) {
dp[v][arr[v]][0] = 0; dp[v][arr[v]][1] = INF;
dp[v][1-arr[v]][1] = 1; dp[v][1-arr[v]][0] = INF;
for (int u:adj[v]) {
if (u == p) continue;
DFS(u, v);
for (int i=0; i<2; i++) for (int j=0; j<2; j++) pd[v][i][j] = dp[v][i][j];
dp[v][0][0] = min(dp[u][1][1]+pd[v][1][0], dp[u][1][0]+pd[v][0][0]);
dp[v][1][0] = min(dp[u][1][1]+pd[v][0][0], dp[u][1][0]+pd[v][1][0]);
dp[v][0][1] = min(dp[u][0][1]+pd[v][1][1], dp[u][0][0]+pd[v][0][1]);
dp[v][1][1] = min(dp[u][0][1]+pd[v][0][1], dp[u][0][0]+pd[v][1][1]);
}
}
int32_t main() {
#ifdef DEBUG
freopen("inp.in", "r", stdin);
freopen("res.out", "w", stdout);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
cin >> n;
for (int i=1; i<n; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i=1; i<=n; i++) {
cin >> arr[i];
arr[i] = 1-arr[i];
}
DFS(1);
ans = min(dp[1][1][0], dp[1][1][1]);
if (ans >= INF/2) cout << "impossible\n";
else cout << ans << endl;
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... |