#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN=3e5+5;
const int mod=998244353;
const int mxlogN=20;
const int mxK=26;
const int inf=2e9;
const int K=600;
vector<int> adj[mxN];
string s;
int dp[2][mxN];
int solve(int i, bool b, int p)
{
if(~dp[b][i]) return dp[b][i];
if(!b)
{
int ans=0, ans2=0, ans3=0;
for(int j:adj[i])
{
if(j==p) continue;
ans=max(ans,solve(j,0,i));
ans2=max(ans2,solve(j,1,i));
ans3+=solve(j,1,i);
}
ans=max(ans,ans3-(s[i]=='1'));
if(s[i]=='1') ans=max(ans,ans2+1);
return dp[b][i]=ans;
}
int ans=0, ans2=0;
if(s[i]=='1') ans=1;
for(int j:adj[i])
{
if(j==p) continue;
ans2+=solve(j,1,i);
}
ans=max(ans,ans2-(s[i]=='1'));
return dp[b][i]=ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for(int i=0; i<n; i++) dp[0][i]=dp[1][i]=-1;
for(int i=1; i<n; i++)
{
int u,v; cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
cout << solve(0,0,0) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7344 KB |
Output is correct |
10 |
Correct |
6 ms |
7264 KB |
Output is correct |
11 |
Correct |
6 ms |
7384 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7384 KB |
Output is correct |
21 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7344 KB |
Output is correct |
10 |
Correct |
6 ms |
7264 KB |
Output is correct |
11 |
Correct |
6 ms |
7384 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7384 KB |
Output is correct |
21 |
Correct |
4 ms |
7372 KB |
Output is correct |
22 |
Correct |
5 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7392 KB |
Output is correct |
24 |
Correct |
5 ms |
7380 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
6 ms |
7380 KB |
Output is correct |
27 |
Correct |
6 ms |
7380 KB |
Output is correct |
28 |
Correct |
5 ms |
7380 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7516 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7388 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7392 KB |
Output is correct |
35 |
Correct |
5 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7384 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
4 ms |
7512 KB |
Output is correct |
40 |
Correct |
6 ms |
7428 KB |
Output is correct |
41 |
Correct |
6 ms |
7508 KB |
Output is correct |
42 |
Correct |
7 ms |
7508 KB |
Output is correct |
43 |
Correct |
5 ms |
7380 KB |
Output is correct |
44 |
Correct |
5 ms |
7508 KB |
Output is correct |
45 |
Correct |
5 ms |
7508 KB |
Output is correct |
46 |
Correct |
5 ms |
7464 KB |
Output is correct |
47 |
Correct |
5 ms |
7476 KB |
Output is correct |
48 |
Correct |
5 ms |
7508 KB |
Output is correct |
49 |
Correct |
5 ms |
7476 KB |
Output is correct |
50 |
Correct |
5 ms |
7508 KB |
Output is correct |
51 |
Correct |
5 ms |
7508 KB |
Output is correct |
52 |
Correct |
5 ms |
7380 KB |
Output is correct |
53 |
Correct |
5 ms |
7508 KB |
Output is correct |
54 |
Correct |
5 ms |
7512 KB |
Output is correct |
55 |
Correct |
5 ms |
7388 KB |
Output is correct |
56 |
Correct |
5 ms |
7536 KB |
Output is correct |
57 |
Correct |
5 ms |
7640 KB |
Output is correct |
58 |
Correct |
5 ms |
7384 KB |
Output is correct |
59 |
Correct |
6 ms |
7380 KB |
Output is correct |
60 |
Correct |
6 ms |
7508 KB |
Output is correct |
61 |
Correct |
6 ms |
7392 KB |
Output is correct |
62 |
Correct |
5 ms |
7516 KB |
Output is correct |
63 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7344 KB |
Output is correct |
10 |
Correct |
6 ms |
7264 KB |
Output is correct |
11 |
Correct |
6 ms |
7384 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7384 KB |
Output is correct |
21 |
Correct |
4 ms |
7372 KB |
Output is correct |
22 |
Correct |
5 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7392 KB |
Output is correct |
24 |
Correct |
5 ms |
7380 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
6 ms |
7380 KB |
Output is correct |
27 |
Correct |
6 ms |
7380 KB |
Output is correct |
28 |
Correct |
5 ms |
7380 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7516 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7388 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7392 KB |
Output is correct |
35 |
Correct |
5 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7384 KB |
Output is correct |
38 |
Correct |
4 ms |
7380 KB |
Output is correct |
39 |
Correct |
4 ms |
7512 KB |
Output is correct |
40 |
Correct |
6 ms |
7428 KB |
Output is correct |
41 |
Correct |
6 ms |
7508 KB |
Output is correct |
42 |
Correct |
7 ms |
7508 KB |
Output is correct |
43 |
Correct |
5 ms |
7380 KB |
Output is correct |
44 |
Correct |
5 ms |
7508 KB |
Output is correct |
45 |
Correct |
5 ms |
7508 KB |
Output is correct |
46 |
Correct |
5 ms |
7464 KB |
Output is correct |
47 |
Correct |
5 ms |
7476 KB |
Output is correct |
48 |
Correct |
5 ms |
7508 KB |
Output is correct |
49 |
Correct |
5 ms |
7476 KB |
Output is correct |
50 |
Correct |
5 ms |
7508 KB |
Output is correct |
51 |
Correct |
5 ms |
7508 KB |
Output is correct |
52 |
Correct |
5 ms |
7380 KB |
Output is correct |
53 |
Correct |
5 ms |
7508 KB |
Output is correct |
54 |
Correct |
5 ms |
7512 KB |
Output is correct |
55 |
Correct |
5 ms |
7388 KB |
Output is correct |
56 |
Correct |
5 ms |
7536 KB |
Output is correct |
57 |
Correct |
5 ms |
7640 KB |
Output is correct |
58 |
Correct |
5 ms |
7384 KB |
Output is correct |
59 |
Correct |
6 ms |
7380 KB |
Output is correct |
60 |
Correct |
6 ms |
7508 KB |
Output is correct |
61 |
Correct |
6 ms |
7392 KB |
Output is correct |
62 |
Correct |
5 ms |
7516 KB |
Output is correct |
63 |
Correct |
4 ms |
7380 KB |
Output is correct |
64 |
Correct |
131 ms |
18360 KB |
Output is correct |
65 |
Correct |
175 ms |
18460 KB |
Output is correct |
66 |
Correct |
128 ms |
18468 KB |
Output is correct |
67 |
Correct |
155 ms |
18412 KB |
Output is correct |
68 |
Correct |
129 ms |
18396 KB |
Output is correct |
69 |
Correct |
166 ms |
18348 KB |
Output is correct |
70 |
Correct |
152 ms |
18432 KB |
Output is correct |
71 |
Correct |
126 ms |
18360 KB |
Output is correct |
72 |
Correct |
147 ms |
18416 KB |
Output is correct |
73 |
Correct |
167 ms |
33492 KB |
Output is correct |
74 |
Correct |
163 ms |
33420 KB |
Output is correct |
75 |
Correct |
151 ms |
18260 KB |
Output is correct |
76 |
Correct |
140 ms |
18392 KB |
Output is correct |
77 |
Correct |
125 ms |
18360 KB |
Output is correct |
78 |
Correct |
166 ms |
18324 KB |
Output is correct |
79 |
Correct |
153 ms |
18360 KB |
Output is correct |
80 |
Correct |
145 ms |
18336 KB |
Output is correct |
81 |
Correct |
100 ms |
18628 KB |
Output is correct |
82 |
Correct |
114 ms |
18444 KB |
Output is correct |
83 |
Correct |
154 ms |
24400 KB |
Output is correct |
84 |
Correct |
162 ms |
27088 KB |
Output is correct |
85 |
Correct |
153 ms |
26604 KB |
Output is correct |
86 |
Correct |
171 ms |
19892 KB |
Output is correct |
87 |
Correct |
132 ms |
19256 KB |
Output is correct |
88 |
Correct |
164 ms |
19648 KB |
Output is correct |
89 |
Correct |
200 ms |
19680 KB |
Output is correct |
90 |
Correct |
119 ms |
19360 KB |
Output is correct |
91 |
Correct |
140 ms |
19720 KB |
Output is correct |
92 |
Correct |
164 ms |
20024 KB |
Output is correct |
93 |
Correct |
168 ms |
23044 KB |
Output is correct |
94 |
Correct |
129 ms |
25148 KB |
Output is correct |
95 |
Correct |
122 ms |
23492 KB |
Output is correct |
96 |
Correct |
172 ms |
21376 KB |
Output is correct |
97 |
Correct |
128 ms |
19456 KB |
Output is correct |
98 |
Correct |
130 ms |
18896 KB |
Output is correct |
99 |
Correct |
160 ms |
18800 KB |
Output is correct |
100 |
Correct |
187 ms |
22516 KB |
Output is correct |
101 |
Correct |
166 ms |
20088 KB |
Output is correct |
102 |
Correct |
179 ms |
19132 KB |
Output is correct |
103 |
Correct |
172 ms |
18904 KB |
Output is correct |
104 |
Correct |
114 ms |
19132 KB |
Output is correct |
105 |
Correct |
185 ms |
29600 KB |
Output is correct |
106 |
Correct |
102 ms |
19008 KB |
Output is correct |
107 |
Correct |
94 ms |
18992 KB |
Output is correct |
108 |
Correct |
204 ms |
34328 KB |
Output is correct |
109 |
Correct |
105 ms |
19132 KB |
Output is correct |
110 |
Correct |
216 ms |
34760 KB |
Output is correct |
111 |
Correct |
156 ms |
18292 KB |
Output is correct |