This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//khodaya khodet komak kon
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define fast_io ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 1e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int mod = 1e9 + 7;//998244353;
const int base = 257;
int n, dp[xn][2][2], pd[2][2], ans = inf, f[xn];
vector <int> adj[xn];
void DFS(int v, int p = - 1){
dp[v][0][1] = dp[v][1][1] = inf;
dp[v][0][0] = 0, dp[v][1][0] = 1;
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[i][j] = dp[v][i][j], dp[v][i][j] = inf;
for (int f1 = 0; f1 < 2; ++ f1)
for (int f2 = 0; f2 < 2; ++ f2)
for (int f3 = 0; f3 < 2; ++ f3)
for (int f4 = 0; f4 < 2; ++ f4)
if ((f1 ^ f3 ^ f4) == f[u])
dp[v][f1][f2 ^ f3] = min(dp[v][f1][f2 ^ f3], pd[f1][f2] + dp[u][f3][f4]);
}
}
int main(){
fast_io;
cin >> n;
for (int i = 1; i < n; ++ i){
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= n; ++ i)
cin >> f[i];
DFS(1);
for (int i = 0; i < 2; ++ i)
for (int j = 0; j < 2; ++ j)
if ((i ^ j) == f[1])
ans = min(ans, dp[1][i][j]);
if (ans == inf)
kill("impossible");
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... |