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>
#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... |