#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 200'010;
int if_is_root[N];
bool has_gen[N];
vector<int> A[N];
int n;
int add_root_gen(int x, int rt)
{
if (has_gen[rt])
return max(x-1, 1);
else
return x;
}
void dfs1(int v, int p)
{
int sum = 0;
for (int u : A[v]) {
if (u == p)
continue;
dfs1(u, v);
sum += add_root_gen(if_is_root[u], u);
}
if_is_root[v] = sum;
}
int ans = 2;
void dfs2(int v, int p, int from_p)
{
int sum = from_p;
for (int u : A[v]) {
if (u == p)
continue;
sum += add_root_gen(if_is_root[u], u);
}
ans = max(ans, add_root_gen(sum, v));
for (int u : A[v]) {
if (u == p)
continue;
int from_u = add_root_gen(if_is_root[u], u);
dfs2(u, v, add_root_gen(sum - from_u, v));
}
}
void handle_edge()
{
int cnt = 0;
Loop (i,0,n)
cnt += has_gen[i];
if (cnt <= 2) {
cout << cnt << '\n';
exit(0);
}
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,1,n) {
int v, u;
cin >> v >> u;
--v; --u;
A[v].push_back(u);
A[u].push_back(v);
}
string s;
cin >> s;
Loop (i,0,n)
has_gen[i] = s[i]-'0';
handle_edge();
dfs1(0, -1);
dfs2(0, -1, 0);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 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 |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 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 |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5036 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
5 ms |
5088 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5060 KB |
Output is correct |
39 |
Correct |
6 ms |
5076 KB |
Output is correct |
40 |
Correct |
4 ms |
5080 KB |
Output is correct |
41 |
Correct |
4 ms |
5076 KB |
Output is correct |
42 |
Correct |
5 ms |
5036 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5088 KB |
Output is correct |
45 |
Correct |
4 ms |
5032 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5036 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
4 ms |
5076 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5032 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5036 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
5 ms |
5088 KB |
Output is correct |
57 |
Correct |
6 ms |
5204 KB |
Output is correct |
58 |
Correct |
4 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5040 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 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 |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5036 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
5 ms |
5088 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5060 KB |
Output is correct |
39 |
Correct |
6 ms |
5076 KB |
Output is correct |
40 |
Correct |
4 ms |
5080 KB |
Output is correct |
41 |
Correct |
4 ms |
5076 KB |
Output is correct |
42 |
Correct |
5 ms |
5036 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5088 KB |
Output is correct |
45 |
Correct |
4 ms |
5032 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5036 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
4 ms |
5076 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5032 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5036 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
5 ms |
5088 KB |
Output is correct |
57 |
Correct |
6 ms |
5204 KB |
Output is correct |
58 |
Correct |
4 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5040 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
141 ms |
15400 KB |
Output is correct |
65 |
Correct |
173 ms |
15428 KB |
Output is correct |
66 |
Correct |
199 ms |
15340 KB |
Output is correct |
67 |
Correct |
174 ms |
15380 KB |
Output is correct |
68 |
Correct |
161 ms |
15296 KB |
Output is correct |
69 |
Correct |
165 ms |
15348 KB |
Output is correct |
70 |
Correct |
168 ms |
15368 KB |
Output is correct |
71 |
Correct |
133 ms |
15384 KB |
Output is correct |
72 |
Correct |
88 ms |
14576 KB |
Output is correct |
73 |
Correct |
221 ms |
27984 KB |
Output is correct |
74 |
Correct |
212 ms |
27976 KB |
Output is correct |
75 |
Correct |
155 ms |
15244 KB |
Output is correct |
76 |
Correct |
171 ms |
15188 KB |
Output is correct |
77 |
Correct |
148 ms |
15240 KB |
Output is correct |
78 |
Correct |
137 ms |
15252 KB |
Output is correct |
79 |
Correct |
170 ms |
15340 KB |
Output is correct |
80 |
Correct |
169 ms |
15164 KB |
Output is correct |
81 |
Correct |
141 ms |
15648 KB |
Output is correct |
82 |
Correct |
133 ms |
15404 KB |
Output is correct |
83 |
Correct |
175 ms |
20488 KB |
Output is correct |
84 |
Correct |
196 ms |
22640 KB |
Output is correct |
85 |
Correct |
173 ms |
22296 KB |
Output is correct |
86 |
Correct |
148 ms |
16696 KB |
Output is correct |
87 |
Correct |
183 ms |
16120 KB |
Output is correct |
88 |
Correct |
163 ms |
16524 KB |
Output is correct |
89 |
Correct |
147 ms |
16664 KB |
Output is correct |
90 |
Correct |
134 ms |
16332 KB |
Output is correct |
91 |
Correct |
152 ms |
16588 KB |
Output is correct |
92 |
Correct |
146 ms |
17016 KB |
Output is correct |
93 |
Correct |
148 ms |
19352 KB |
Output is correct |
94 |
Correct |
143 ms |
21080 KB |
Output is correct |
95 |
Correct |
153 ms |
19656 KB |
Output is correct |
96 |
Correct |
158 ms |
17940 KB |
Output is correct |
97 |
Correct |
148 ms |
16440 KB |
Output is correct |
98 |
Correct |
160 ms |
15864 KB |
Output is correct |
99 |
Correct |
175 ms |
15744 KB |
Output is correct |
100 |
Correct |
164 ms |
18908 KB |
Output is correct |
101 |
Correct |
132 ms |
16896 KB |
Output is correct |
102 |
Correct |
149 ms |
16148 KB |
Output is correct |
103 |
Correct |
155 ms |
15960 KB |
Output is correct |
104 |
Correct |
95 ms |
16232 KB |
Output is correct |
105 |
Correct |
221 ms |
24716 KB |
Output is correct |
106 |
Correct |
68 ms |
16044 KB |
Output is correct |
107 |
Correct |
80 ms |
16112 KB |
Output is correct |
108 |
Correct |
102 ms |
14416 KB |
Output is correct |
109 |
Correct |
80 ms |
15380 KB |
Output is correct |
110 |
Correct |
225 ms |
29032 KB |
Output is correct |
111 |
Correct |
132 ms |
15244 KB |
Output is correct |