#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=4e5+5;
const ll K=350;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll n,x,y,h[N],dp[N],ans,col[N],sum;
vector<ll> v[N];
string s;
void DFS(ll nd,ll pa){
dp[nd]=0;
for(auto i:v[nd]){
if(i==pa)continue;
DFS(i,nd);
dp[nd]+=max(dp[i]-col[i],col[i]);
}
return ;
}
void move(ll rt,ll to){
dp[rt]-=max(dp[to]-col[to],col[to]);
dp[to]+=max(dp[rt]-col[rt],col[rt]);
}
void reroot(ll nd,ll pa){
ans=max(ans,max(dp[nd]-col[nd],col[nd]));
for(auto i:v[nd]){
if(i==pa)continue;
move(nd,i);
reroot(i,nd);
move(i,nd);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
REP(i,n-1)cin>>x>>y,v[x].pb(y),v[y].pb(x);
cin>>s;
REP(i,n)if(s[i]=='1')col[i+1]=1,++sum;
if(sum>=2)ans=2;else ans=sum;
DFS(1,0);
reroot(1,0);
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
7 ms |
9708 KB |
Output is correct |
22 |
Correct |
8 ms |
9836 KB |
Output is correct |
23 |
Correct |
8 ms |
9836 KB |
Output is correct |
24 |
Correct |
8 ms |
9836 KB |
Output is correct |
25 |
Correct |
8 ms |
9836 KB |
Output is correct |
26 |
Correct |
8 ms |
9836 KB |
Output is correct |
27 |
Correct |
8 ms |
9836 KB |
Output is correct |
28 |
Correct |
8 ms |
9836 KB |
Output is correct |
29 |
Correct |
8 ms |
9928 KB |
Output is correct |
30 |
Correct |
8 ms |
9964 KB |
Output is correct |
31 |
Correct |
8 ms |
9964 KB |
Output is correct |
32 |
Correct |
8 ms |
9836 KB |
Output is correct |
33 |
Correct |
7 ms |
9836 KB |
Output is correct |
34 |
Correct |
8 ms |
9836 KB |
Output is correct |
35 |
Correct |
7 ms |
9836 KB |
Output is correct |
36 |
Correct |
8 ms |
9836 KB |
Output is correct |
37 |
Correct |
8 ms |
9836 KB |
Output is correct |
38 |
Correct |
8 ms |
9836 KB |
Output is correct |
39 |
Correct |
9 ms |
9964 KB |
Output is correct |
40 |
Correct |
8 ms |
9836 KB |
Output is correct |
41 |
Correct |
8 ms |
9964 KB |
Output is correct |
42 |
Correct |
8 ms |
9836 KB |
Output is correct |
43 |
Correct |
9 ms |
9836 KB |
Output is correct |
44 |
Correct |
8 ms |
9984 KB |
Output is correct |
45 |
Correct |
8 ms |
9964 KB |
Output is correct |
46 |
Correct |
8 ms |
9836 KB |
Output is correct |
47 |
Correct |
8 ms |
9836 KB |
Output is correct |
48 |
Correct |
8 ms |
9964 KB |
Output is correct |
49 |
Correct |
8 ms |
9964 KB |
Output is correct |
50 |
Correct |
9 ms |
9964 KB |
Output is correct |
51 |
Correct |
8 ms |
9964 KB |
Output is correct |
52 |
Correct |
8 ms |
9836 KB |
Output is correct |
53 |
Correct |
8 ms |
9964 KB |
Output is correct |
54 |
Correct |
8 ms |
9964 KB |
Output is correct |
55 |
Correct |
9 ms |
9836 KB |
Output is correct |
56 |
Correct |
8 ms |
9836 KB |
Output is correct |
57 |
Correct |
8 ms |
9984 KB |
Output is correct |
58 |
Correct |
8 ms |
9836 KB |
Output is correct |
59 |
Correct |
8 ms |
9836 KB |
Output is correct |
60 |
Correct |
8 ms |
9964 KB |
Output is correct |
61 |
Correct |
8 ms |
9836 KB |
Output is correct |
62 |
Correct |
8 ms |
9964 KB |
Output is correct |
63 |
Correct |
8 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
7 ms |
9708 KB |
Output is correct |
22 |
Correct |
8 ms |
9836 KB |
Output is correct |
23 |
Correct |
8 ms |
9836 KB |
Output is correct |
24 |
Correct |
8 ms |
9836 KB |
Output is correct |
25 |
Correct |
8 ms |
9836 KB |
Output is correct |
26 |
Correct |
8 ms |
9836 KB |
Output is correct |
27 |
Correct |
8 ms |
9836 KB |
Output is correct |
28 |
Correct |
8 ms |
9836 KB |
Output is correct |
29 |
Correct |
8 ms |
9928 KB |
Output is correct |
30 |
Correct |
8 ms |
9964 KB |
Output is correct |
31 |
Correct |
8 ms |
9964 KB |
Output is correct |
32 |
Correct |
8 ms |
9836 KB |
Output is correct |
33 |
Correct |
7 ms |
9836 KB |
Output is correct |
34 |
Correct |
8 ms |
9836 KB |
Output is correct |
35 |
Correct |
7 ms |
9836 KB |
Output is correct |
36 |
Correct |
8 ms |
9836 KB |
Output is correct |
37 |
Correct |
8 ms |
9836 KB |
Output is correct |
38 |
Correct |
8 ms |
9836 KB |
Output is correct |
39 |
Correct |
9 ms |
9964 KB |
Output is correct |
40 |
Correct |
8 ms |
9836 KB |
Output is correct |
41 |
Correct |
8 ms |
9964 KB |
Output is correct |
42 |
Correct |
8 ms |
9836 KB |
Output is correct |
43 |
Correct |
9 ms |
9836 KB |
Output is correct |
44 |
Correct |
8 ms |
9984 KB |
Output is correct |
45 |
Correct |
8 ms |
9964 KB |
Output is correct |
46 |
Correct |
8 ms |
9836 KB |
Output is correct |
47 |
Correct |
8 ms |
9836 KB |
Output is correct |
48 |
Correct |
8 ms |
9964 KB |
Output is correct |
49 |
Correct |
8 ms |
9964 KB |
Output is correct |
50 |
Correct |
9 ms |
9964 KB |
Output is correct |
51 |
Correct |
8 ms |
9964 KB |
Output is correct |
52 |
Correct |
8 ms |
9836 KB |
Output is correct |
53 |
Correct |
8 ms |
9964 KB |
Output is correct |
54 |
Correct |
8 ms |
9964 KB |
Output is correct |
55 |
Correct |
9 ms |
9836 KB |
Output is correct |
56 |
Correct |
8 ms |
9836 KB |
Output is correct |
57 |
Correct |
8 ms |
9984 KB |
Output is correct |
58 |
Correct |
8 ms |
9836 KB |
Output is correct |
59 |
Correct |
8 ms |
9836 KB |
Output is correct |
60 |
Correct |
8 ms |
9964 KB |
Output is correct |
61 |
Correct |
8 ms |
9836 KB |
Output is correct |
62 |
Correct |
8 ms |
9964 KB |
Output is correct |
63 |
Correct |
8 ms |
9836 KB |
Output is correct |
64 |
Correct |
210 ms |
20856 KB |
Output is correct |
65 |
Correct |
232 ms |
21036 KB |
Output is correct |
66 |
Correct |
206 ms |
20892 KB |
Output is correct |
67 |
Correct |
214 ms |
20892 KB |
Output is correct |
68 |
Correct |
217 ms |
21020 KB |
Output is correct |
69 |
Correct |
214 ms |
20984 KB |
Output is correct |
70 |
Correct |
214 ms |
20960 KB |
Output is correct |
71 |
Correct |
221 ms |
20792 KB |
Output is correct |
72 |
Correct |
210 ms |
19368 KB |
Output is correct |
73 |
Correct |
240 ms |
32556 KB |
Output is correct |
74 |
Correct |
250 ms |
32556 KB |
Output is correct |
75 |
Correct |
213 ms |
22184 KB |
Output is correct |
76 |
Correct |
213 ms |
22192 KB |
Output is correct |
77 |
Correct |
225 ms |
21168 KB |
Output is correct |
78 |
Correct |
228 ms |
20904 KB |
Output is correct |
79 |
Correct |
226 ms |
20912 KB |
Output is correct |
80 |
Correct |
231 ms |
20904 KB |
Output is correct |
81 |
Correct |
176 ms |
20960 KB |
Output is correct |
82 |
Correct |
206 ms |
23268 KB |
Output is correct |
83 |
Correct |
228 ms |
28336 KB |
Output is correct |
84 |
Correct |
243 ms |
30524 KB |
Output is correct |
85 |
Correct |
231 ms |
30136 KB |
Output is correct |
86 |
Correct |
220 ms |
24620 KB |
Output is correct |
87 |
Correct |
239 ms |
24104 KB |
Output is correct |
88 |
Correct |
201 ms |
24296 KB |
Output is correct |
89 |
Correct |
204 ms |
24124 KB |
Output is correct |
90 |
Correct |
198 ms |
23952 KB |
Output is correct |
91 |
Correct |
204 ms |
24208 KB |
Output is correct |
92 |
Correct |
206 ms |
24476 KB |
Output is correct |
93 |
Correct |
195 ms |
26724 KB |
Output is correct |
94 |
Correct |
192 ms |
28384 KB |
Output is correct |
95 |
Correct |
187 ms |
27228 KB |
Output is correct |
96 |
Correct |
196 ms |
25256 KB |
Output is correct |
97 |
Correct |
207 ms |
23812 KB |
Output is correct |
98 |
Correct |
191 ms |
23388 KB |
Output is correct |
99 |
Correct |
204 ms |
23340 KB |
Output is correct |
100 |
Correct |
201 ms |
26264 KB |
Output is correct |
101 |
Correct |
194 ms |
24488 KB |
Output is correct |
102 |
Correct |
189 ms |
23548 KB |
Output is correct |
103 |
Correct |
215 ms |
23628 KB |
Output is correct |
104 |
Correct |
116 ms |
22364 KB |
Output is correct |
105 |
Correct |
236 ms |
30124 KB |
Output is correct |
106 |
Correct |
130 ms |
23900 KB |
Output is correct |
107 |
Correct |
127 ms |
23772 KB |
Output is correct |
108 |
Correct |
240 ms |
34092 KB |
Output is correct |
109 |
Correct |
121 ms |
22364 KB |
Output is correct |
110 |
Correct |
239 ms |
34476 KB |
Output is correct |
111 |
Correct |
195 ms |
23356 KB |
Output is correct |