이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) { return (a > b ? (a = b, 1) : 0); }
template <typename T>
bool umx(T& a, T b) { return (a < b ? (a = b, 1) : 0); }
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 1e5 + 5;
int n;
ve<int> g[N];
bool a[N];
int dp[N][2][2];
void dfs(int v, int p) {
bool leaf = 1;
fe (to, g[v]) {
if (to == p) continue;
dfs(to, v);
leaf = 0;
}
if (leaf) {
dp[v][a[v]][0] = 0;
dp[v][!a[v]][1] = 1;
return;
}
// cout << dp[v][0][1] << "\n";
// don't press
ve<int> dp(2, 1e9), ndp(2, 1e9);
dp[a[v]] = 0;
fe (to, g[v]) {
if (to == p) continue;
fr (was, 0, 1) {
umn(ndp[was ^ 1], dp[was] + ::dp[to][0][1]);
umn(ndp[was], dp[was] + ::dp[to][0][0]);
}
fr (was, 0, 1) {
dp[was] = ndp[was];
ndp[was] = 1e9;
}
}
umn(::dp[v][0][0], dp[0]);
umn(::dp[v][1][0], dp[1]);
fill(all(dp), 1e9);
fill(all(ndp), 1e9);
// press
dp[!a[v]] = 1;
fe (to, g[v]) {
if (to == p) continue;
fr (was, 0, 1) {
umn(ndp[was ^ 1], dp[was] + ::dp[to][1][1]);
umn(ndp[was], dp[was] + ::dp[to][1][0]);
}
fr (was, 0, 1) {
dp[was] = ndp[was];
ndp[was] = 1e9;
}
}
umn(::dp[v][0][1], dp[0]);
umn(::dp[v][1][1], dp[1]);
// cout << v << " " << ::dp[v][0][0] << " " << ::dp[v][0][1] << " " << ::dp[v][1][0] << " " << ::dp[v][1][1] << "\n";
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n;
fr (i, 1, n - 1) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
fr (i, 1, n) {
cin >> a[i];
}
fr (i, 1, n) {
fr (j, 0, 1) {
fr (k, 0, 1) {
dp[i][j][k] = 1e9;
}
}
}
dfs(1, -1);
int best = 1e9;
fr (i, 0, 1) {
umn(best, dp[1][0][i]);
}
if (best == 1e9) {
cout << "impossible\n";
return 0;
}
cout << best << "\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... |