#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=2e5+10;
int dp[N][2],t[N];
vi graf[N];
void dfs(int v,int o){
int xd=0,xd2=0,xd3=0;
for(auto u:graf[v]){
if(u==o) continue;
dfs(u,v);
xd=max(xd,dp[u][0]);
xd2=max(xd2,dp[u][1]);
xd3+=dp[u][1];
}
if(t[v]) dp[v][0]=max({xd3-1,xd,xd2+1,1}),dp[v][1]=max({1,xd3-1});
else dp[v][0]=max({xd3,xd,0}),dp[v][1]=xd3;
}
void solve(){
int n;
cin>>n;
rep(i,n-1){
int a,b;
cin>>a>>b;
graf[a].push_back(b),graf[b].push_back(a);
}
rep(i,n){
char a;
cin>>a;
t[i+1]=int(a)-48;
}
dfs(1,1);
cout<<dp[1][0]<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 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 |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5136 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
5 ms |
5144 KB |
Output is correct |
34 |
Correct |
4 ms |
5100 KB |
Output is correct |
35 |
Correct |
5 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5032 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5160 KB |
Output is correct |
42 |
Correct |
3 ms |
5080 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5160 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
3 ms |
5156 KB |
Output is correct |
49 |
Correct |
3 ms |
5196 KB |
Output is correct |
50 |
Correct |
4 ms |
5064 KB |
Output is correct |
51 |
Correct |
3 ms |
5160 KB |
Output is correct |
52 |
Correct |
5 ms |
5068 KB |
Output is correct |
53 |
Correct |
5 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
5164 KB |
Output is correct |
56 |
Correct |
4 ms |
5068 KB |
Output is correct |
57 |
Correct |
3 ms |
5292 KB |
Output is correct |
58 |
Correct |
3 ms |
5028 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5160 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5220 KB |
Output is correct |
63 |
Correct |
3 ms |
5120 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 |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5136 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
5 ms |
5144 KB |
Output is correct |
34 |
Correct |
4 ms |
5100 KB |
Output is correct |
35 |
Correct |
5 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5032 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5160 KB |
Output is correct |
42 |
Correct |
3 ms |
5080 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5160 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
3 ms |
5156 KB |
Output is correct |
49 |
Correct |
3 ms |
5196 KB |
Output is correct |
50 |
Correct |
4 ms |
5064 KB |
Output is correct |
51 |
Correct |
3 ms |
5160 KB |
Output is correct |
52 |
Correct |
5 ms |
5068 KB |
Output is correct |
53 |
Correct |
5 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
5164 KB |
Output is correct |
56 |
Correct |
4 ms |
5068 KB |
Output is correct |
57 |
Correct |
3 ms |
5292 KB |
Output is correct |
58 |
Correct |
3 ms |
5028 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5160 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5220 KB |
Output is correct |
63 |
Correct |
3 ms |
5120 KB |
Output is correct |
64 |
Correct |
156 ms |
17408 KB |
Output is correct |
65 |
Correct |
134 ms |
17348 KB |
Output is correct |
66 |
Correct |
126 ms |
17396 KB |
Output is correct |
67 |
Correct |
117 ms |
17312 KB |
Output is correct |
68 |
Correct |
124 ms |
17428 KB |
Output is correct |
69 |
Correct |
134 ms |
17476 KB |
Output is correct |
70 |
Correct |
125 ms |
17396 KB |
Output is correct |
71 |
Correct |
148 ms |
17444 KB |
Output is correct |
72 |
Correct |
119 ms |
17348 KB |
Output is correct |
73 |
Correct |
138 ms |
31540 KB |
Output is correct |
74 |
Correct |
154 ms |
31416 KB |
Output is correct |
75 |
Correct |
127 ms |
18628 KB |
Output is correct |
76 |
Correct |
130 ms |
18816 KB |
Output is correct |
77 |
Correct |
128 ms |
17324 KB |
Output is correct |
78 |
Correct |
163 ms |
17344 KB |
Output is correct |
79 |
Correct |
124 ms |
17340 KB |
Output is correct |
80 |
Correct |
118 ms |
17348 KB |
Output is correct |
81 |
Correct |
112 ms |
17352 KB |
Output is correct |
82 |
Correct |
119 ms |
17304 KB |
Output is correct |
83 |
Correct |
138 ms |
23504 KB |
Output is correct |
84 |
Correct |
138 ms |
26068 KB |
Output is correct |
85 |
Correct |
149 ms |
25632 KB |
Output is correct |
86 |
Correct |
132 ms |
19176 KB |
Output is correct |
87 |
Correct |
123 ms |
18332 KB |
Output is correct |
88 |
Correct |
116 ms |
18344 KB |
Output is correct |
89 |
Correct |
133 ms |
18416 KB |
Output is correct |
90 |
Correct |
123 ms |
18208 KB |
Output is correct |
91 |
Correct |
108 ms |
18372 KB |
Output is correct |
92 |
Correct |
122 ms |
18756 KB |
Output is correct |
93 |
Correct |
110 ms |
21504 KB |
Output is correct |
94 |
Correct |
106 ms |
23524 KB |
Output is correct |
95 |
Correct |
117 ms |
21916 KB |
Output is correct |
96 |
Correct |
120 ms |
19892 KB |
Output is correct |
97 |
Correct |
113 ms |
18036 KB |
Output is correct |
98 |
Correct |
123 ms |
17536 KB |
Output is correct |
99 |
Correct |
127 ms |
17408 KB |
Output is correct |
100 |
Correct |
145 ms |
20996 KB |
Output is correct |
101 |
Correct |
122 ms |
18572 KB |
Output is correct |
102 |
Correct |
109 ms |
17764 KB |
Output is correct |
103 |
Correct |
115 ms |
17740 KB |
Output is correct |
104 |
Correct |
84 ms |
17972 KB |
Output is correct |
105 |
Correct |
140 ms |
27716 KB |
Output is correct |
106 |
Correct |
65 ms |
17692 KB |
Output is correct |
107 |
Correct |
68 ms |
17680 KB |
Output is correct |
108 |
Correct |
148 ms |
32452 KB |
Output is correct |
109 |
Correct |
102 ms |
17968 KB |
Output is correct |
110 |
Correct |
127 ms |
32840 KB |
Output is correct |
111 |
Correct |
105 ms |
17352 KB |
Output is correct |