이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordset;
#pragma GCC optimize("-O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-Os")
ll INF = 1000000000;
ll mod = 1e9 + 7;
mt19937 gen(time(0));
vector<vector<int>> a;
vector<vector<vector<ll>>> dp; // the first parametr is a vertex, the second is 1 if vertex turned on and the third is 1 if we toggle vertex
vector<int> is;
void dfs(int v, int p){
if(a[v].size() == 1 && p != -1) {
if(is[v]){
dp[v][1][0] = 0;
dp[v][0][1] = 1;
}
else{
dp[v][0][0] = 0;
dp[v][1][1] = 1;
}
return;
}
int i = 0;
for(int u : a[v]){
if(u == p){
swap(a[v][i], a[v].back());
}
i++;
}
if(p != -1) a[v].pop_back();
for(int u : a[v]){
dfs(u, v);
}
vector<vector<ll>> dp2((int)a[v].size(), vector<ll>(2, INF)), dp22((int)a[v].size(), vector<ll>(2, INF));
for(int i = 0; i < (int)a[v].size(); i++){
if(a[v][i] == p) continue;
if(!i) {
dp2[i][0] = dp[a[v][i]][1][0];
dp2[i][1] = dp[a[v][i]][1][1];
dp22[i][0] = dp[a[v][i]][0][0];
dp22[i][1] = dp[a[v][i]][0][1];
} else {
dp2[i][0] = min(dp2[i - 1][0] + dp[a[v][i]][1][0], dp2[i - 1][1] + dp[a[v][i]][1][1]);
dp2[i][1] = min(dp2[i - 1][1] + dp[a[v][i]][1][0], dp2[i - 1][0] + dp[a[v][i]][1][1]);
dp22[i][0] = min(dp22[i - 1][0] + dp[a[v][i]][0][0], dp22[i - 1][1] + dp[a[v][i]][0][1]);
dp22[i][1] = min(dp22[i - 1][1] + dp[a[v][i]][0][0], dp22[i - 1][0] + dp[a[v][i]][0][1]);
}
}
if(!is[v]){
dp[v][0][0] = dp22[(int)a[v].size() - 1][0];
dp[v][1][1] = dp2[(int)a[v].size() - 1][0] + 1ll;
dp[v][0][1] = dp2[(int)a[v].size() - 1][1] + 1ll;
dp[v][1][0] = dp22[(int)a[v].size() - 1][1];
} else{
dp[v][0][0] = dp22[(int)a[v].size() - 1][1];
dp[v][1][1] = dp2[(int)a[v].size() - 1][1] + 1ll;
dp[v][0][1] = dp2[(int)a[v].size() - 1][0] + 1ll;
dp[v][1][0] = dp22[(int)a[v].size() - 1][0];
}
}
void solve(){
int n;
cin >> n;
a.resize(n);
dp.resize(n, vector<vector<ll>>(2, vector<ll>(2, INF)));
is.resize(n);
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
u--; v--;
a[u].push_back(v);
a[v].push_back(u);
}
for(int i = 0; i < n; i++) cin >> is[i];
if(n == 1){
cout << is[0];
return;
}
dfs(0, -1);
if(min(dp[0][0][0], dp[0][0][1]) >= INF){
cout << "impossible";
} else cout << min(dp[0][0][0], dp[0][0][1]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int tt;
//cin >> tt;
tt = 1;
while (tt--) {
solve();
#ifdef LOCAL
cout << "__________________________________" << endl;
#endif
}
#ifdef LOCAL
cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n';
#endif
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... |