#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int>vi;
#define pb push_back
#define sz(v) (int)v.size()
#define all(x) begin(x),end(x)
#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=b-1; i>=a; i--)
void IO(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
//-----------------------------------------
void ckmin(int &x, int y){
x=min(x,y);
}
const int INF=1e7;
const int MX=1e5+10;
int N,memo[MX][2][2];
vi a(MX),adj[MX];
int solve(int u, int p, int i, int j){
int &ind=memo[u][i][j];
if(ind!=-1) return ind;
vi val(2,0); val[1]=INF;
for(int v: adj[u]) if(v!=p){
vi new_val(2,INF);
ckmin(new_val[0],val[1]+solve(v,u,j,1));
ckmin(new_val[1],val[0]+solve(v,u,j,1));
ckmin(new_val[0],val[0]+solve(v,u,j,0));
ckmin(new_val[1],val[1]+solve(v,u,j,0));
val.assign(all(new_val));
}
int state=((a[u]^i)^j);
int ans=j+val[state];
return ind=ans;
}
int main(){
IO();
cin>>N;
FOR(i,0,N-1){
int u,v; cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
FOR(i,1,N+1) cin>>a[i];
memset(memo,-1,sizeof(memo));
int ans=min(solve(1,1,0,1),solve(1,1,0,0));
if(ans>=INF) cout << "impossible" << endl;
else cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4556 KB |
Output is correct |
2 |
Correct |
3 ms |
4588 KB |
Output is correct |
3 |
Correct |
3 ms |
4556 KB |
Output is correct |
4 |
Correct |
3 ms |
4556 KB |
Output is correct |
5 |
Correct |
3 ms |
4556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4556 KB |
Output is correct |
2 |
Correct |
3 ms |
4588 KB |
Output is correct |
3 |
Correct |
3 ms |
4556 KB |
Output is correct |
4 |
Correct |
3 ms |
4556 KB |
Output is correct |
5 |
Correct |
3 ms |
4556 KB |
Output is correct |
6 |
Correct |
3 ms |
4584 KB |
Output is correct |
7 |
Correct |
3 ms |
4556 KB |
Output is correct |
8 |
Correct |
3 ms |
4556 KB |
Output is correct |
9 |
Correct |
3 ms |
4588 KB |
Output is correct |
10 |
Correct |
3 ms |
4556 KB |
Output is correct |
11 |
Correct |
3 ms |
4596 KB |
Output is correct |
12 |
Correct |
3 ms |
4584 KB |
Output is correct |
13 |
Correct |
3 ms |
4592 KB |
Output is correct |
14 |
Correct |
3 ms |
4556 KB |
Output is correct |
15 |
Correct |
3 ms |
4588 KB |
Output is correct |
16 |
Correct |
3 ms |
4556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
32360 KB |
Output is correct |
2 |
Correct |
218 ms |
32036 KB |
Output is correct |
3 |
Correct |
208 ms |
32520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
32404 KB |
Output is correct |
2 |
Correct |
234 ms |
32024 KB |
Output is correct |
3 |
Correct |
262 ms |
32476 KB |
Output is correct |
4 |
Correct |
171 ms |
8516 KB |
Output is correct |
5 |
Correct |
210 ms |
8932 KB |
Output is correct |
6 |
Correct |
222 ms |
9072 KB |
Output is correct |
7 |
Correct |
4 ms |
4580 KB |
Output is correct |
8 |
Correct |
54 ms |
6172 KB |
Output is correct |
9 |
Correct |
210 ms |
9300 KB |
Output is correct |
10 |
Correct |
191 ms |
9328 KB |
Output is correct |
11 |
Correct |
196 ms |
11000 KB |
Output is correct |
12 |
Correct |
213 ms |
11252 KB |
Output is correct |
13 |
Correct |
214 ms |
8764 KB |
Output is correct |
14 |
Correct |
179 ms |
9012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4556 KB |
Output is correct |
2 |
Correct |
3 ms |
4588 KB |
Output is correct |
3 |
Correct |
3 ms |
4556 KB |
Output is correct |
4 |
Correct |
3 ms |
4556 KB |
Output is correct |
5 |
Correct |
3 ms |
4556 KB |
Output is correct |
6 |
Correct |
3 ms |
4584 KB |
Output is correct |
7 |
Correct |
3 ms |
4556 KB |
Output is correct |
8 |
Correct |
3 ms |
4556 KB |
Output is correct |
9 |
Correct |
3 ms |
4588 KB |
Output is correct |
10 |
Correct |
3 ms |
4556 KB |
Output is correct |
11 |
Correct |
3 ms |
4596 KB |
Output is correct |
12 |
Correct |
3 ms |
4584 KB |
Output is correct |
13 |
Correct |
3 ms |
4592 KB |
Output is correct |
14 |
Correct |
3 ms |
4556 KB |
Output is correct |
15 |
Correct |
3 ms |
4588 KB |
Output is correct |
16 |
Correct |
3 ms |
4556 KB |
Output is correct |
17 |
Correct |
237 ms |
32360 KB |
Output is correct |
18 |
Correct |
218 ms |
32036 KB |
Output is correct |
19 |
Correct |
208 ms |
32520 KB |
Output is correct |
20 |
Correct |
190 ms |
32404 KB |
Output is correct |
21 |
Correct |
234 ms |
32024 KB |
Output is correct |
22 |
Correct |
262 ms |
32476 KB |
Output is correct |
23 |
Correct |
171 ms |
8516 KB |
Output is correct |
24 |
Correct |
210 ms |
8932 KB |
Output is correct |
25 |
Correct |
222 ms |
9072 KB |
Output is correct |
26 |
Correct |
4 ms |
4580 KB |
Output is correct |
27 |
Correct |
54 ms |
6172 KB |
Output is correct |
28 |
Correct |
210 ms |
9300 KB |
Output is correct |
29 |
Correct |
191 ms |
9328 KB |
Output is correct |
30 |
Correct |
196 ms |
11000 KB |
Output is correct |
31 |
Correct |
213 ms |
11252 KB |
Output is correct |
32 |
Correct |
214 ms |
8764 KB |
Output is correct |
33 |
Correct |
179 ms |
9012 KB |
Output is correct |
34 |
Correct |
3 ms |
4588 KB |
Output is correct |
35 |
Correct |
3 ms |
4556 KB |
Output is correct |
36 |
Correct |
3 ms |
4556 KB |
Output is correct |
37 |
Correct |
3 ms |
4556 KB |
Output is correct |
38 |
Correct |
3 ms |
4544 KB |
Output is correct |
39 |
Correct |
3 ms |
4556 KB |
Output is correct |
40 |
Correct |
3 ms |
4556 KB |
Output is correct |
41 |
Correct |
3 ms |
4516 KB |
Output is correct |
42 |
Correct |
3 ms |
4588 KB |
Output is correct |
43 |
Correct |
3 ms |
4556 KB |
Output is correct |
44 |
Correct |
3 ms |
4556 KB |
Output is correct |
45 |
Correct |
211 ms |
32292 KB |
Output is correct |
46 |
Correct |
208 ms |
32012 KB |
Output is correct |
47 |
Correct |
241 ms |
32508 KB |
Output is correct |
48 |
Correct |
184 ms |
8712 KB |
Output is correct |
49 |
Correct |
245 ms |
8900 KB |
Output is correct |
50 |
Correct |
200 ms |
9028 KB |
Output is correct |
51 |
Correct |
4 ms |
4556 KB |
Output is correct |
52 |
Correct |
52 ms |
6216 KB |
Output is correct |
53 |
Correct |
193 ms |
9312 KB |
Output is correct |
54 |
Correct |
227 ms |
9328 KB |
Output is correct |
55 |
Correct |
215 ms |
10948 KB |
Output is correct |
56 |
Correct |
198 ms |
11224 KB |
Output is correct |
57 |
Correct |
180 ms |
8776 KB |
Output is correct |
58 |
Correct |
210 ms |
9064 KB |
Output is correct |
59 |
Correct |
60 ms |
6164 KB |
Output is correct |
60 |
Correct |
195 ms |
8988 KB |
Output is correct |
61 |
Correct |
195 ms |
9156 KB |
Output is correct |
62 |
Correct |
209 ms |
9288 KB |
Output is correct |
63 |
Correct |
178 ms |
9412 KB |
Output is correct |
64 |
Correct |
181 ms |
9224 KB |
Output is correct |
65 |
Correct |
176 ms |
9548 KB |
Output is correct |
66 |
Correct |
185 ms |
9616 KB |
Output is correct |
67 |
Correct |
161 ms |
9480 KB |
Output is correct |
68 |
Correct |
170 ms |
9404 KB |
Output is correct |
69 |
Correct |
167 ms |
9404 KB |
Output is correct |
70 |
Correct |
140 ms |
9432 KB |
Output is correct |