This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 2e5;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
int n;
int a[N + 1];
vector<int> g[N+1];
vector<vector<int>> dfs(int v, int par){
if(g[v].size() == 1 and v != par){
vector< vector<int> > res(2, vector<int>(2));
res[a[v]][1] = res[!a[v]][0] = MOD;
res[a[v]][0] = 0, res[!a[v]][1] = 1;
return res;
}
vector< vector<int> > sub(2, vector<int>(2, 0));
sub[0][1] = INF;
sub[1][1] = INF;
for(int to : g[v]){
if(to == par) continue;
auto child = dfs(to, v);
vector< vector<int> > new_sub(2, vector<int>(2));
for(int i = 0;i < 2; i++){
new_sub[i][0] = min(sub[i][0] + child[i][0], sub[i][1] + child[i][1]);
new_sub[i][1] = min(sub[i][0] + child[i][1], sub[i][1] + child[i][0]);
}
sub = new_sub;
}
vector< vector<int> > to_ret(2, vector<int>(2));
to_ret[a[v]][0] = sub[1][0];
to_ret[a[v]][1] = sub[0][1] + 1;
to_ret[!a[v]][0] = sub[1][1];
to_ret[!a[v]][1] = sub[0][0] + 1;
return to_ret;
}
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1;i < n; i++){
int A, b; cin >> A >> b;
g[A].push_back(b);
g[b].push_back(A);
}
for(int i = 1;i <= n; i++){
cin >> a[i];
a[i] = !a[i];
}
auto ans = dfs(1, 1);
int res = min(ans[1][1], ans[1][0]);
if(res >= MOD) cout << "impossible";
else cout << res;
return 0;
}
Compilation message (stderr)
xanadu.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main(){
| ^~~~
# | 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... |