//####################
//PowerPlant
//####################
#include<bits/stdc++.h>
using namespace std;
const int maxiN = 2*1e5;
bool isPowerPlant[maxiN];
vector<int> links[maxiN];
int dp[2][maxiN];
int ans = 0;
int solve(bool ataken,int node,int dad=-1){
if(dp[ataken][node]!=-1)return dp[ataken][node];
if(ataken==0){
int sum = 0;
int bestsub = 0;
for(int v:links[node])if(v!=dad){
int x = solve(0,v,node);
bestsub = max(bestsub,x);
sum += x;
ans = max(ans, isPowerPlant[node] + solve(1,v,node));
}
if(bestsub<1)bestsub=isPowerPlant[node];
if(bestsub>1)bestsub-=isPowerPlant[node];
ans = max(ans,max(sum-isPowerPlant[node],bestsub));
return dp[ataken][node] = max(sum-isPowerPlant[node],bestsub);
}else{
int sum = 0;
int bestsub = 0;
for(int v:links[node])if(v!=dad){
int x = solve(0,v,node);
bestsub = max(bestsub,x);
sum += x;
}
ans = max(ans, max(sum-isPowerPlant[node],bestsub));
return dp[ataken][node] = max(sum-isPowerPlant[node],bestsub);
}
}
signed main(){
ios::sync_with_stdio(false);
int n;cin>>n;
for(int i = 1;i<n;i++){
int a,b;cin>>a>>b;
a--;
b--;
links[a].push_back(b);
links[b].push_back(a);
}
fill(dp[0],dp[0]+n,-1);
fill(dp[1],dp[1]+n,-1);
string binary;cin>>binary;
for(int i= 0;i<n;i++)isPowerPlant[i] = (binary[i]=='1');
solve(0,0);
cout<<ans<<endl;
return 0;
};
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4960 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4960 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5252 KB |
Output is correct |
26 |
Correct |
2 ms |
5184 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5172 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
2 ms |
5248 KB |
Output is correct |
33 |
Correct |
3 ms |
5176 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
4 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5148 KB |
Output is correct |
41 |
Correct |
3 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
4 ms |
5212 KB |
Output is correct |
47 |
Correct |
4 ms |
5308 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5208 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
3 ms |
5212 KB |
Output is correct |
52 |
Correct |
3 ms |
5212 KB |
Output is correct |
53 |
Correct |
3 ms |
5124 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
3 ms |
5212 KB |
Output is correct |
56 |
Correct |
2 ms |
5264 KB |
Output is correct |
57 |
Correct |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5212 KB |
Output is correct |
60 |
Correct |
3 ms |
5252 KB |
Output is correct |
61 |
Correct |
3 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4960 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5252 KB |
Output is correct |
26 |
Correct |
2 ms |
5184 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5172 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
2 ms |
5248 KB |
Output is correct |
33 |
Correct |
3 ms |
5176 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
4 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5148 KB |
Output is correct |
41 |
Correct |
3 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
4 ms |
5212 KB |
Output is correct |
47 |
Correct |
4 ms |
5308 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5208 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
3 ms |
5212 KB |
Output is correct |
52 |
Correct |
3 ms |
5212 KB |
Output is correct |
53 |
Correct |
3 ms |
5124 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
3 ms |
5212 KB |
Output is correct |
56 |
Correct |
2 ms |
5264 KB |
Output is correct |
57 |
Correct |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5212 KB |
Output is correct |
60 |
Correct |
3 ms |
5252 KB |
Output is correct |
61 |
Correct |
3 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5180 KB |
Output is correct |
64 |
Correct |
75 ms |
16332 KB |
Output is correct |
65 |
Correct |
61 ms |
16272 KB |
Output is correct |
66 |
Correct |
73 ms |
16132 KB |
Output is correct |
67 |
Correct |
64 ms |
16276 KB |
Output is correct |
68 |
Correct |
64 ms |
16252 KB |
Output is correct |
69 |
Correct |
62 ms |
16312 KB |
Output is correct |
70 |
Correct |
59 ms |
16192 KB |
Output is correct |
71 |
Correct |
69 ms |
16288 KB |
Output is correct |
72 |
Correct |
62 ms |
16272 KB |
Output is correct |
73 |
Correct |
76 ms |
31376 KB |
Output is correct |
74 |
Correct |
81 ms |
31332 KB |
Output is correct |
75 |
Correct |
59 ms |
16024 KB |
Output is correct |
76 |
Correct |
75 ms |
16028 KB |
Output is correct |
77 |
Correct |
64 ms |
16044 KB |
Output is correct |
78 |
Correct |
60 ms |
16016 KB |
Output is correct |
79 |
Correct |
59 ms |
16024 KB |
Output is correct |
80 |
Correct |
60 ms |
16020 KB |
Output is correct |
81 |
Correct |
52 ms |
16556 KB |
Output is correct |
82 |
Correct |
57 ms |
16212 KB |
Output is correct |
83 |
Correct |
67 ms |
22232 KB |
Output is correct |
84 |
Correct |
68 ms |
24996 KB |
Output is correct |
85 |
Correct |
69 ms |
24472 KB |
Output is correct |
86 |
Correct |
61 ms |
17944 KB |
Output is correct |
87 |
Correct |
59 ms |
17168 KB |
Output is correct |
88 |
Correct |
59 ms |
17616 KB |
Output is correct |
89 |
Correct |
64 ms |
17568 KB |
Output is correct |
90 |
Correct |
69 ms |
17364 KB |
Output is correct |
91 |
Correct |
63 ms |
17516 KB |
Output is correct |
92 |
Correct |
70 ms |
18104 KB |
Output is correct |
93 |
Correct |
77 ms |
20836 KB |
Output is correct |
94 |
Correct |
58 ms |
22980 KB |
Output is correct |
95 |
Correct |
60 ms |
21324 KB |
Output is correct |
96 |
Correct |
61 ms |
19376 KB |
Output is correct |
97 |
Correct |
64 ms |
17476 KB |
Output is correct |
98 |
Correct |
58 ms |
16836 KB |
Output is correct |
99 |
Correct |
58 ms |
16580 KB |
Output is correct |
100 |
Correct |
71 ms |
20352 KB |
Output is correct |
101 |
Correct |
62 ms |
17936 KB |
Output is correct |
102 |
Correct |
60 ms |
16992 KB |
Output is correct |
103 |
Correct |
61 ms |
16672 KB |
Output is correct |
104 |
Correct |
43 ms |
17088 KB |
Output is correct |
105 |
Correct |
67 ms |
27648 KB |
Output is correct |
106 |
Correct |
46 ms |
17068 KB |
Output is correct |
107 |
Correct |
42 ms |
16836 KB |
Output is correct |
108 |
Correct |
109 ms |
32196 KB |
Output is correct |
109 |
Correct |
58 ms |
17096 KB |
Output is correct |
110 |
Correct |
74 ms |
32824 KB |
Output is correct |
111 |
Correct |
55 ms |
16032 KB |
Output is correct |