#include <bits/stdc++.h>
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
typedef unsigned short u16;
typedef short i16;
typedef unsigned int u32;
typedef int i32;
typedef unsigned long long u64;
typedef long long i64;
typedef float f32;
typedef double f64;
typedef long double f80;
typedef long double f128;
namespace std
{
template <typename T, typename U>
struct hash<pair<T, U>>
{
size_t operator()(const pair<T, U>& x) const { return hash<T>()(x.first) ^ hash<U>()(x.second); }
};
} // namespace std
template <typename T>
using limits = std::numeric_limits<T>;
template <typename T, typename U>
using pa = std::pair<T, U>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;
#ifdef LOCAL
#include <chrono>
#include "tools.hpp"
#define here(...) \
std::cerr << __func__ << ':' << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; \
_debug(__VA_ARGS__)
#else
#define here(...)
#endif
typedef std::vector<std::vector<u32>> AdjList;
AdjList adj;
std::vector<i32> dp, state;
i32 ans{};
void dfs(u32 u, u32 prev)
{
i32 sum{}, mx{};
for (auto& v : adj[u])
{
if (v != prev)
{
dfs(v, u);
sum += dp[v];
mx = std::max(mx, dp[v]);
}
}
if (state[u])
{
ans = std::max(ans, std::max(sum - 1, mx + 1));
}
else
{
ans = std::max(ans, sum);
}
dp[u] = std::max(state[u], sum - state[u]);
}
void solve()
{
u32 n;
std::cin >> n;
adj.assign(n, std::vector<u32>());
dp.assign(n, 0);
state.assign(n, 0);
for (u32 i = 0; i < n - 1; ++i)
{
u32 u, v;
std::cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (auto& i : state)
{
char ci;
std::cin >> ci;
i = ci - '0';
}
dfs(0, limits<u32>::max());
std::cout << ans << '\n';
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
#ifdef LOCAL
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
auto st = high_resolution_clock::now();
std::ifstream input("./in.txt");
std::ofstream output("./log/bsol.txt");
auto cin_buf = std::cin.rdbuf(input.rdbuf());
auto cout_buf = std::cout.rdbuf(output.rdbuf());
#endif
solve();
#ifdef LOCAL
std::cin.rdbuf(cin_buf);
std::cout.rdbuf(cout_buf);
input.close();
output.close();
auto ed = high_resolution_clock::now();
std::cerr << "Time elapsed: " << duration_cast<microseconds>(ed - st).count() << " microseconds\n";
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
456 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
464 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
440 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
452 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
448 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
436 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
460 KB |
Output is correct |
62 |
Correct |
1 ms |
468 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
456 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
464 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
440 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
452 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
448 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
436 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
460 KB |
Output is correct |
62 |
Correct |
1 ms |
468 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
70 ms |
14244 KB |
Output is correct |
65 |
Correct |
68 ms |
14352 KB |
Output is correct |
66 |
Correct |
84 ms |
14324 KB |
Output is correct |
67 |
Correct |
79 ms |
14344 KB |
Output is correct |
68 |
Correct |
67 ms |
14288 KB |
Output is correct |
69 |
Correct |
99 ms |
14360 KB |
Output is correct |
70 |
Correct |
68 ms |
14304 KB |
Output is correct |
71 |
Correct |
77 ms |
14268 KB |
Output is correct |
72 |
Correct |
105 ms |
14344 KB |
Output is correct |
73 |
Correct |
82 ms |
26860 KB |
Output is correct |
74 |
Correct |
123 ms |
26880 KB |
Output is correct |
75 |
Correct |
100 ms |
14156 KB |
Output is correct |
76 |
Correct |
73 ms |
14224 KB |
Output is correct |
77 |
Correct |
79 ms |
14252 KB |
Output is correct |
78 |
Correct |
97 ms |
14156 KB |
Output is correct |
79 |
Correct |
106 ms |
14276 KB |
Output is correct |
80 |
Correct |
82 ms |
14216 KB |
Output is correct |
81 |
Correct |
67 ms |
14620 KB |
Output is correct |
82 |
Correct |
64 ms |
14388 KB |
Output is correct |
83 |
Correct |
78 ms |
19404 KB |
Output is correct |
84 |
Correct |
91 ms |
21592 KB |
Output is correct |
85 |
Correct |
78 ms |
21176 KB |
Output is correct |
86 |
Correct |
84 ms |
15648 KB |
Output is correct |
87 |
Correct |
92 ms |
15044 KB |
Output is correct |
88 |
Correct |
64 ms |
15564 KB |
Output is correct |
89 |
Correct |
66 ms |
15508 KB |
Output is correct |
90 |
Correct |
124 ms |
15388 KB |
Output is correct |
91 |
Correct |
82 ms |
15600 KB |
Output is correct |
92 |
Correct |
68 ms |
15924 KB |
Output is correct |
93 |
Correct |
74 ms |
18408 KB |
Output is correct |
94 |
Correct |
79 ms |
20052 KB |
Output is correct |
95 |
Correct |
69 ms |
18676 KB |
Output is correct |
96 |
Correct |
65 ms |
16840 KB |
Output is correct |
97 |
Correct |
94 ms |
15432 KB |
Output is correct |
98 |
Correct |
73 ms |
14840 KB |
Output is correct |
99 |
Correct |
88 ms |
14632 KB |
Output is correct |
100 |
Correct |
69 ms |
17808 KB |
Output is correct |
101 |
Correct |
83 ms |
15780 KB |
Output is correct |
102 |
Correct |
98 ms |
15060 KB |
Output is correct |
103 |
Correct |
87 ms |
14896 KB |
Output is correct |
104 |
Correct |
46 ms |
14952 KB |
Output is correct |
105 |
Correct |
104 ms |
23724 KB |
Output is correct |
106 |
Correct |
48 ms |
14956 KB |
Output is correct |
107 |
Correct |
52 ms |
15032 KB |
Output is correct |
108 |
Correct |
82 ms |
27764 KB |
Output is correct |
109 |
Correct |
68 ms |
14944 KB |
Output is correct |
110 |
Correct |
86 ms |
27936 KB |
Output is correct |
111 |
Correct |
64 ms |
14140 KB |
Output is correct |