#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
vector<int> adj[MAXN];
char c[MAXN];
int dp[MAXN];
int s[MAXN], m[MAXN];
int dpf(int node, int prev) {
int sum = 0, mx = 0;
for(int x: adj[node]) {
if(x != prev) {
int d = dpf(x, node);
sum += d;
mx = max(mx, d);
}
}
s[node] = sum;
m[node] = mx;
if(prev == -1 && c[node] == '1') {
return dp[node] = max(mx + 1, sum - 1);
}
if(prev == -1 && c[node] == '0') {
return dp[node] = sum;
}
if(c[node] == '1') {
return dp[node] = max(1ll, sum - 1);
}
else {
return dp[node] = sum;
}
}
int ans = 0;
void dfs(int node, int prev) {
ans = max(ans, dp[node]);
vector<int> v;
for(int x: adj[node]) {
if(x != prev) v.push_back(dp[x]);
}
int w = v.size();
vector<int> pmax, smax;
pmax.resize(w);
smax.resize(w);
if(w) pmax[0] = v[0], smax[w-1] = v[w-1];
for(int i=1; i<w; i++) pmax[i] = max(pmax[i-1], v[i]);
for(int i=w-2; i>=0; i--) smax[i] = max(smax[i+1], v[i]);
int id = 0;
for(int x: adj[node]) {
if(x != prev) {
int sto1, sto2, sto3, sto4, sto5, sto6;
sto1 = s[node], sto2 = m[node], sto3 = dp[node];
sto4 = s[x], sto5 = m[x], sto6 = dp[x];
//reroot node -> x
s[node] -= dp[x];
m[node] = max((id == 0 ? 0 : pmax[id-1]), (id == w-1 ? 0 : smax[id+1]));
dp[node] = (c[node] == '1' ? max(1ll, s[node] - 1) : s[node]);
s[x] += dp[node];
m[x] = max(m[x], dp[node]);
dp[x] = (c[x] == '1' ? max(m[x] + 1, s[x] - 1) : s[x]);
dfs(x, node);
//reroot x -> node
s[node] = sto1, m[node] = sto2, dp[node] = sto3;
s[x] = sto4, m[x] = sto5, dp[x] = sto6;
id++;
}
}
}
void solve(int tc) {
int N;
cin >> N;
for(int i=1; i<N; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if(N == 1) {
cout << "1\n"; return;
}
for(int i=1; i<=N; i++) cin >> c[i];
dpf(1, -1);
dfs(1, -1);
cout << ans << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7348 KB |
Output is correct |
11 |
Correct |
4 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7300 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7268 KB |
Output is correct |
19 |
Correct |
4 ms |
7304 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7348 KB |
Output is correct |
11 |
Correct |
4 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7300 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7268 KB |
Output is correct |
19 |
Correct |
4 ms |
7304 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7360 KB |
Output is correct |
22 |
Correct |
6 ms |
7504 KB |
Output is correct |
23 |
Correct |
5 ms |
7528 KB |
Output is correct |
24 |
Correct |
6 ms |
7504 KB |
Output is correct |
25 |
Correct |
5 ms |
7412 KB |
Output is correct |
26 |
Correct |
5 ms |
7500 KB |
Output is correct |
27 |
Correct |
5 ms |
7500 KB |
Output is correct |
28 |
Correct |
5 ms |
7500 KB |
Output is correct |
29 |
Correct |
6 ms |
8000 KB |
Output is correct |
30 |
Correct |
6 ms |
7884 KB |
Output is correct |
31 |
Correct |
5 ms |
7504 KB |
Output is correct |
32 |
Correct |
6 ms |
7508 KB |
Output is correct |
33 |
Correct |
5 ms |
7500 KB |
Output is correct |
34 |
Correct |
6 ms |
7504 KB |
Output is correct |
35 |
Correct |
5 ms |
7504 KB |
Output is correct |
36 |
Correct |
5 ms |
7504 KB |
Output is correct |
37 |
Correct |
6 ms |
7508 KB |
Output is correct |
38 |
Correct |
6 ms |
7484 KB |
Output is correct |
39 |
Correct |
5 ms |
7768 KB |
Output is correct |
40 |
Correct |
5 ms |
7628 KB |
Output is correct |
41 |
Correct |
6 ms |
7756 KB |
Output is correct |
42 |
Correct |
5 ms |
7616 KB |
Output is correct |
43 |
Correct |
5 ms |
7500 KB |
Output is correct |
44 |
Correct |
6 ms |
7636 KB |
Output is correct |
45 |
Correct |
6 ms |
7628 KB |
Output is correct |
46 |
Correct |
6 ms |
7500 KB |
Output is correct |
47 |
Correct |
5 ms |
7496 KB |
Output is correct |
48 |
Correct |
5 ms |
7792 KB |
Output is correct |
49 |
Correct |
7 ms |
7756 KB |
Output is correct |
50 |
Correct |
5 ms |
7628 KB |
Output is correct |
51 |
Correct |
5 ms |
7628 KB |
Output is correct |
52 |
Correct |
5 ms |
7500 KB |
Output is correct |
53 |
Correct |
5 ms |
7636 KB |
Output is correct |
54 |
Correct |
5 ms |
7564 KB |
Output is correct |
55 |
Correct |
7 ms |
7628 KB |
Output is correct |
56 |
Correct |
8 ms |
7684 KB |
Output is correct |
57 |
Correct |
6 ms |
8016 KB |
Output is correct |
58 |
Correct |
6 ms |
7504 KB |
Output is correct |
59 |
Correct |
6 ms |
7512 KB |
Output is correct |
60 |
Correct |
6 ms |
7756 KB |
Output is correct |
61 |
Correct |
5 ms |
7500 KB |
Output is correct |
62 |
Correct |
6 ms |
7884 KB |
Output is correct |
63 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7348 KB |
Output is correct |
11 |
Correct |
4 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7300 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7268 KB |
Output is correct |
19 |
Correct |
4 ms |
7304 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7360 KB |
Output is correct |
22 |
Correct |
6 ms |
7504 KB |
Output is correct |
23 |
Correct |
5 ms |
7528 KB |
Output is correct |
24 |
Correct |
6 ms |
7504 KB |
Output is correct |
25 |
Correct |
5 ms |
7412 KB |
Output is correct |
26 |
Correct |
5 ms |
7500 KB |
Output is correct |
27 |
Correct |
5 ms |
7500 KB |
Output is correct |
28 |
Correct |
5 ms |
7500 KB |
Output is correct |
29 |
Correct |
6 ms |
8000 KB |
Output is correct |
30 |
Correct |
6 ms |
7884 KB |
Output is correct |
31 |
Correct |
5 ms |
7504 KB |
Output is correct |
32 |
Correct |
6 ms |
7508 KB |
Output is correct |
33 |
Correct |
5 ms |
7500 KB |
Output is correct |
34 |
Correct |
6 ms |
7504 KB |
Output is correct |
35 |
Correct |
5 ms |
7504 KB |
Output is correct |
36 |
Correct |
5 ms |
7504 KB |
Output is correct |
37 |
Correct |
6 ms |
7508 KB |
Output is correct |
38 |
Correct |
6 ms |
7484 KB |
Output is correct |
39 |
Correct |
5 ms |
7768 KB |
Output is correct |
40 |
Correct |
5 ms |
7628 KB |
Output is correct |
41 |
Correct |
6 ms |
7756 KB |
Output is correct |
42 |
Correct |
5 ms |
7616 KB |
Output is correct |
43 |
Correct |
5 ms |
7500 KB |
Output is correct |
44 |
Correct |
6 ms |
7636 KB |
Output is correct |
45 |
Correct |
6 ms |
7628 KB |
Output is correct |
46 |
Correct |
6 ms |
7500 KB |
Output is correct |
47 |
Correct |
5 ms |
7496 KB |
Output is correct |
48 |
Correct |
5 ms |
7792 KB |
Output is correct |
49 |
Correct |
7 ms |
7756 KB |
Output is correct |
50 |
Correct |
5 ms |
7628 KB |
Output is correct |
51 |
Correct |
5 ms |
7628 KB |
Output is correct |
52 |
Correct |
5 ms |
7500 KB |
Output is correct |
53 |
Correct |
5 ms |
7636 KB |
Output is correct |
54 |
Correct |
5 ms |
7564 KB |
Output is correct |
55 |
Correct |
7 ms |
7628 KB |
Output is correct |
56 |
Correct |
8 ms |
7684 KB |
Output is correct |
57 |
Correct |
6 ms |
8016 KB |
Output is correct |
58 |
Correct |
6 ms |
7504 KB |
Output is correct |
59 |
Correct |
6 ms |
7512 KB |
Output is correct |
60 |
Correct |
6 ms |
7756 KB |
Output is correct |
61 |
Correct |
5 ms |
7500 KB |
Output is correct |
62 |
Correct |
6 ms |
7884 KB |
Output is correct |
63 |
Correct |
5 ms |
7424 KB |
Output is correct |
64 |
Correct |
201 ms |
20940 KB |
Output is correct |
65 |
Correct |
205 ms |
21028 KB |
Output is correct |
66 |
Correct |
205 ms |
21116 KB |
Output is correct |
67 |
Correct |
188 ms |
20996 KB |
Output is correct |
68 |
Correct |
195 ms |
21092 KB |
Output is correct |
69 |
Correct |
221 ms |
21064 KB |
Output is correct |
70 |
Correct |
203 ms |
21032 KB |
Output is correct |
71 |
Correct |
194 ms |
21224 KB |
Output is correct |
72 |
Correct |
197 ms |
21092 KB |
Output is correct |
73 |
Correct |
279 ms |
73388 KB |
Output is correct |
74 |
Correct |
292 ms |
73028 KB |
Output is correct |
75 |
Correct |
186 ms |
22332 KB |
Output is correct |
76 |
Correct |
225 ms |
22324 KB |
Output is correct |
77 |
Correct |
203 ms |
21016 KB |
Output is correct |
78 |
Correct |
211 ms |
21088 KB |
Output is correct |
79 |
Correct |
216 ms |
20908 KB |
Output is correct |
80 |
Correct |
200 ms |
20944 KB |
Output is correct |
81 |
Correct |
157 ms |
23508 KB |
Output is correct |
82 |
Correct |
165 ms |
23488 KB |
Output is correct |
83 |
Correct |
225 ms |
43992 KB |
Output is correct |
84 |
Correct |
240 ms |
53060 KB |
Output is correct |
85 |
Correct |
232 ms |
51744 KB |
Output is correct |
86 |
Correct |
208 ms |
28124 KB |
Output is correct |
87 |
Correct |
212 ms |
25724 KB |
Output is correct |
88 |
Correct |
202 ms |
25560 KB |
Output is correct |
89 |
Correct |
183 ms |
25588 KB |
Output is correct |
90 |
Correct |
189 ms |
24708 KB |
Output is correct |
91 |
Correct |
201 ms |
25700 KB |
Output is correct |
92 |
Correct |
186 ms |
27320 KB |
Output is correct |
93 |
Correct |
189 ms |
39528 KB |
Output is correct |
94 |
Correct |
189 ms |
44836 KB |
Output is correct |
95 |
Correct |
190 ms |
40176 KB |
Output is correct |
96 |
Correct |
197 ms |
32120 KB |
Output is correct |
97 |
Correct |
179 ms |
25208 KB |
Output is correct |
98 |
Correct |
167 ms |
22796 KB |
Output is correct |
99 |
Correct |
184 ms |
22444 KB |
Output is correct |
100 |
Correct |
208 ms |
36564 KB |
Output is correct |
101 |
Correct |
194 ms |
27464 KB |
Output is correct |
102 |
Correct |
174 ms |
23740 KB |
Output is correct |
103 |
Correct |
182 ms |
22872 KB |
Output is correct |
104 |
Correct |
95 ms |
27736 KB |
Output is correct |
105 |
Correct |
259 ms |
61192 KB |
Output is correct |
106 |
Correct |
93 ms |
27452 KB |
Output is correct |
107 |
Correct |
99 ms |
27508 KB |
Output is correct |
108 |
Correct |
305 ms |
77864 KB |
Output is correct |
109 |
Correct |
110 ms |
27712 KB |
Output is correct |
110 |
Correct |
254 ms |
79132 KB |
Output is correct |
111 |
Correct |
162 ms |
22280 KB |
Output is correct |