#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int oo = 1e9;
const int N = 2e5 + 1;
int n, s[N], d[N][2];
vi adj[N];
void dfs(int u, int p) {
int s0 = 0, s1 = 0;
vi adjn;
EACH(v, adj[u]) {
if (v == p) continue;
dfs(v, u);
adjn.pb(v);
s0 += d[v][0];
s1 += d[v][1];
}
vi a;
if (s[u]) d[u][0] = max(d[u][0], 1);
EACH(v, adjn) {
d[u][0] = max(d[u][0], d[v][0]);
if (s[u]) d[u][0] = max(d[u][0], d[v][1] + 1);
a.pb(d[v][1]);
}
if (sz(a) > 1) {
sort(all(a), greater<int>());
int cur = a[0] + a[1];
d[u][0] = max(d[u][0], cur - s[u]);
FOR(i, 2, sz(a) - 1) {
cur += a[i];
d[u][0] = max(d[u][0], cur - s[u]);
}
}
if (s[u]) d[u][1] = max(d[u][1], 1);
d[u][1] = max(d[u][1], s1 - s[u]);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n;
FOR(i, 1, n - 1) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
FOR(i, 1, n) {
char c;
cin >> c;
s[i] = c - '0';
}
dfs(1, 0);
cout << d[1][0];
return 0;
}
/*
6
2 3
4 3
1 3
3 5
6 2
110011
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
power.cpp:58:2: note: in expansion of macro 'EACH'
58 | EACH(v, adj[u]) {
| ^~~~
power.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
power.cpp:67:2: note: in expansion of macro 'EACH'
67 | EACH(v, adjn) {
| ^~~~
power.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
power.cpp:76:3: note: in expansion of macro 'FOR'
76 | FOR(i, 2, sz(a) - 1) {
| ^~~
power.cpp: In function 'int main()':
power.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
power.cpp:89:5: note: in expansion of macro 'FOR'
89 | FOR(i, 1, n - 1) {
| ^~~
power.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
power.cpp:95:5: note: in expansion of macro 'FOR'
95 | FOR(i, 1, n) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 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 |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 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 |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5044 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5096 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5044 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5228 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5036 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5124 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
5 ms |
5172 KB |
Output is correct |
40 |
Correct |
4 ms |
5092 KB |
Output is correct |
41 |
Correct |
4 ms |
5204 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5204 KB |
Output is correct |
45 |
Correct |
4 ms |
5172 KB |
Output is correct |
46 |
Correct |
3 ms |
5172 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5212 KB |
Output is correct |
50 |
Correct |
4 ms |
5172 KB |
Output is correct |
51 |
Correct |
3 ms |
5204 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
4 ms |
5204 KB |
Output is correct |
54 |
Correct |
4 ms |
5092 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5300 KB |
Output is correct |
58 |
Correct |
4 ms |
5176 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5332 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 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 |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5044 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5096 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5044 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5228 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5036 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5124 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
5 ms |
5172 KB |
Output is correct |
40 |
Correct |
4 ms |
5092 KB |
Output is correct |
41 |
Correct |
4 ms |
5204 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5204 KB |
Output is correct |
45 |
Correct |
4 ms |
5172 KB |
Output is correct |
46 |
Correct |
3 ms |
5172 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5212 KB |
Output is correct |
50 |
Correct |
4 ms |
5172 KB |
Output is correct |
51 |
Correct |
3 ms |
5204 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
4 ms |
5204 KB |
Output is correct |
54 |
Correct |
4 ms |
5092 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5300 KB |
Output is correct |
58 |
Correct |
4 ms |
5176 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5332 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
150 ms |
16372 KB |
Output is correct |
65 |
Correct |
144 ms |
16472 KB |
Output is correct |
66 |
Correct |
157 ms |
16648 KB |
Output is correct |
67 |
Correct |
135 ms |
16392 KB |
Output is correct |
68 |
Correct |
139 ms |
16464 KB |
Output is correct |
69 |
Correct |
168 ms |
16468 KB |
Output is correct |
70 |
Correct |
164 ms |
16428 KB |
Output is correct |
71 |
Correct |
148 ms |
16524 KB |
Output is correct |
72 |
Correct |
142 ms |
16476 KB |
Output is correct |
73 |
Correct |
161 ms |
39180 KB |
Output is correct |
74 |
Correct |
171 ms |
39040 KB |
Output is correct |
75 |
Correct |
154 ms |
16332 KB |
Output is correct |
76 |
Correct |
150 ms |
16332 KB |
Output is correct |
77 |
Correct |
154 ms |
16296 KB |
Output is correct |
78 |
Correct |
155 ms |
16312 KB |
Output is correct |
79 |
Correct |
184 ms |
16356 KB |
Output is correct |
80 |
Correct |
150 ms |
16228 KB |
Output is correct |
81 |
Correct |
125 ms |
17760 KB |
Output is correct |
82 |
Correct |
150 ms |
17096 KB |
Output is correct |
83 |
Correct |
173 ms |
26160 KB |
Output is correct |
84 |
Correct |
162 ms |
30236 KB |
Output is correct |
85 |
Correct |
164 ms |
29636 KB |
Output is correct |
86 |
Correct |
154 ms |
19196 KB |
Output is correct |
87 |
Correct |
160 ms |
17972 KB |
Output is correct |
88 |
Correct |
131 ms |
18352 KB |
Output is correct |
89 |
Correct |
134 ms |
18144 KB |
Output is correct |
90 |
Correct |
131 ms |
17740 KB |
Output is correct |
91 |
Correct |
142 ms |
18176 KB |
Output is correct |
92 |
Correct |
143 ms |
18916 KB |
Output is correct |
93 |
Correct |
149 ms |
24012 KB |
Output is correct |
94 |
Correct |
132 ms |
26480 KB |
Output is correct |
95 |
Correct |
132 ms |
24536 KB |
Output is correct |
96 |
Correct |
143 ms |
20980 KB |
Output is correct |
97 |
Correct |
130 ms |
17996 KB |
Output is correct |
98 |
Correct |
156 ms |
16996 KB |
Output is correct |
99 |
Correct |
141 ms |
16876 KB |
Output is correct |
100 |
Correct |
138 ms |
22780 KB |
Output is correct |
101 |
Correct |
123 ms |
18984 KB |
Output is correct |
102 |
Correct |
126 ms |
17480 KB |
Output is correct |
103 |
Correct |
147 ms |
17116 KB |
Output is correct |
104 |
Correct |
82 ms |
19516 KB |
Output is correct |
105 |
Correct |
160 ms |
33484 KB |
Output is correct |
106 |
Correct |
81 ms |
19288 KB |
Output is correct |
107 |
Correct |
84 ms |
19256 KB |
Output is correct |
108 |
Correct |
173 ms |
40484 KB |
Output is correct |
109 |
Correct |
80 ms |
19520 KB |
Output is correct |
110 |
Correct |
177 ms |
41152 KB |
Output is correct |
111 |
Correct |
139 ms |
16288 KB |
Output is correct |