#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 2e5 + 5;
int dp[2][mn], power[mn];
vector<int> adj[mn];
void dfs (int u, int p) {
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
dp[0][u] += max(0, dp[0][v]);
dp[1][u] = max(dp[1][u], dp[0][v]);
}
dp[1][u] += power[u];
dp[0][u] -= power[u];
if (power[u])
dp[0][u] = max(dp[0][u], 1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
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);
}
for (int i = 1; i <= n; i++) {
char c; cin >> c;
power[i] = c - '0';
}
dfs(1, 1);
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < 2; j++)
ans = max(ans, dp[j][i]);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6636 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6636 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6808 KB |
Output is correct |
29 |
Correct |
2 ms |
7004 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
2 ms |
6744 KB |
Output is correct |
33 |
Correct |
2 ms |
6744 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
2 ms |
6912 KB |
Output is correct |
43 |
Correct |
3 ms |
6876 KB |
Output is correct |
44 |
Correct |
2 ms |
6800 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6892 KB |
Output is correct |
48 |
Correct |
2 ms |
6748 KB |
Output is correct |
49 |
Correct |
2 ms |
6888 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
2 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
6748 KB |
Output is correct |
53 |
Correct |
2 ms |
6808 KB |
Output is correct |
54 |
Correct |
2 ms |
6748 KB |
Output is correct |
55 |
Correct |
2 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
7000 KB |
Output is correct |
57 |
Correct |
2 ms |
7000 KB |
Output is correct |
58 |
Correct |
2 ms |
6748 KB |
Output is correct |
59 |
Correct |
2 ms |
6748 KB |
Output is correct |
60 |
Correct |
2 ms |
6748 KB |
Output is correct |
61 |
Correct |
2 ms |
6748 KB |
Output is correct |
62 |
Correct |
2 ms |
6900 KB |
Output is correct |
63 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6636 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6808 KB |
Output is correct |
29 |
Correct |
2 ms |
7004 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
2 ms |
6744 KB |
Output is correct |
33 |
Correct |
2 ms |
6744 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
2 ms |
6912 KB |
Output is correct |
43 |
Correct |
3 ms |
6876 KB |
Output is correct |
44 |
Correct |
2 ms |
6800 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6892 KB |
Output is correct |
48 |
Correct |
2 ms |
6748 KB |
Output is correct |
49 |
Correct |
2 ms |
6888 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
2 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
6748 KB |
Output is correct |
53 |
Correct |
2 ms |
6808 KB |
Output is correct |
54 |
Correct |
2 ms |
6748 KB |
Output is correct |
55 |
Correct |
2 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
7000 KB |
Output is correct |
57 |
Correct |
2 ms |
7000 KB |
Output is correct |
58 |
Correct |
2 ms |
6748 KB |
Output is correct |
59 |
Correct |
2 ms |
6748 KB |
Output is correct |
60 |
Correct |
2 ms |
6748 KB |
Output is correct |
61 |
Correct |
2 ms |
6748 KB |
Output is correct |
62 |
Correct |
2 ms |
6900 KB |
Output is correct |
63 |
Correct |
2 ms |
6744 KB |
Output is correct |
64 |
Correct |
64 ms |
16468 KB |
Output is correct |
65 |
Correct |
84 ms |
16376 KB |
Output is correct |
66 |
Correct |
64 ms |
16468 KB |
Output is correct |
67 |
Correct |
73 ms |
16372 KB |
Output is correct |
68 |
Correct |
87 ms |
16336 KB |
Output is correct |
69 |
Correct |
64 ms |
16468 KB |
Output is correct |
70 |
Correct |
71 ms |
16768 KB |
Output is correct |
71 |
Correct |
86 ms |
16332 KB |
Output is correct |
72 |
Correct |
59 ms |
16464 KB |
Output is correct |
73 |
Correct |
85 ms |
29092 KB |
Output is correct |
74 |
Correct |
110 ms |
29128 KB |
Output is correct |
75 |
Correct |
58 ms |
16464 KB |
Output is correct |
76 |
Correct |
58 ms |
16212 KB |
Output is correct |
77 |
Correct |
59 ms |
16456 KB |
Output is correct |
78 |
Correct |
66 ms |
16212 KB |
Output is correct |
79 |
Correct |
78 ms |
16456 KB |
Output is correct |
80 |
Correct |
59 ms |
16212 KB |
Output is correct |
81 |
Correct |
55 ms |
16732 KB |
Output is correct |
82 |
Correct |
60 ms |
16600 KB |
Output is correct |
83 |
Correct |
73 ms |
21652 KB |
Output is correct |
84 |
Correct |
75 ms |
23636 KB |
Output is correct |
85 |
Correct |
74 ms |
23380 KB |
Output is correct |
86 |
Correct |
59 ms |
17804 KB |
Output is correct |
87 |
Correct |
102 ms |
17184 KB |
Output is correct |
88 |
Correct |
66 ms |
17748 KB |
Output is correct |
89 |
Correct |
63 ms |
17636 KB |
Output is correct |
90 |
Correct |
66 ms |
17492 KB |
Output is correct |
91 |
Correct |
68 ms |
17744 KB |
Output is correct |
92 |
Correct |
63 ms |
17936 KB |
Output is correct |
93 |
Correct |
65 ms |
20436 KB |
Output is correct |
94 |
Correct |
71 ms |
21964 KB |
Output is correct |
95 |
Correct |
69 ms |
20944 KB |
Output is correct |
96 |
Correct |
71 ms |
19076 KB |
Output is correct |
97 |
Correct |
56 ms |
17580 KB |
Output is correct |
98 |
Correct |
64 ms |
16980 KB |
Output is correct |
99 |
Correct |
80 ms |
16724 KB |
Output is correct |
100 |
Correct |
80 ms |
20092 KB |
Output is correct |
101 |
Correct |
62 ms |
18084 KB |
Output is correct |
102 |
Correct |
55 ms |
17304 KB |
Output is correct |
103 |
Correct |
63 ms |
16896 KB |
Output is correct |
104 |
Correct |
44 ms |
17340 KB |
Output is correct |
105 |
Correct |
91 ms |
25988 KB |
Output is correct |
106 |
Correct |
46 ms |
17100 KB |
Output is correct |
107 |
Correct |
44 ms |
17100 KB |
Output is correct |
108 |
Correct |
79 ms |
29780 KB |
Output is correct |
109 |
Correct |
67 ms |
17344 KB |
Output is correct |
110 |
Correct |
80 ms |
30032 KB |
Output is correct |
111 |
Correct |
54 ms |
16208 KB |
Output is correct |