//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,have[N],dp[N],ans=0,root=1;
vector<int>g[N];
void dfs(int p,int lp){
int tans=0,mx=0,cnt=0,mh=0;
for (auto i:g[p]){
if (i!=lp){
dfs(i,p);
tans+=have[i];
mh=max(mh,have[i]);
if (have[i]) cnt++;
mx=max({mx,dp[i]});
}
}
// cout<<p<<" -> "<<mx<<" "<<mh<<' '<<tans<<" "<<have[p]<<'\n';
dp[p]=max({mx,mh+have[p]});
if (cnt<=1) dp[p]=max(dp[p],tans+have[p]);
else dp[p]=max(dp[p],tans-have[p]);
have[p]=max({have[p],tans-have[p],cnt-have[p]});
}
signed main(){
fast
cin>>n;
for (int i=1;i<n;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i=1;i<=n;i++){
char c; cin>>c;
have[i]=c-'0';
}
dfs(1,0);
// for (int i=1;i<=n;i++)
// cout<<dp[i]<<' ';
// cout<<'\n';
cout<<dp[1]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5040 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4976 KB |
Output is correct |
15 |
Correct |
4 ms |
5032 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5040 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4976 KB |
Output is correct |
15 |
Correct |
4 ms |
5032 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5036 KB |
Output is correct |
22 |
Correct |
5 ms |
5100 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5200 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5168 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5100 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5052 KB |
Output is correct |
36 |
Correct |
4 ms |
5044 KB |
Output is correct |
37 |
Correct |
4 ms |
5044 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
4 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5172 KB |
Output is correct |
42 |
Correct |
4 ms |
5176 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5100 KB |
Output is correct |
45 |
Correct |
3 ms |
5164 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
5 ms |
5180 KB |
Output is correct |
48 |
Correct |
3 ms |
5224 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5204 KB |
Output is correct |
51 |
Correct |
4 ms |
5204 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5204 KB |
Output is correct |
55 |
Correct |
3 ms |
5096 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
4 ms |
5228 KB |
Output is correct |
58 |
Correct |
3 ms |
5176 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5232 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5332 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5040 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4976 KB |
Output is correct |
15 |
Correct |
4 ms |
5032 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5036 KB |
Output is correct |
22 |
Correct |
5 ms |
5100 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5200 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5168 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5100 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5052 KB |
Output is correct |
36 |
Correct |
4 ms |
5044 KB |
Output is correct |
37 |
Correct |
4 ms |
5044 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
4 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5172 KB |
Output is correct |
42 |
Correct |
4 ms |
5176 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5100 KB |
Output is correct |
45 |
Correct |
3 ms |
5164 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
5 ms |
5180 KB |
Output is correct |
48 |
Correct |
3 ms |
5224 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5204 KB |
Output is correct |
51 |
Correct |
4 ms |
5204 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5204 KB |
Output is correct |
55 |
Correct |
3 ms |
5096 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
4 ms |
5228 KB |
Output is correct |
58 |
Correct |
3 ms |
5176 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5232 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5332 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
121 ms |
18096 KB |
Output is correct |
65 |
Correct |
175 ms |
18212 KB |
Output is correct |
66 |
Correct |
149 ms |
18212 KB |
Output is correct |
67 |
Correct |
126 ms |
18208 KB |
Output is correct |
68 |
Correct |
123 ms |
18196 KB |
Output is correct |
69 |
Correct |
121 ms |
18092 KB |
Output is correct |
70 |
Correct |
131 ms |
18100 KB |
Output is correct |
71 |
Correct |
151 ms |
18124 KB |
Output is correct |
72 |
Correct |
135 ms |
18148 KB |
Output is correct |
73 |
Correct |
140 ms |
34876 KB |
Output is correct |
74 |
Correct |
138 ms |
34852 KB |
Output is correct |
75 |
Correct |
144 ms |
19428 KB |
Output is correct |
76 |
Correct |
151 ms |
19432 KB |
Output is correct |
77 |
Correct |
127 ms |
18188 KB |
Output is correct |
78 |
Correct |
130 ms |
18192 KB |
Output is correct |
79 |
Correct |
121 ms |
18200 KB |
Output is correct |
80 |
Correct |
144 ms |
18120 KB |
Output is correct |
81 |
Correct |
96 ms |
18092 KB |
Output is correct |
82 |
Correct |
115 ms |
18068 KB |
Output is correct |
83 |
Correct |
146 ms |
25272 KB |
Output is correct |
84 |
Correct |
160 ms |
28352 KB |
Output is correct |
85 |
Correct |
154 ms |
27800 KB |
Output is correct |
86 |
Correct |
127 ms |
20044 KB |
Output is correct |
87 |
Correct |
127 ms |
19276 KB |
Output is correct |
88 |
Correct |
126 ms |
19344 KB |
Output is correct |
89 |
Correct |
117 ms |
19384 KB |
Output is correct |
90 |
Correct |
131 ms |
19040 KB |
Output is correct |
91 |
Correct |
142 ms |
19376 KB |
Output is correct |
92 |
Correct |
118 ms |
19788 KB |
Output is correct |
93 |
Correct |
110 ms |
22988 KB |
Output is correct |
94 |
Correct |
128 ms |
25316 KB |
Output is correct |
95 |
Correct |
125 ms |
23564 KB |
Output is correct |
96 |
Correct |
126 ms |
21044 KB |
Output is correct |
97 |
Correct |
121 ms |
18972 KB |
Output is correct |
98 |
Correct |
129 ms |
18292 KB |
Output is correct |
99 |
Correct |
139 ms |
18324 KB |
Output is correct |
100 |
Correct |
113 ms |
22396 KB |
Output is correct |
101 |
Correct |
116 ms |
19532 KB |
Output is correct |
102 |
Correct |
106 ms |
18536 KB |
Output is correct |
103 |
Correct |
156 ms |
18768 KB |
Output is correct |
104 |
Correct |
69 ms |
18736 KB |
Output is correct |
105 |
Correct |
136 ms |
30348 KB |
Output is correct |
106 |
Correct |
80 ms |
18612 KB |
Output is correct |
107 |
Correct |
68 ms |
18572 KB |
Output is correct |
108 |
Correct |
157 ms |
36040 KB |
Output is correct |
109 |
Correct |
79 ms |
18772 KB |
Output is correct |
110 |
Correct |
133 ms |
36444 KB |
Output is correct |
111 |
Correct |
111 ms |
18124 KB |
Output is correct |