#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
*/
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
#define maxn 200005
ll n;
vector<ll> g[maxn];
ll c[maxn];
ll dp[maxn][2];
void dfs(ll u,ll p){
ll mx = 0;
ll mx1 = 0;
ll mx0 = 0;
ll sum = 0;
for(ll s : g[u]){
if(s==p) continue;
dfs(s,u);
mx = max({mx,dp[s][0],dp[s][1]-1});
mx1 = max(mx1,dp[s][1]);
mx0 = max(mx0,dp[s][0]);
sum+=dp[s][0];
}
dp[u][0] = max(sum-c[u],c[u]);
if(c[u]&&mx!=0) dp[u][1] = mx + c[u];
else dp[u][1] = mx1;
//cerr<<"u: " <<u<< " "<< c[u]<< " "<<dp[u][0]<< " "<<dp[u][1]<<endl;
}
int main(){
daj_mi_malo_vremena
cin >> n;
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y); g[y].pb(x);
}
ll sum = 0;
{
string s; cin >> s;
for(ll i = 1;i<=n;i++) c[i] = s[i-1]-'0';
for(ll i = 1;i<=n;i++) sum+=c[i];
}
if(sum==0){cout<<0<<endl; return 0;}
dfs(1,1);
//ceri(c,1,n);
//for(ll i = 1;i<=n;i++) cerr<<dp[i][0]<< " ";
//cerr<<endl;
//for(ll i = 1;i<=n;i++) cerr<<dp[i][1]<< " ";
//cerr<<endl;
cout<<max({1LL,dp[1][0],dp[1][1]})<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5168 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5148 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5168 KB |
Output is correct |
34 |
Correct |
5 ms |
5172 KB |
Output is correct |
35 |
Correct |
4 ms |
5168 KB |
Output is correct |
36 |
Correct |
4 ms |
5168 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5164 KB |
Output is correct |
40 |
Correct |
3 ms |
5208 KB |
Output is correct |
41 |
Correct |
4 ms |
5204 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
43 |
Correct |
5 ms |
5096 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
3 ms |
5204 KB |
Output is correct |
46 |
Correct |
3 ms |
5180 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5168 KB |
Output is correct |
50 |
Correct |
3 ms |
5204 KB |
Output is correct |
51 |
Correct |
5 ms |
5204 KB |
Output is correct |
52 |
Correct |
4 ms |
5168 KB |
Output is correct |
53 |
Correct |
3 ms |
5204 KB |
Output is correct |
54 |
Correct |
4 ms |
5204 KB |
Output is correct |
55 |
Correct |
3 ms |
5204 KB |
Output is correct |
56 |
Correct |
3 ms |
5192 KB |
Output is correct |
57 |
Correct |
3 ms |
5332 KB |
Output is correct |
58 |
Correct |
4 ms |
5172 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5148 KB |
Output is correct |
61 |
Correct |
3 ms |
5160 KB |
Output is correct |
62 |
Correct |
4 ms |
5292 KB |
Output is correct |
63 |
Correct |
4 ms |
5172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5168 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5148 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5168 KB |
Output is correct |
34 |
Correct |
5 ms |
5172 KB |
Output is correct |
35 |
Correct |
4 ms |
5168 KB |
Output is correct |
36 |
Correct |
4 ms |
5168 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5164 KB |
Output is correct |
40 |
Correct |
3 ms |
5208 KB |
Output is correct |
41 |
Correct |
4 ms |
5204 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
43 |
Correct |
5 ms |
5096 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
3 ms |
5204 KB |
Output is correct |
46 |
Correct |
3 ms |
5180 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5168 KB |
Output is correct |
50 |
Correct |
3 ms |
5204 KB |
Output is correct |
51 |
Correct |
5 ms |
5204 KB |
Output is correct |
52 |
Correct |
4 ms |
5168 KB |
Output is correct |
53 |
Correct |
3 ms |
5204 KB |
Output is correct |
54 |
Correct |
4 ms |
5204 KB |
Output is correct |
55 |
Correct |
3 ms |
5204 KB |
Output is correct |
56 |
Correct |
3 ms |
5192 KB |
Output is correct |
57 |
Correct |
3 ms |
5332 KB |
Output is correct |
58 |
Correct |
4 ms |
5172 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5148 KB |
Output is correct |
61 |
Correct |
3 ms |
5160 KB |
Output is correct |
62 |
Correct |
4 ms |
5292 KB |
Output is correct |
63 |
Correct |
4 ms |
5172 KB |
Output is correct |
64 |
Correct |
119 ms |
19848 KB |
Output is correct |
65 |
Correct |
129 ms |
19992 KB |
Output is correct |
66 |
Correct |
126 ms |
19896 KB |
Output is correct |
67 |
Correct |
142 ms |
19788 KB |
Output is correct |
68 |
Correct |
124 ms |
19880 KB |
Output is correct |
69 |
Correct |
120 ms |
19900 KB |
Output is correct |
70 |
Correct |
128 ms |
19936 KB |
Output is correct |
71 |
Correct |
126 ms |
19796 KB |
Output is correct |
72 |
Correct |
120 ms |
19872 KB |
Output is correct |
73 |
Correct |
130 ms |
34028 KB |
Output is correct |
74 |
Correct |
138 ms |
34084 KB |
Output is correct |
75 |
Correct |
128 ms |
21132 KB |
Output is correct |
76 |
Correct |
118 ms |
21076 KB |
Output is correct |
77 |
Correct |
120 ms |
19812 KB |
Output is correct |
78 |
Correct |
130 ms |
19936 KB |
Output is correct |
79 |
Correct |
128 ms |
19876 KB |
Output is correct |
80 |
Correct |
131 ms |
19808 KB |
Output is correct |
81 |
Correct |
95 ms |
20132 KB |
Output is correct |
82 |
Correct |
110 ms |
19856 KB |
Output is correct |
83 |
Correct |
132 ms |
25952 KB |
Output is correct |
84 |
Correct |
131 ms |
28636 KB |
Output is correct |
85 |
Correct |
131 ms |
28204 KB |
Output is correct |
86 |
Correct |
120 ms |
21484 KB |
Output is correct |
87 |
Correct |
126 ms |
20824 KB |
Output is correct |
88 |
Correct |
118 ms |
20944 KB |
Output is correct |
89 |
Correct |
113 ms |
20920 KB |
Output is correct |
90 |
Correct |
111 ms |
20624 KB |
Output is correct |
91 |
Correct |
120 ms |
20968 KB |
Output is correct |
92 |
Correct |
117 ms |
21324 KB |
Output is correct |
93 |
Correct |
110 ms |
23984 KB |
Output is correct |
94 |
Correct |
101 ms |
26244 KB |
Output is correct |
95 |
Correct |
101 ms |
24544 KB |
Output is correct |
96 |
Correct |
106 ms |
22416 KB |
Output is correct |
97 |
Correct |
113 ms |
20588 KB |
Output is correct |
98 |
Correct |
114 ms |
20132 KB |
Output is correct |
99 |
Correct |
111 ms |
19884 KB |
Output is correct |
100 |
Correct |
109 ms |
23560 KB |
Output is correct |
101 |
Correct |
113 ms |
21060 KB |
Output is correct |
102 |
Correct |
110 ms |
20260 KB |
Output is correct |
103 |
Correct |
119 ms |
20168 KB |
Output is correct |
104 |
Correct |
68 ms |
20760 KB |
Output is correct |
105 |
Correct |
128 ms |
30168 KB |
Output is correct |
106 |
Correct |
80 ms |
20756 KB |
Output is correct |
107 |
Correct |
73 ms |
20640 KB |
Output is correct |
108 |
Correct |
137 ms |
34976 KB |
Output is correct |
109 |
Correct |
67 ms |
20728 KB |
Output is correct |
110 |
Correct |
129 ms |
35260 KB |
Output is correct |
111 |
Correct |
108 ms |
19800 KB |
Output is correct |