#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 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);
}
}
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 reroot(int x, int y) {
dp[x] = (c[x] == '1' ? 1 : 0);
int sum = 0, mx = 0;
for(int z: adj[y]) {
sum += dp[z];
mx = max(mx, dp[z]);
}
dp[y] = (c[y] == '1' ? max(mx + 1, sum - 1) : sum);
}
void dfs(int node, int prev) {
ans = max(ans, dp[node]);
for(int x: adj[node]) {
if(x != prev) {
reroot(node, x);
dfs(x, node);
reroot(x, node);
}
}
}
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 |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
6 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
4 ms |
7244 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
4 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
11 |
Correct |
5 ms |
7372 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
4 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
6 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
4 ms |
7244 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
4 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
11 |
Correct |
5 ms |
7372 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
4 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
22 |
Correct |
5 ms |
7464 KB |
Output is correct |
23 |
Correct |
6 ms |
7372 KB |
Output is correct |
24 |
Correct |
5 ms |
7460 KB |
Output is correct |
25 |
Correct |
6 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7372 KB |
Output is correct |
27 |
Correct |
5 ms |
7372 KB |
Output is correct |
28 |
Correct |
5 ms |
7372 KB |
Output is correct |
29 |
Correct |
5 ms |
7500 KB |
Output is correct |
30 |
Correct |
6 ms |
7500 KB |
Output is correct |
31 |
Correct |
5 ms |
7372 KB |
Output is correct |
32 |
Correct |
6 ms |
7372 KB |
Output is correct |
33 |
Correct |
6 ms |
7468 KB |
Output is correct |
34 |
Correct |
6 ms |
7372 KB |
Output is correct |
35 |
Correct |
5 ms |
7372 KB |
Output is correct |
36 |
Correct |
5 ms |
7372 KB |
Output is correct |
37 |
Correct |
8 ms |
7372 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 KB |
Output is correct |
39 |
Correct |
5 ms |
7500 KB |
Output is correct |
40 |
Correct |
5 ms |
7372 KB |
Output is correct |
41 |
Correct |
5 ms |
7500 KB |
Output is correct |
42 |
Correct |
7 ms |
7372 KB |
Output is correct |
43 |
Correct |
6 ms |
7372 KB |
Output is correct |
44 |
Correct |
5 ms |
7372 KB |
Output is correct |
45 |
Correct |
5 ms |
7372 KB |
Output is correct |
46 |
Correct |
5 ms |
7372 KB |
Output is correct |
47 |
Correct |
5 ms |
7372 KB |
Output is correct |
48 |
Correct |
6 ms |
7512 KB |
Output is correct |
49 |
Correct |
5 ms |
7532 KB |
Output is correct |
50 |
Correct |
5 ms |
7384 KB |
Output is correct |
51 |
Correct |
5 ms |
7384 KB |
Output is correct |
52 |
Correct |
6 ms |
7484 KB |
Output is correct |
53 |
Correct |
6 ms |
7384 KB |
Output is correct |
54 |
Correct |
5 ms |
7384 KB |
Output is correct |
55 |
Correct |
5 ms |
7488 KB |
Output is correct |
56 |
Correct |
11 ms |
7384 KB |
Output is correct |
57 |
Correct |
5 ms |
7512 KB |
Output is correct |
58 |
Correct |
8 ms |
7388 KB |
Output is correct |
59 |
Correct |
8 ms |
7372 KB |
Output is correct |
60 |
Correct |
5 ms |
7500 KB |
Output is correct |
61 |
Correct |
9 ms |
7388 KB |
Output is correct |
62 |
Correct |
5 ms |
7500 KB |
Output is correct |
63 |
Correct |
6 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
6 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
4 ms |
7244 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
4 ms |
7244 KB |
Output is correct |
10 |
Correct |
5 ms |
7244 KB |
Output is correct |
11 |
Correct |
5 ms |
7372 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
4 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
22 |
Correct |
5 ms |
7464 KB |
Output is correct |
23 |
Correct |
6 ms |
7372 KB |
Output is correct |
24 |
Correct |
5 ms |
7460 KB |
Output is correct |
25 |
Correct |
6 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7372 KB |
Output is correct |
27 |
Correct |
5 ms |
7372 KB |
Output is correct |
28 |
Correct |
5 ms |
7372 KB |
Output is correct |
29 |
Correct |
5 ms |
7500 KB |
Output is correct |
30 |
Correct |
6 ms |
7500 KB |
Output is correct |
31 |
Correct |
5 ms |
7372 KB |
Output is correct |
32 |
Correct |
6 ms |
7372 KB |
Output is correct |
33 |
Correct |
6 ms |
7468 KB |
Output is correct |
34 |
Correct |
6 ms |
7372 KB |
Output is correct |
35 |
Correct |
5 ms |
7372 KB |
Output is correct |
36 |
Correct |
5 ms |
7372 KB |
Output is correct |
37 |
Correct |
8 ms |
7372 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 KB |
Output is correct |
39 |
Correct |
5 ms |
7500 KB |
Output is correct |
40 |
Correct |
5 ms |
7372 KB |
Output is correct |
41 |
Correct |
5 ms |
7500 KB |
Output is correct |
42 |
Correct |
7 ms |
7372 KB |
Output is correct |
43 |
Correct |
6 ms |
7372 KB |
Output is correct |
44 |
Correct |
5 ms |
7372 KB |
Output is correct |
45 |
Correct |
5 ms |
7372 KB |
Output is correct |
46 |
Correct |
5 ms |
7372 KB |
Output is correct |
47 |
Correct |
5 ms |
7372 KB |
Output is correct |
48 |
Correct |
6 ms |
7512 KB |
Output is correct |
49 |
Correct |
5 ms |
7532 KB |
Output is correct |
50 |
Correct |
5 ms |
7384 KB |
Output is correct |
51 |
Correct |
5 ms |
7384 KB |
Output is correct |
52 |
Correct |
6 ms |
7484 KB |
Output is correct |
53 |
Correct |
6 ms |
7384 KB |
Output is correct |
54 |
Correct |
5 ms |
7384 KB |
Output is correct |
55 |
Correct |
5 ms |
7488 KB |
Output is correct |
56 |
Correct |
11 ms |
7384 KB |
Output is correct |
57 |
Correct |
5 ms |
7512 KB |
Output is correct |
58 |
Correct |
8 ms |
7388 KB |
Output is correct |
59 |
Correct |
8 ms |
7372 KB |
Output is correct |
60 |
Correct |
5 ms |
7500 KB |
Output is correct |
61 |
Correct |
9 ms |
7388 KB |
Output is correct |
62 |
Correct |
5 ms |
7500 KB |
Output is correct |
63 |
Correct |
6 ms |
7372 KB |
Output is correct |
64 |
Correct |
149 ms |
16452 KB |
Output is correct |
65 |
Correct |
165 ms |
19188 KB |
Output is correct |
66 |
Correct |
194 ms |
19192 KB |
Output is correct |
67 |
Correct |
191 ms |
19120 KB |
Output is correct |
68 |
Correct |
177 ms |
19184 KB |
Output is correct |
69 |
Correct |
215 ms |
19180 KB |
Output is correct |
70 |
Correct |
152 ms |
19144 KB |
Output is correct |
71 |
Correct |
186 ms |
19184 KB |
Output is correct |
72 |
Correct |
172 ms |
19188 KB |
Output is correct |
73 |
Correct |
206 ms |
28192 KB |
Output is correct |
74 |
Correct |
168 ms |
28228 KB |
Output is correct |
75 |
Correct |
163 ms |
20416 KB |
Output is correct |
76 |
Correct |
173 ms |
20416 KB |
Output is correct |
77 |
Correct |
160 ms |
19092 KB |
Output is correct |
78 |
Correct |
173 ms |
19172 KB |
Output is correct |
79 |
Correct |
166 ms |
19176 KB |
Output is correct |
80 |
Correct |
185 ms |
19176 KB |
Output is correct |
81 |
Execution timed out |
1565 ms |
19108 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |