#include <bits/stdc++.h>
#define TASK "askdjaskd"
#define endl '\n'
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define BIT(i,x) (((i)>>(x))&1)
#define FOR(i,a,b) for(int i = (a); i<=(b); i++)
#define FORD(i,a,b) for(int i = (a); i>=(b); i--)
#define all(C) C.begin(), C.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int INT_LIM = 2147483647;
const ll LL_LIM = 9223372036854775807;
template <typename X> bool minimize(X &x, const X &y) {if (x>y){x = y; return true;}return false;}
template <typename X> bool maximize(X &x, const X &y) {if (x<y){x = y; return true;}return false;}
///------------------------------------------///
int n;
int a[100005];
vector<int> adj[100005];
void inp()
{
cin >> n;
FOR(i, 1, n-1)
{
int u,v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
FOR(i, 1, n) cin >> a[i];
}
int f[100005][2][2];
void calc(int u, int p)
{
bool leaf = true;
for (auto v:adj[u]) if (v!=p)
{
calc(v, u);
leaf = false;
}
if (leaf)
{
f[u][0][a[u]] = 0;
f[u][1][(a[u]+1)%2] = 1;
return;
}
FOR(d, 0, 1)
{
vector<int> pr(2, 0);
pr[1] = INT_LIM;
for (auto v:adj[u]) if (v!=p)
{
vector<int> g(2, INT_LIM);
if (f[v][0][d]!=INT_LIM)
{
if (pr[0]!=INT_LIM) minimize(g[0], pr[0]+f[v][0][d]);
if (pr[1]!=INT_LIM) minimize(g[1], pr[1]+f[v][0][d]);
}
if (f[v][1][d]!=INT_LIM)
{
if (pr[0]!=INT_LIM) minimize(g[1], pr[0]+f[v][1][d]);
if (pr[1]!=INT_LIM) minimize(g[0], pr[1]+f[v][1][d]);
}
pr = g;
}
if (pr[0]!=INT_LIM) f[u][d][(a[u]+d+0)%2] = pr[0]+d;
if (pr[1]!=INT_LIM) f[u][d][(a[u]+d+1)%2] = pr[1]+d;
// if (u==4) cout << d << ' ' << pr[0] << ' ' << pr[1] << endl;
}
}
void solve()
{
FOR(i, 1, n) FOR(j, 0, 1) FOR(k, 0, 1) f[i][j][k] = INT_LIM;
calc(1, -1);
int ans = min(f[1][0][0], f[1][1][0]);
if (ans==INT_LIM) cout << "impossible";
else cout << ans;
}
signed main()
{
///--------------------------///
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
if (fopen(TASK".INP","r")!=NULL)
{
freopen(TASK".INP","r",stdin);
freopen(TASK".OUT","w",stdout);
}
///--------------------------///
int NTEST = 1;
bool codeforces = 0;
if (codeforces) cin >> NTEST;
while (NTEST--)
{
inp();
solve();
}
return 0;
}
///------------------------------------------///
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'int main()':
xanadu.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(TASK".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(TASK".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |