#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
const int MAXN = 1e5 + 13;
const int INF = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef array<array<int, 2>, 2> dparr;
int N;
vi edge[MAXN];
bitset<MAXN> flip;
dparr dp[MAXN]; //current state of vtx, whether vtx has been flipped.
int ans;
dparr comb(dparr l, dparr r)
{
dparr res;
FOR(i, 0, 2) FOR(j, 0, 2) res[i][j] = INF;
FOR(i, 0, 2)
{
FOR(j, 0, 2)
{
FOR(k, 0, 2)
{
ckmin(res[k ^ j][i], r[i][j] + l[k][i]);
}
}
}
return res;
}
void dfs(int u, int p)
{
for (int v : edge[u])
{
if (v == p) continue;
dfs(v, u);
}
FOR(i, 0, 2)
{
FOR(j, 0, 2)
{
dp[u][i][j] = INF;
}
}
dp[u][flip[u] ^ 1][1] = 1;
dp[u][flip[u]][0] = 0;
for (int v : edge[u])
{
if (v == p) continue;
dp[u] = comb(dp[u], dp[v]);
}
return;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
cin >> N;
FOR(i, 0, N - 1)
{
int u, v;
cin >> u >> v; u--; v--;
edge[u].PB(v);
edge[v].PB(u);
}
FOR(i, 0, N)
{
bool b; cin >> b;
flip[i] = b;
}
dfs(0, N);
ans = min(dp[0][0][1], dp[0][0][0]);
if (ans == INF)
{
cout << "impossible\n";
return 0;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2664 KB |
Output is correct |
13 |
Correct |
3 ms |
2672 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
16428 KB |
Output is correct |
2 |
Correct |
66 ms |
16240 KB |
Output is correct |
3 |
Correct |
66 ms |
16496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
16376 KB |
Output is correct |
2 |
Correct |
73 ms |
16248 KB |
Output is correct |
3 |
Correct |
65 ms |
16480 KB |
Output is correct |
4 |
Correct |
62 ms |
8004 KB |
Output is correct |
5 |
Correct |
62 ms |
8452 KB |
Output is correct |
6 |
Correct |
65 ms |
8608 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
20 ms |
4684 KB |
Output is correct |
9 |
Correct |
78 ms |
8644 KB |
Output is correct |
10 |
Correct |
70 ms |
8676 KB |
Output is correct |
11 |
Correct |
76 ms |
9148 KB |
Output is correct |
12 |
Correct |
83 ms |
9340 KB |
Output is correct |
13 |
Correct |
58 ms |
8260 KB |
Output is correct |
14 |
Correct |
75 ms |
8616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2664 KB |
Output is correct |
13 |
Correct |
3 ms |
2672 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
63 ms |
16428 KB |
Output is correct |
18 |
Correct |
66 ms |
16240 KB |
Output is correct |
19 |
Correct |
66 ms |
16496 KB |
Output is correct |
20 |
Correct |
71 ms |
16376 KB |
Output is correct |
21 |
Correct |
73 ms |
16248 KB |
Output is correct |
22 |
Correct |
65 ms |
16480 KB |
Output is correct |
23 |
Correct |
62 ms |
8004 KB |
Output is correct |
24 |
Correct |
62 ms |
8452 KB |
Output is correct |
25 |
Correct |
65 ms |
8608 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
20 ms |
4684 KB |
Output is correct |
28 |
Correct |
78 ms |
8644 KB |
Output is correct |
29 |
Correct |
70 ms |
8676 KB |
Output is correct |
30 |
Correct |
76 ms |
9148 KB |
Output is correct |
31 |
Correct |
83 ms |
9340 KB |
Output is correct |
32 |
Correct |
58 ms |
8260 KB |
Output is correct |
33 |
Correct |
75 ms |
8616 KB |
Output is correct |
34 |
Correct |
2 ms |
2636 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
2636 KB |
Output is correct |
37 |
Correct |
2 ms |
2668 KB |
Output is correct |
38 |
Correct |
2 ms |
2636 KB |
Output is correct |
39 |
Correct |
2 ms |
2636 KB |
Output is correct |
40 |
Correct |
3 ms |
2668 KB |
Output is correct |
41 |
Correct |
3 ms |
2636 KB |
Output is correct |
42 |
Correct |
2 ms |
2664 KB |
Output is correct |
43 |
Correct |
2 ms |
2668 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
63 ms |
16328 KB |
Output is correct |
46 |
Correct |
66 ms |
16196 KB |
Output is correct |
47 |
Correct |
76 ms |
16476 KB |
Output is correct |
48 |
Correct |
59 ms |
7976 KB |
Output is correct |
49 |
Correct |
69 ms |
8672 KB |
Output is correct |
50 |
Correct |
74 ms |
8676 KB |
Output is correct |
51 |
Correct |
3 ms |
2636 KB |
Output is correct |
52 |
Correct |
20 ms |
4684 KB |
Output is correct |
53 |
Correct |
71 ms |
8600 KB |
Output is correct |
54 |
Correct |
66 ms |
8688 KB |
Output is correct |
55 |
Correct |
66 ms |
9088 KB |
Output is correct |
56 |
Correct |
74 ms |
9336 KB |
Output is correct |
57 |
Correct |
60 ms |
8244 KB |
Output is correct |
58 |
Correct |
71 ms |
8588 KB |
Output is correct |
59 |
Correct |
22 ms |
4652 KB |
Output is correct |
60 |
Correct |
58 ms |
8032 KB |
Output is correct |
61 |
Correct |
65 ms |
8548 KB |
Output is correct |
62 |
Correct |
90 ms |
8864 KB |
Output is correct |
63 |
Correct |
70 ms |
8788 KB |
Output is correct |
64 |
Correct |
94 ms |
8680 KB |
Output is correct |
65 |
Correct |
59 ms |
9292 KB |
Output is correct |
66 |
Correct |
57 ms |
9160 KB |
Output is correct |
67 |
Correct |
48 ms |
9048 KB |
Output is correct |
68 |
Correct |
54 ms |
9124 KB |
Output is correct |
69 |
Correct |
48 ms |
9084 KB |
Output is correct |
70 |
Correct |
51 ms |
9116 KB |
Output is correct |