#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
int dp[maxn],res=0,n,val[maxn];
vector<int>adj[maxn];
void dfs(int u=1,int par=0){
for(auto x:adj[u]){
if(x!=par){
dfs(x,u);
dp[u]+=dp[x];
res=max(res,dp[x]+val[u]);
}
}
dp[u]=max(dp[u]+val[u]*-1,val[u]);
return ;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
val[i]=c-'0';
}
dfs(1);
res=max(res,dp[1]);
cout<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
5160 KB |
Output is correct |
11 |
Correct |
1 ms |
5168 KB |
Output is correct |
12 |
Correct |
1 ms |
4952 KB |
Output is correct |
13 |
Correct |
1 ms |
4972 KB |
Output is correct |
14 |
Correct |
1 ms |
5168 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
5164 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
5160 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
5160 KB |
Output is correct |
11 |
Correct |
1 ms |
5168 KB |
Output is correct |
12 |
Correct |
1 ms |
4952 KB |
Output is correct |
13 |
Correct |
1 ms |
4972 KB |
Output is correct |
14 |
Correct |
1 ms |
5168 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
5164 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
5160 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5176 KB |
Output is correct |
25 |
Correct |
2 ms |
5180 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5128 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5252 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5228 KB |
Output is correct |
36 |
Correct |
2 ms |
5208 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5180 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5228 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5176 KB |
Output is correct |
45 |
Correct |
2 ms |
5212 KB |
Output is correct |
46 |
Correct |
2 ms |
5212 KB |
Output is correct |
47 |
Correct |
2 ms |
5876 KB |
Output is correct |
48 |
Correct |
2 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5224 KB |
Output is correct |
52 |
Correct |
2 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
2 ms |
5212 KB |
Output is correct |
56 |
Correct |
1 ms |
5212 KB |
Output is correct |
57 |
Correct |
2 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
2 ms |
5212 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5380 KB |
Output is correct |
63 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
5160 KB |
Output is correct |
11 |
Correct |
1 ms |
5168 KB |
Output is correct |
12 |
Correct |
1 ms |
4952 KB |
Output is correct |
13 |
Correct |
1 ms |
4972 KB |
Output is correct |
14 |
Correct |
1 ms |
5168 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
5164 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
5160 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5176 KB |
Output is correct |
25 |
Correct |
2 ms |
5180 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5128 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5252 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5228 KB |
Output is correct |
36 |
Correct |
2 ms |
5208 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5180 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5228 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5176 KB |
Output is correct |
45 |
Correct |
2 ms |
5212 KB |
Output is correct |
46 |
Correct |
2 ms |
5212 KB |
Output is correct |
47 |
Correct |
2 ms |
5876 KB |
Output is correct |
48 |
Correct |
2 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5224 KB |
Output is correct |
52 |
Correct |
2 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
2 ms |
5212 KB |
Output is correct |
56 |
Correct |
1 ms |
5212 KB |
Output is correct |
57 |
Correct |
2 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
2 ms |
5212 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5380 KB |
Output is correct |
63 |
Correct |
2 ms |
5212 KB |
Output is correct |
64 |
Correct |
62 ms |
15600 KB |
Output is correct |
65 |
Correct |
67 ms |
15784 KB |
Output is correct |
66 |
Correct |
62 ms |
15588 KB |
Output is correct |
67 |
Correct |
94 ms |
15688 KB |
Output is correct |
68 |
Correct |
57 ms |
15592 KB |
Output is correct |
69 |
Correct |
75 ms |
15952 KB |
Output is correct |
70 |
Correct |
58 ms |
15700 KB |
Output is correct |
71 |
Correct |
78 ms |
15620 KB |
Output is correct |
72 |
Correct |
68 ms |
15720 KB |
Output is correct |
73 |
Correct |
77 ms |
28240 KB |
Output is correct |
74 |
Correct |
94 ms |
28804 KB |
Output is correct |
75 |
Correct |
77 ms |
15652 KB |
Output is correct |
76 |
Correct |
61 ms |
15512 KB |
Output is correct |
77 |
Correct |
56 ms |
15512 KB |
Output is correct |
78 |
Correct |
59 ms |
15444 KB |
Output is correct |
79 |
Correct |
57 ms |
15444 KB |
Output is correct |
80 |
Correct |
66 ms |
15560 KB |
Output is correct |
81 |
Correct |
52 ms |
15816 KB |
Output is correct |
82 |
Correct |
56 ms |
15696 KB |
Output is correct |
83 |
Correct |
88 ms |
21004 KB |
Output is correct |
84 |
Correct |
70 ms |
22864 KB |
Output is correct |
85 |
Correct |
71 ms |
22692 KB |
Output is correct |
86 |
Correct |
61 ms |
16980 KB |
Output is correct |
87 |
Correct |
74 ms |
16428 KB |
Output is correct |
88 |
Correct |
73 ms |
16952 KB |
Output is correct |
89 |
Correct |
61 ms |
16988 KB |
Output is correct |
90 |
Correct |
66 ms |
16716 KB |
Output is correct |
91 |
Correct |
80 ms |
16892 KB |
Output is correct |
92 |
Correct |
80 ms |
17228 KB |
Output is correct |
93 |
Correct |
61 ms |
19664 KB |
Output is correct |
94 |
Correct |
65 ms |
21372 KB |
Output is correct |
95 |
Correct |
59 ms |
20016 KB |
Output is correct |
96 |
Correct |
82 ms |
18344 KB |
Output is correct |
97 |
Correct |
56 ms |
16596 KB |
Output is correct |
98 |
Correct |
58 ms |
16312 KB |
Output is correct |
99 |
Correct |
75 ms |
15956 KB |
Output is correct |
100 |
Correct |
67 ms |
19300 KB |
Output is correct |
101 |
Correct |
63 ms |
17236 KB |
Output is correct |
102 |
Correct |
59 ms |
16540 KB |
Output is correct |
103 |
Correct |
61 ms |
16212 KB |
Output is correct |
104 |
Correct |
41 ms |
16328 KB |
Output is correct |
105 |
Correct |
78 ms |
25212 KB |
Output is correct |
106 |
Correct |
47 ms |
16224 KB |
Output is correct |
107 |
Correct |
42 ms |
16328 KB |
Output is correct |
108 |
Correct |
75 ms |
29124 KB |
Output is correct |
109 |
Correct |
46 ms |
16340 KB |
Output is correct |
110 |
Correct |
74 ms |
29292 KB |
Output is correct |
111 |
Correct |
71 ms |
15536 KB |
Output is correct |