#include<iostream>
#include<vector>
#include<queue>
using namespace std;
using pi = pair<int, int>;
using pii = pair<pi, pi>;
int main() {
int n; cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++)
{
int a, b; cin >> a >> b; a--; b--;
g[b].push_back(a); g[a].push_back(b);
}
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
vector<int> p(n), ch(n);
queue<int> q; q.push(0);
while (!q.empty())
{
int x = q.front(); q.pop();
for (int y : g[x])
{
if (p[x] != y)
{
p[y] = x;
q.push(y);
ch[x]++;
}
}
}
for (int i = 0; i < n; i++)
{
if (!ch[i])
{
q.push(i);
}
}
int r1 = 0;
vector<int> a(n), vv = v;
for (int j = n - 1; j >= 2; j--)
{
if (v[j] != a[j])
{
v[j] = !v[j];
v[j - 1] = !v[j - 1];
v[j - 2] = !v[j - 2];
r1++;
}
}
if (v[1] != a[1])
{
v[1] = !v[1];
v[0] = !v[0];
r1++;
}
if (v[0] != a[0])
{
r1 = n + 1;
}
v = vv;
int r2 = 1;
a[n - 1] = 1; a[n - 2] = 1;
for (int j = n - 1; j >= 2; j--)
{
if (v[j] != a[j])
{
v[j] = !v[j];
v[j - 1] = !v[j - 1];
v[j - 2] = !v[j - 2];
r2++;
}
}
if (v[1] != a[1])
{
v[1] = !v[1];
v[0] = !v[0];
r2++;
}
if (v[0] != a[0])
{
r2 = n + 1;
}
int r = min(r1, r2);
if (r == n + 1)
{
cout << "IMPOSSIBLE" << endl;
return 0;
}
cout << r << endl;
}
# | 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... |