#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
ll dp[200005][2];
vector<vll>adj;
string s;
ll solve(ll idx,ll par,bool ep){
if(dp[idx][ep] != -1){
return dp[idx][ep];
}
ll ans = s[idx - 1] - '0';
if(ep){
ll res = -ans;
for(auto x:adj[idx]){
if(x != par){
res += solve(x,idx,ep);
}
}
ans = max(ans,res);
}
else{
ans = 0;
for(auto x:adj[idx]){
if(x != par){
ans = max(ans,solve(x,idx,0));
}
}
if(s[idx - 1] == '1'){
ll res = 1;
for(auto x:adj[idx]){
if(x != par){
res = max(res,1 + solve(x,idx,1));
}
}
ans = max(ans,res);
}
ll res = -(s[idx - 1] - '0');
for(auto x:adj[idx]){
if(x != par){
res += solve(x,idx,1);
}
}
ans = max(ans,res);
}
return dp[idx][ep] = ans;
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
memset(dp,-1,sizeof dp);
adj.assign(n+5,vll());
f(i,0,n-1){
ll a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
cin>>s;
cout<<solve(1,0,0)<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3400 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3404 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3396 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3332 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3400 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3400 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3404 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3396 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3332 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3400 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
3 ms |
3540 KB |
Output is correct |
23 |
Correct |
3 ms |
3540 KB |
Output is correct |
24 |
Correct |
3 ms |
3540 KB |
Output is correct |
25 |
Correct |
2 ms |
3540 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
3 ms |
3536 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
2 ms |
3668 KB |
Output is correct |
30 |
Correct |
3 ms |
3672 KB |
Output is correct |
31 |
Correct |
3 ms |
3540 KB |
Output is correct |
32 |
Correct |
2 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
3540 KB |
Output is correct |
34 |
Correct |
3 ms |
3540 KB |
Output is correct |
35 |
Correct |
3 ms |
3540 KB |
Output is correct |
36 |
Correct |
4 ms |
3540 KB |
Output is correct |
37 |
Correct |
3 ms |
3540 KB |
Output is correct |
38 |
Correct |
2 ms |
3540 KB |
Output is correct |
39 |
Correct |
2 ms |
3540 KB |
Output is correct |
40 |
Correct |
3 ms |
3540 KB |
Output is correct |
41 |
Correct |
3 ms |
3668 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3540 KB |
Output is correct |
44 |
Correct |
4 ms |
3536 KB |
Output is correct |
45 |
Correct |
2 ms |
3540 KB |
Output is correct |
46 |
Correct |
2 ms |
3540 KB |
Output is correct |
47 |
Correct |
3 ms |
3568 KB |
Output is correct |
48 |
Correct |
2 ms |
3668 KB |
Output is correct |
49 |
Correct |
3 ms |
3668 KB |
Output is correct |
50 |
Correct |
2 ms |
3540 KB |
Output is correct |
51 |
Correct |
3 ms |
3536 KB |
Output is correct |
52 |
Correct |
3 ms |
3540 KB |
Output is correct |
53 |
Correct |
3 ms |
3540 KB |
Output is correct |
54 |
Correct |
3 ms |
3540 KB |
Output is correct |
55 |
Correct |
3 ms |
3540 KB |
Output is correct |
56 |
Correct |
2 ms |
3540 KB |
Output is correct |
57 |
Correct |
3 ms |
3664 KB |
Output is correct |
58 |
Correct |
3 ms |
3540 KB |
Output is correct |
59 |
Correct |
3 ms |
3540 KB |
Output is correct |
60 |
Correct |
3 ms |
3668 KB |
Output is correct |
61 |
Correct |
2 ms |
3540 KB |
Output is correct |
62 |
Correct |
3 ms |
3668 KB |
Output is correct |
63 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3400 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3404 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3396 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3332 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3400 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
3 ms |
3540 KB |
Output is correct |
23 |
Correct |
3 ms |
3540 KB |
Output is correct |
24 |
Correct |
3 ms |
3540 KB |
Output is correct |
25 |
Correct |
2 ms |
3540 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
3 ms |
3536 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
2 ms |
3668 KB |
Output is correct |
30 |
Correct |
3 ms |
3672 KB |
Output is correct |
31 |
Correct |
3 ms |
3540 KB |
Output is correct |
32 |
Correct |
2 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
3540 KB |
Output is correct |
34 |
Correct |
3 ms |
3540 KB |
Output is correct |
35 |
Correct |
3 ms |
3540 KB |
Output is correct |
36 |
Correct |
4 ms |
3540 KB |
Output is correct |
37 |
Correct |
3 ms |
3540 KB |
Output is correct |
38 |
Correct |
2 ms |
3540 KB |
Output is correct |
39 |
Correct |
2 ms |
3540 KB |
Output is correct |
40 |
Correct |
3 ms |
3540 KB |
Output is correct |
41 |
Correct |
3 ms |
3668 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3540 KB |
Output is correct |
44 |
Correct |
4 ms |
3536 KB |
Output is correct |
45 |
Correct |
2 ms |
3540 KB |
Output is correct |
46 |
Correct |
2 ms |
3540 KB |
Output is correct |
47 |
Correct |
3 ms |
3568 KB |
Output is correct |
48 |
Correct |
2 ms |
3668 KB |
Output is correct |
49 |
Correct |
3 ms |
3668 KB |
Output is correct |
50 |
Correct |
2 ms |
3540 KB |
Output is correct |
51 |
Correct |
3 ms |
3536 KB |
Output is correct |
52 |
Correct |
3 ms |
3540 KB |
Output is correct |
53 |
Correct |
3 ms |
3540 KB |
Output is correct |
54 |
Correct |
3 ms |
3540 KB |
Output is correct |
55 |
Correct |
3 ms |
3540 KB |
Output is correct |
56 |
Correct |
2 ms |
3540 KB |
Output is correct |
57 |
Correct |
3 ms |
3664 KB |
Output is correct |
58 |
Correct |
3 ms |
3540 KB |
Output is correct |
59 |
Correct |
3 ms |
3540 KB |
Output is correct |
60 |
Correct |
3 ms |
3668 KB |
Output is correct |
61 |
Correct |
2 ms |
3540 KB |
Output is correct |
62 |
Correct |
3 ms |
3668 KB |
Output is correct |
63 |
Correct |
3 ms |
3540 KB |
Output is correct |
64 |
Correct |
170 ms |
18692 KB |
Output is correct |
65 |
Correct |
145 ms |
18544 KB |
Output is correct |
66 |
Correct |
140 ms |
18624 KB |
Output is correct |
67 |
Correct |
141 ms |
18628 KB |
Output is correct |
68 |
Correct |
148 ms |
18668 KB |
Output is correct |
69 |
Correct |
158 ms |
18644 KB |
Output is correct |
70 |
Correct |
172 ms |
18624 KB |
Output is correct |
71 |
Correct |
185 ms |
18512 KB |
Output is correct |
72 |
Correct |
127 ms |
18572 KB |
Output is correct |
73 |
Correct |
187 ms |
32672 KB |
Output is correct |
74 |
Correct |
213 ms |
32728 KB |
Output is correct |
75 |
Correct |
142 ms |
19844 KB |
Output is correct |
76 |
Correct |
148 ms |
19836 KB |
Output is correct |
77 |
Correct |
149 ms |
18596 KB |
Output is correct |
78 |
Correct |
140 ms |
18588 KB |
Output is correct |
79 |
Correct |
141 ms |
18680 KB |
Output is correct |
80 |
Correct |
154 ms |
18532 KB |
Output is correct |
81 |
Correct |
146 ms |
18560 KB |
Output is correct |
82 |
Correct |
163 ms |
18488 KB |
Output is correct |
83 |
Correct |
175 ms |
24600 KB |
Output is correct |
84 |
Correct |
185 ms |
27288 KB |
Output is correct |
85 |
Correct |
181 ms |
26844 KB |
Output is correct |
86 |
Correct |
145 ms |
20116 KB |
Output is correct |
87 |
Correct |
160 ms |
19360 KB |
Output is correct |
88 |
Correct |
146 ms |
19528 KB |
Output is correct |
89 |
Correct |
142 ms |
19492 KB |
Output is correct |
90 |
Correct |
136 ms |
19324 KB |
Output is correct |
91 |
Correct |
160 ms |
19572 KB |
Output is correct |
92 |
Correct |
146 ms |
19872 KB |
Output is correct |
93 |
Correct |
161 ms |
22680 KB |
Output is correct |
94 |
Correct |
167 ms |
24700 KB |
Output is correct |
95 |
Correct |
126 ms |
23176 KB |
Output is correct |
96 |
Correct |
141 ms |
20976 KB |
Output is correct |
97 |
Correct |
153 ms |
19304 KB |
Output is correct |
98 |
Correct |
136 ms |
18756 KB |
Output is correct |
99 |
Correct |
143 ms |
18648 KB |
Output is correct |
100 |
Correct |
160 ms |
22188 KB |
Output is correct |
101 |
Correct |
145 ms |
19632 KB |
Output is correct |
102 |
Correct |
122 ms |
18820 KB |
Output is correct |
103 |
Correct |
125 ms |
18860 KB |
Output is correct |
104 |
Correct |
95 ms |
19096 KB |
Output is correct |
105 |
Correct |
178 ms |
28808 KB |
Output is correct |
106 |
Correct |
89 ms |
19100 KB |
Output is correct |
107 |
Correct |
95 ms |
19012 KB |
Output is correct |
108 |
Correct |
169 ms |
33668 KB |
Output is correct |
109 |
Correct |
83 ms |
19132 KB |
Output is correct |
110 |
Correct |
169 ms |
33896 KB |
Output is correct |
111 |
Correct |
174 ms |
18520 KB |
Output is correct |