#include <bits/stdc++.h>
#include <chrono>
#include <math.h>
#pragma GCC optimize("-O3")
using namespace std;
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
#ifdef LOCAL
ostream &operator<<(ostream &out, string str) {
for (char c : str)
out << c;
return out;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; }
template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << '{';
for (auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) \
{}
#endif
const int MAXN = 1e6;
vector<int> G[MAXN];
string S;
int res = 1;
int n;
int dfs(int x, int p = -1) {
vector<int> Y;
for (int y : G[x])
if (y != p) {
int a = dfs(y, x);
if (a > 0)
Y.push_back(a);
}
if (Y.empty())
return S[x] == '1';
sort(all(Y));
int T = max(accumulate(all(Y), 0) - (S[x] == '1'), 1);
debug(x + 1, Y, T);
res = max(res, max(T, Y.back() + (S[x] == '1')));
return T;
}
int32_t main() {
_upgrade;
cin >> n;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
G[--a].push_back(--b);
G[b].push_back(a);
}
cin >> S;
dfs(0);
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23788 KB |
Output is correct |
4 |
Correct |
17 ms |
23788 KB |
Output is correct |
5 |
Correct |
17 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23788 KB |
Output is correct |
7 |
Correct |
16 ms |
23788 KB |
Output is correct |
8 |
Correct |
16 ms |
23796 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
16 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23788 KB |
Output is correct |
14 |
Correct |
16 ms |
23788 KB |
Output is correct |
15 |
Correct |
16 ms |
23788 KB |
Output is correct |
16 |
Correct |
16 ms |
23788 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
16 ms |
23788 KB |
Output is correct |
19 |
Correct |
16 ms |
23788 KB |
Output is correct |
20 |
Correct |
16 ms |
23788 KB |
Output is correct |
21 |
Correct |
16 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23788 KB |
Output is correct |
4 |
Correct |
17 ms |
23788 KB |
Output is correct |
5 |
Correct |
17 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23788 KB |
Output is correct |
7 |
Correct |
16 ms |
23788 KB |
Output is correct |
8 |
Correct |
16 ms |
23796 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
16 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23788 KB |
Output is correct |
14 |
Correct |
16 ms |
23788 KB |
Output is correct |
15 |
Correct |
16 ms |
23788 KB |
Output is correct |
16 |
Correct |
16 ms |
23788 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
16 ms |
23788 KB |
Output is correct |
19 |
Correct |
16 ms |
23788 KB |
Output is correct |
20 |
Correct |
16 ms |
23788 KB |
Output is correct |
21 |
Correct |
16 ms |
23788 KB |
Output is correct |
22 |
Correct |
18 ms |
23936 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
17 ms |
23916 KB |
Output is correct |
25 |
Correct |
17 ms |
23916 KB |
Output is correct |
26 |
Correct |
17 ms |
23916 KB |
Output is correct |
27 |
Correct |
17 ms |
23916 KB |
Output is correct |
28 |
Correct |
17 ms |
23916 KB |
Output is correct |
29 |
Correct |
17 ms |
24044 KB |
Output is correct |
30 |
Correct |
17 ms |
24044 KB |
Output is correct |
31 |
Correct |
17 ms |
23916 KB |
Output is correct |
32 |
Correct |
18 ms |
23916 KB |
Output is correct |
33 |
Correct |
17 ms |
23788 KB |
Output is correct |
34 |
Correct |
17 ms |
23916 KB |
Output is correct |
35 |
Correct |
18 ms |
23916 KB |
Output is correct |
36 |
Correct |
17 ms |
23916 KB |
Output is correct |
37 |
Correct |
17 ms |
23916 KB |
Output is correct |
38 |
Correct |
17 ms |
23916 KB |
Output is correct |
39 |
Correct |
17 ms |
24044 KB |
Output is correct |
40 |
Correct |
17 ms |
23916 KB |
Output is correct |
41 |
Correct |
17 ms |
24172 KB |
Output is correct |
42 |
Correct |
17 ms |
23916 KB |
Output is correct |
43 |
Correct |
17 ms |
23916 KB |
Output is correct |
44 |
Correct |
17 ms |
23916 KB |
Output is correct |
45 |
Correct |
17 ms |
23916 KB |
Output is correct |
46 |
Correct |
17 ms |
23916 KB |
Output is correct |
47 |
Correct |
17 ms |
23916 KB |
Output is correct |
48 |
Correct |
18 ms |
24044 KB |
Output is correct |
49 |
Correct |
17 ms |
24044 KB |
Output is correct |
50 |
Correct |
17 ms |
23916 KB |
Output is correct |
51 |
Correct |
17 ms |
23936 KB |
Output is correct |
52 |
Correct |
17 ms |
23916 KB |
Output is correct |
53 |
Correct |
17 ms |
23916 KB |
Output is correct |
54 |
Correct |
17 ms |
23916 KB |
Output is correct |
55 |
Correct |
17 ms |
23916 KB |
Output is correct |
56 |
Correct |
17 ms |
23936 KB |
Output is correct |
57 |
Correct |
17 ms |
24044 KB |
Output is correct |
58 |
Correct |
18 ms |
23916 KB |
Output is correct |
59 |
Correct |
17 ms |
23916 KB |
Output is correct |
60 |
Correct |
18 ms |
24044 KB |
Output is correct |
61 |
Correct |
17 ms |
23916 KB |
Output is correct |
62 |
Correct |
17 ms |
24044 KB |
Output is correct |
63 |
Correct |
18 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23788 KB |
Output is correct |
4 |
Correct |
17 ms |
23788 KB |
Output is correct |
5 |
Correct |
17 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23788 KB |
Output is correct |
7 |
Correct |
16 ms |
23788 KB |
Output is correct |
8 |
Correct |
16 ms |
23796 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
16 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23788 KB |
Output is correct |
14 |
Correct |
16 ms |
23788 KB |
Output is correct |
15 |
Correct |
16 ms |
23788 KB |
Output is correct |
16 |
Correct |
16 ms |
23788 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
16 ms |
23788 KB |
Output is correct |
19 |
Correct |
16 ms |
23788 KB |
Output is correct |
20 |
Correct |
16 ms |
23788 KB |
Output is correct |
21 |
Correct |
16 ms |
23788 KB |
Output is correct |
22 |
Correct |
18 ms |
23936 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
17 ms |
23916 KB |
Output is correct |
25 |
Correct |
17 ms |
23916 KB |
Output is correct |
26 |
Correct |
17 ms |
23916 KB |
Output is correct |
27 |
Correct |
17 ms |
23916 KB |
Output is correct |
28 |
Correct |
17 ms |
23916 KB |
Output is correct |
29 |
Correct |
17 ms |
24044 KB |
Output is correct |
30 |
Correct |
17 ms |
24044 KB |
Output is correct |
31 |
Correct |
17 ms |
23916 KB |
Output is correct |
32 |
Correct |
18 ms |
23916 KB |
Output is correct |
33 |
Correct |
17 ms |
23788 KB |
Output is correct |
34 |
Correct |
17 ms |
23916 KB |
Output is correct |
35 |
Correct |
18 ms |
23916 KB |
Output is correct |
36 |
Correct |
17 ms |
23916 KB |
Output is correct |
37 |
Correct |
17 ms |
23916 KB |
Output is correct |
38 |
Correct |
17 ms |
23916 KB |
Output is correct |
39 |
Correct |
17 ms |
24044 KB |
Output is correct |
40 |
Correct |
17 ms |
23916 KB |
Output is correct |
41 |
Correct |
17 ms |
24172 KB |
Output is correct |
42 |
Correct |
17 ms |
23916 KB |
Output is correct |
43 |
Correct |
17 ms |
23916 KB |
Output is correct |
44 |
Correct |
17 ms |
23916 KB |
Output is correct |
45 |
Correct |
17 ms |
23916 KB |
Output is correct |
46 |
Correct |
17 ms |
23916 KB |
Output is correct |
47 |
Correct |
17 ms |
23916 KB |
Output is correct |
48 |
Correct |
18 ms |
24044 KB |
Output is correct |
49 |
Correct |
17 ms |
24044 KB |
Output is correct |
50 |
Correct |
17 ms |
23916 KB |
Output is correct |
51 |
Correct |
17 ms |
23936 KB |
Output is correct |
52 |
Correct |
17 ms |
23916 KB |
Output is correct |
53 |
Correct |
17 ms |
23916 KB |
Output is correct |
54 |
Correct |
17 ms |
23916 KB |
Output is correct |
55 |
Correct |
17 ms |
23916 KB |
Output is correct |
56 |
Correct |
17 ms |
23936 KB |
Output is correct |
57 |
Correct |
17 ms |
24044 KB |
Output is correct |
58 |
Correct |
18 ms |
23916 KB |
Output is correct |
59 |
Correct |
17 ms |
23916 KB |
Output is correct |
60 |
Correct |
18 ms |
24044 KB |
Output is correct |
61 |
Correct |
17 ms |
23916 KB |
Output is correct |
62 |
Correct |
17 ms |
24044 KB |
Output is correct |
63 |
Correct |
18 ms |
23916 KB |
Output is correct |
64 |
Correct |
185 ms |
33360 KB |
Output is correct |
65 |
Correct |
184 ms |
33360 KB |
Output is correct |
66 |
Correct |
185 ms |
33348 KB |
Output is correct |
67 |
Correct |
186 ms |
33360 KB |
Output is correct |
68 |
Correct |
179 ms |
33348 KB |
Output is correct |
69 |
Correct |
176 ms |
33484 KB |
Output is correct |
70 |
Correct |
178 ms |
33368 KB |
Output is correct |
71 |
Correct |
166 ms |
33364 KB |
Output is correct |
72 |
Correct |
174 ms |
33484 KB |
Output is correct |
73 |
Correct |
207 ms |
50988 KB |
Output is correct |
74 |
Correct |
210 ms |
50860 KB |
Output is correct |
75 |
Correct |
187 ms |
33356 KB |
Output is correct |
76 |
Correct |
184 ms |
33236 KB |
Output is correct |
77 |
Correct |
189 ms |
33260 KB |
Output is correct |
78 |
Correct |
188 ms |
33356 KB |
Output is correct |
79 |
Correct |
183 ms |
33236 KB |
Output is correct |
80 |
Correct |
174 ms |
33228 KB |
Output is correct |
81 |
Correct |
153 ms |
34404 KB |
Output is correct |
82 |
Correct |
168 ms |
33644 KB |
Output is correct |
83 |
Correct |
195 ms |
40752 KB |
Output is correct |
84 |
Correct |
201 ms |
43704 KB |
Output is correct |
85 |
Correct |
194 ms |
43064 KB |
Output is correct |
86 |
Correct |
184 ms |
35244 KB |
Output is correct |
87 |
Correct |
181 ms |
34344 KB |
Output is correct |
88 |
Correct |
169 ms |
34664 KB |
Output is correct |
89 |
Correct |
172 ms |
34872 KB |
Output is correct |
90 |
Correct |
171 ms |
34444 KB |
Output is correct |
91 |
Correct |
169 ms |
34960 KB |
Output is correct |
92 |
Correct |
177 ms |
35224 KB |
Output is correct |
93 |
Correct |
168 ms |
39036 KB |
Output is correct |
94 |
Correct |
169 ms |
41560 KB |
Output is correct |
95 |
Correct |
160 ms |
39440 KB |
Output is correct |
96 |
Correct |
168 ms |
36760 KB |
Output is correct |
97 |
Correct |
155 ms |
34564 KB |
Output is correct |
98 |
Correct |
160 ms |
33884 KB |
Output is correct |
99 |
Correct |
181 ms |
33684 KB |
Output is correct |
100 |
Correct |
165 ms |
38168 KB |
Output is correct |
101 |
Correct |
171 ms |
35240 KB |
Output is correct |
102 |
Correct |
162 ms |
34172 KB |
Output is correct |
103 |
Correct |
171 ms |
33888 KB |
Output is correct |
104 |
Correct |
100 ms |
34164 KB |
Output is correct |
105 |
Correct |
198 ms |
46508 KB |
Output is correct |
106 |
Correct |
111 ms |
35552 KB |
Output is correct |
107 |
Correct |
108 ms |
35552 KB |
Output is correct |
108 |
Correct |
210 ms |
52268 KB |
Output is correct |
109 |
Correct |
106 ms |
34144 KB |
Output is correct |
110 |
Correct |
207 ms |
52524 KB |
Output is correct |
111 |
Correct |
176 ms |
33236 KB |
Output is correct |