#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
const ll INF = 1e9;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
ll N; cin >> N;
if (N==1) {
cout << "1\n"; exit(0);
}
ll ans = 0;
vector<ll> adj[N];
vector<ll> fadj[N];
for (ll i=0;i<(N-1);i++) {
ll a,b; cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<bool> found;
vector<ll> radj;
for (ll i=0;i<N;i++) {
radj.push_back(-1);
found.push_back(0);
}
queue<ll> q0;
q0.push(0);
ll fdegL[N];
queue<ll> q1;
vector<ll> tpls;
while (!q0.empty()) {
ll x = q0.front(); q0.pop();
tpls.push_back(x);
for (ll y: adj[x]) {
if (y != radj[x]) {
fadj[x].push_back(y);
radj[y]=x;
q0.push(y);
}
}
fdegL[x]=fadj[x].size();
if (fdegL[x]==0) {
q1.push(x);
}
}
bool S[N];
string s1; cin >> s1;
for (ll i=0;i<N;i++) {
if (s1[i]=='1') {
S[i]=1;
} else {
assert(s1[i]=='0');
S[i]=0;
}
}
ll dp0[N]; //dp on: you know that it has to connect down; given that it
//does this, what is the max value?
ll dp1[N]; //does not connect down; what's the max value?
ll NUMFOUND=0;
/*while (!q1.empty()) {
ll x = q1.front(); q1.pop();
NUMFOUND++;
if (fadj[x].size()==0) {
dp0[x]=-INF;
dp1[x]=S[x];
} else {
dp1[x]=S[x];
ll mx=-INF; ll sm=0; //max and sum
for (ll y: fadj[x]) {
ll vy = max(dp1[y],dp0[y]);
mx = max(mx,vy);
sm += max(0LL,vy);
}
assert(mx!=(-INF));
//cout << "mx,sm,dp1="<<mx<<","<<sm<<","<<dp1[x]<<"\n";
ans = max(ans,dp1[x]+mx);
dp0[x]=sm-dp1[x];
ans = max(ans,dp0[x]);
}
if (radj[x]!=-1) {
fdegL[radj[x]]--;
if (fdegL[radj[x]]==0) {
q1.push(radj[x]);
}
}
}*/
for (ll i=(N-1);i>=0;i--) {
ll x = tpls[i];
NUMFOUND++;
if (fadj[x].size()==0) {
dp0[x]=-INF;
dp1[x]=S[x];
} else {
dp1[x]=S[x];
ll mx=-INF; ll sm=0; //max and sum
for (ll y: fadj[x]) {
ll vy = max(dp1[y],dp0[y]);
mx = max(mx,vy);
sm += max(0LL,vy);
}
assert(mx!=(-INF));
//cout << "mx,sm,dp1="<<mx<<","<<sm<<","<<dp1[x]<<"\n";
ans = max(ans,dp1[x]+mx);
dp0[x]=sm-dp1[x];
ans = max(ans,dp0[x]);
}
if (radj[x]!=-1) {
fdegL[radj[x]]--;
if (fdegL[radj[x]]==0) {
q1.push(radj[x]);
}
}
}
assert(NUMFOUND==N);
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
760 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
728 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
876 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
2 ms |
592 KB |
Output is correct |
36 |
Correct |
2 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
1 ms |
636 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
1 ms |
592 KB |
Output is correct |
44 |
Correct |
2 ms |
764 KB |
Output is correct |
45 |
Correct |
2 ms |
592 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
592 KB |
Output is correct |
48 |
Correct |
2 ms |
592 KB |
Output is correct |
49 |
Correct |
1 ms |
592 KB |
Output is correct |
50 |
Correct |
1 ms |
592 KB |
Output is correct |
51 |
Correct |
2 ms |
592 KB |
Output is correct |
52 |
Correct |
2 ms |
592 KB |
Output is correct |
53 |
Correct |
1 ms |
728 KB |
Output is correct |
54 |
Correct |
2 ms |
592 KB |
Output is correct |
55 |
Correct |
1 ms |
592 KB |
Output is correct |
56 |
Correct |
1 ms |
592 KB |
Output is correct |
57 |
Correct |
1 ms |
592 KB |
Output is correct |
58 |
Correct |
1 ms |
592 KB |
Output is correct |
59 |
Correct |
2 ms |
592 KB |
Output is correct |
60 |
Correct |
2 ms |
592 KB |
Output is correct |
61 |
Correct |
2 ms |
592 KB |
Output is correct |
62 |
Correct |
2 ms |
592 KB |
Output is correct |
63 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
760 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
728 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
876 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
2 ms |
592 KB |
Output is correct |
36 |
Correct |
2 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
1 ms |
636 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
1 ms |
592 KB |
Output is correct |
44 |
Correct |
2 ms |
764 KB |
Output is correct |
45 |
Correct |
2 ms |
592 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
592 KB |
Output is correct |
48 |
Correct |
2 ms |
592 KB |
Output is correct |
49 |
Correct |
1 ms |
592 KB |
Output is correct |
50 |
Correct |
1 ms |
592 KB |
Output is correct |
51 |
Correct |
2 ms |
592 KB |
Output is correct |
52 |
Correct |
2 ms |
592 KB |
Output is correct |
53 |
Correct |
1 ms |
728 KB |
Output is correct |
54 |
Correct |
2 ms |
592 KB |
Output is correct |
55 |
Correct |
1 ms |
592 KB |
Output is correct |
56 |
Correct |
1 ms |
592 KB |
Output is correct |
57 |
Correct |
1 ms |
592 KB |
Output is correct |
58 |
Correct |
1 ms |
592 KB |
Output is correct |
59 |
Correct |
2 ms |
592 KB |
Output is correct |
60 |
Correct |
2 ms |
592 KB |
Output is correct |
61 |
Correct |
2 ms |
592 KB |
Output is correct |
62 |
Correct |
2 ms |
592 KB |
Output is correct |
63 |
Correct |
1 ms |
592 KB |
Output is correct |
64 |
Correct |
126 ms |
33720 KB |
Output is correct |
65 |
Correct |
105 ms |
33732 KB |
Output is correct |
66 |
Correct |
127 ms |
33720 KB |
Output is correct |
67 |
Correct |
122 ms |
33696 KB |
Output is correct |
68 |
Correct |
124 ms |
33724 KB |
Output is correct |
69 |
Correct |
138 ms |
33720 KB |
Output is correct |
70 |
Correct |
124 ms |
33720 KB |
Output is correct |
71 |
Correct |
126 ms |
33720 KB |
Output is correct |
72 |
Correct |
119 ms |
33720 KB |
Output is correct |
73 |
Correct |
139 ms |
34756 KB |
Output is correct |
74 |
Correct |
119 ms |
34744 KB |
Output is correct |
75 |
Correct |
123 ms |
33472 KB |
Output is correct |
76 |
Correct |
128 ms |
33472 KB |
Output is correct |
77 |
Correct |
136 ms |
33976 KB |
Output is correct |
78 |
Correct |
123 ms |
33976 KB |
Output is correct |
79 |
Correct |
114 ms |
33968 KB |
Output is correct |
80 |
Correct |
123 ms |
33984 KB |
Output is correct |
81 |
Correct |
93 ms |
32840 KB |
Output is correct |
82 |
Correct |
106 ms |
33344 KB |
Output is correct |
83 |
Correct |
108 ms |
33728 KB |
Output is correct |
84 |
Correct |
102 ms |
33984 KB |
Output is correct |
85 |
Correct |
131 ms |
33984 KB |
Output is correct |
86 |
Correct |
106 ms |
33780 KB |
Output is correct |
87 |
Correct |
142 ms |
33720 KB |
Output is correct |
88 |
Correct |
122 ms |
34240 KB |
Output is correct |
89 |
Correct |
114 ms |
34404 KB |
Output is correct |
90 |
Correct |
119 ms |
34324 KB |
Output is correct |
91 |
Correct |
134 ms |
34240 KB |
Output is correct |
92 |
Correct |
121 ms |
34240 KB |
Output is correct |
93 |
Correct |
131 ms |
33344 KB |
Output is correct |
94 |
Correct |
90 ms |
33244 KB |
Output is correct |
95 |
Correct |
92 ms |
33336 KB |
Output is correct |
96 |
Correct |
94 ms |
33464 KB |
Output is correct |
97 |
Correct |
104 ms |
33732 KB |
Output is correct |
98 |
Correct |
94 ms |
33976 KB |
Output is correct |
99 |
Correct |
91 ms |
33984 KB |
Output is correct |
100 |
Correct |
106 ms |
33464 KB |
Output is correct |
101 |
Correct |
88 ms |
33472 KB |
Output is correct |
102 |
Correct |
88 ms |
33720 KB |
Output is correct |
103 |
Correct |
93 ms |
34240 KB |
Output is correct |
104 |
Correct |
70 ms |
33880 KB |
Output is correct |
105 |
Correct |
117 ms |
34764 KB |
Output is correct |
106 |
Correct |
75 ms |
33624 KB |
Output is correct |
107 |
Correct |
71 ms |
33768 KB |
Output is correct |
108 |
Correct |
118 ms |
34800 KB |
Output is correct |
109 |
Correct |
75 ms |
33924 KB |
Output is correct |
110 |
Correct |
110 ms |
34752 KB |
Output is correct |
111 |
Correct |
100 ms |
32820 KB |
Output is correct |