#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=2e5;
vector<int> e[N+10];
int dp[N+10][2];
int t[N+10];
void dfs(int x,int p)
{
for(auto v:e[x])
{
if(v!=p)
dfs(v,x);
}
int s1=0;
for(auto v:e[x])
{
if(v!=p)
s1+=dp[v][1];
}
// dp[0]
dp[x][0]=t[x];
for(auto v:e[x])
{
if(v!=p)
dp[x][0]=max({dp[x][0],dp[v][0],dp[v][1]+t[x]});
}
dp[x][0]=max(dp[x][0],s1-t[x]);
// dp[1]
dp[x][1]=t[x];
for(auto v:e[x])
{
if(v!=p)
dp[x][1]=max(dp[x][1],dp[v][1]-t[x]);
}
dp[x][1]=max(dp[x][1],s1-t[x]);
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
t[i]=c-'0';
}
dfs(1,0);
cout<<dp[1][0]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5012 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5012 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5024 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5224 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5028 KB |
Output is correct |
32 |
Correct |
4 ms |
5024 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5028 KB |
Output is correct |
36 |
Correct |
4 ms |
5100 KB |
Output is correct |
37 |
Correct |
4 ms |
5024 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5160 KB |
Output is correct |
40 |
Correct |
4 ms |
5060 KB |
Output is correct |
41 |
Correct |
4 ms |
5196 KB |
Output is correct |
42 |
Correct |
4 ms |
5148 KB |
Output is correct |
43 |
Correct |
4 ms |
5032 KB |
Output is correct |
44 |
Correct |
5 ms |
5156 KB |
Output is correct |
45 |
Correct |
5 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5152 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5068 KB |
Output is correct |
49 |
Correct |
4 ms |
5068 KB |
Output is correct |
50 |
Correct |
4 ms |
5156 KB |
Output is correct |
51 |
Correct |
4 ms |
5160 KB |
Output is correct |
52 |
Correct |
4 ms |
5068 KB |
Output is correct |
53 |
Correct |
4 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
4 ms |
5024 KB |
Output is correct |
57 |
Correct |
4 ms |
5196 KB |
Output is correct |
58 |
Correct |
4 ms |
5068 KB |
Output is correct |
59 |
Correct |
4 ms |
5068 KB |
Output is correct |
60 |
Correct |
4 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5012 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5024 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5224 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5028 KB |
Output is correct |
32 |
Correct |
4 ms |
5024 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5028 KB |
Output is correct |
36 |
Correct |
4 ms |
5100 KB |
Output is correct |
37 |
Correct |
4 ms |
5024 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5160 KB |
Output is correct |
40 |
Correct |
4 ms |
5060 KB |
Output is correct |
41 |
Correct |
4 ms |
5196 KB |
Output is correct |
42 |
Correct |
4 ms |
5148 KB |
Output is correct |
43 |
Correct |
4 ms |
5032 KB |
Output is correct |
44 |
Correct |
5 ms |
5156 KB |
Output is correct |
45 |
Correct |
5 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5152 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5068 KB |
Output is correct |
49 |
Correct |
4 ms |
5068 KB |
Output is correct |
50 |
Correct |
4 ms |
5156 KB |
Output is correct |
51 |
Correct |
4 ms |
5160 KB |
Output is correct |
52 |
Correct |
4 ms |
5068 KB |
Output is correct |
53 |
Correct |
4 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
4 ms |
5024 KB |
Output is correct |
57 |
Correct |
4 ms |
5196 KB |
Output is correct |
58 |
Correct |
4 ms |
5068 KB |
Output is correct |
59 |
Correct |
4 ms |
5068 KB |
Output is correct |
60 |
Correct |
4 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
64 |
Correct |
152 ms |
16556 KB |
Output is correct |
65 |
Correct |
173 ms |
16544 KB |
Output is correct |
66 |
Correct |
169 ms |
16436 KB |
Output is correct |
67 |
Correct |
156 ms |
16436 KB |
Output is correct |
68 |
Correct |
163 ms |
16432 KB |
Output is correct |
69 |
Correct |
151 ms |
16324 KB |
Output is correct |
70 |
Correct |
148 ms |
16424 KB |
Output is correct |
71 |
Correct |
165 ms |
16408 KB |
Output is correct |
72 |
Correct |
165 ms |
16324 KB |
Output is correct |
73 |
Correct |
170 ms |
26552 KB |
Output is correct |
74 |
Correct |
170 ms |
26360 KB |
Output is correct |
75 |
Correct |
143 ms |
16316 KB |
Output is correct |
76 |
Correct |
155 ms |
16324 KB |
Output is correct |
77 |
Correct |
152 ms |
16324 KB |
Output is correct |
78 |
Correct |
147 ms |
16288 KB |
Output is correct |
79 |
Correct |
149 ms |
16312 KB |
Output is correct |
80 |
Correct |
150 ms |
16292 KB |
Output is correct |
81 |
Correct |
129 ms |
16608 KB |
Output is correct |
82 |
Correct |
137 ms |
16448 KB |
Output is correct |
83 |
Correct |
161 ms |
20504 KB |
Output is correct |
84 |
Correct |
162 ms |
22180 KB |
Output is correct |
85 |
Correct |
161 ms |
21956 KB |
Output is correct |
86 |
Correct |
148 ms |
17492 KB |
Output is correct |
87 |
Correct |
151 ms |
17048 KB |
Output is correct |
88 |
Correct |
154 ms |
17452 KB |
Output is correct |
89 |
Correct |
161 ms |
17552 KB |
Output is correct |
90 |
Correct |
139 ms |
17424 KB |
Output is correct |
91 |
Correct |
137 ms |
17596 KB |
Output is correct |
92 |
Correct |
134 ms |
17732 KB |
Output is correct |
93 |
Correct |
144 ms |
19548 KB |
Output is correct |
94 |
Correct |
137 ms |
20920 KB |
Output is correct |
95 |
Correct |
137 ms |
20000 KB |
Output is correct |
96 |
Correct |
144 ms |
18584 KB |
Output is correct |
97 |
Correct |
132 ms |
17304 KB |
Output is correct |
98 |
Correct |
149 ms |
16860 KB |
Output is correct |
99 |
Correct |
159 ms |
16724 KB |
Output is correct |
100 |
Correct |
157 ms |
19268 KB |
Output is correct |
101 |
Correct |
144 ms |
17744 KB |
Output is correct |
102 |
Correct |
130 ms |
17024 KB |
Output is correct |
103 |
Correct |
178 ms |
16964 KB |
Output is correct |
104 |
Correct |
97 ms |
17200 KB |
Output is correct |
105 |
Correct |
188 ms |
23836 KB |
Output is correct |
106 |
Correct |
85 ms |
16952 KB |
Output is correct |
107 |
Correct |
88 ms |
17112 KB |
Output is correct |
108 |
Correct |
164 ms |
27076 KB |
Output is correct |
109 |
Correct |
104 ms |
17128 KB |
Output is correct |
110 |
Correct |
186 ms |
27360 KB |
Output is correct |
111 |
Correct |
143 ms |
16380 KB |
Output is correct |