#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
const int nmax = 1e5 + 9;
const int inf = 1e9;
vector<int> adj[nmax];
bool on[nmax];
int dp[2][2][nmax];
void dfs (int nod = 1, int par = 0)
{
int temp[2][2];
temp[0][0] = temp[0][1] = 0;
temp[1][0] = temp[1][1] = 0;
if (adj[nod].size() == 1 && nod != 1)
{
if (on[nod])
{
temp[1][1] = inf;
temp[0][1] = inf;
}
else {
temp[1][1] = inf;
temp[0][1] = inf;
}
}
temp[0][1] = inf;
temp[1][1] = inf;
for (auto to : adj[nod])
{
if (to == par) continue;
dfs (to , nod);
int t[2][2];
t[0][0] = min(temp[0][0] + dp[0][0][to] , temp[0][1] + dp[0][1][to]);
t[1][0] = min (temp[1][0] + dp[1][0][to] , temp[1][1] + dp[1][1][to]);
t[0][1] = min(temp[0][1] + dp[0][0][to] , temp[0][0] + dp[0][1][to]);
t[1][1] = min(temp[1][0] + dp[1][1][to] , temp[1][1] + dp[1][0][to]);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
temp[i][j] = t[i][j];
}
if (on[nod])
{
dp[0][0][nod] = temp[0][1];
dp[0][1][nod] = 1 + temp[1][0];
dp[1][0][nod] = temp[0][0];
dp[1][1][nod] = 1 + temp[1][1];
}
else {
dp[0][0][nod] = temp[0][0];
dp[0][1][nod] = 1 + temp[1][1];
dp[1][0][nod] = temp[0][1];
dp[1][1][nod] = 1 + temp[1][0];
}
// cout << nod << '\n';
// cout << temp[0][0] << ' ' << temp[0][1] << ' ' << temp[1][0] << ' ' << temp[1][1] << '\n';
// cout << dp[0][0][nod] << ' ' << dp[0][1][nod] << ' ' << dp[1][0][nod] <<
// ' '<< dp[1][1][nod];
// cout << '\n';
}
int main ()
{
int n; cin >> n;
for (int i = 1; i < n; i++)
{
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
cin >> on[i];
}
dfs();
if (min(dp[0][1][1] , dp[0][0][1]) >= inf) cout << "impossible\n";
else cout << min(dp[0][1][1] , dp[0][0][1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
18068 KB |
Output is correct |
2 |
Correct |
95 ms |
17852 KB |
Output is correct |
3 |
Correct |
100 ms |
18136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
18128 KB |
Output is correct |
2 |
Correct |
117 ms |
17992 KB |
Output is correct |
3 |
Correct |
101 ms |
18104 KB |
Output is correct |
4 |
Incorrect |
82 ms |
8204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |