//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
void dfs(ll v, ll p, vector<vector<ll>>& tree, string& s, vector<ll>& dp1, vector<ll>& dp2) {
ll mdp2 = 0, mdp1 = 0, sdp2 = 0, sdp1 = 0;
for (auto i : tree[v]) {
if (i != p) {
dfs(i, v, tree, s, dp1, dp2);
assign_max(mdp2, dp2[i]);
assign_max(mdp1, dp1[i]);
sdp1 += dp1[i];
sdp2 += dp2[i];
}
}
if (s[v] == '1') {
assign_max(dp2[v], 1ll);
assign_max(dp2[v], sdp2 - 1);
assign_max(dp1[v], mdp2 + 1);
assign_max(dp1[v], mdp1);
assign_max(dp1[v], sdp2 - 1);
} else {
dp2[v] = sdp2;
assign_max(dp1[v], mdp1);
assign_max(dp1[v], sdp2);
}
}
void solve() {
ll n;
cin >> n;
vector<vector<ll>> tree(n);
for (ll i = 1; i < n; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
string s;
cin >> s;
vector<ll> dp1(n, 0), dp2(n, 0);
dfs(0, 0, tree, s, dp1, dp2);
cout << dp1[0] << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:95:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
power.cpp:96:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
556 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
600 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
484 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
600 KB |
Output is correct |
51 |
Correct |
1 ms |
480 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
600 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
600 KB |
Output is correct |
59 |
Correct |
1 ms |
600 KB |
Output is correct |
60 |
Correct |
1 ms |
736 KB |
Output is correct |
61 |
Correct |
1 ms |
480 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
556 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
600 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
484 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
600 KB |
Output is correct |
51 |
Correct |
1 ms |
480 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
600 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
600 KB |
Output is correct |
59 |
Correct |
1 ms |
600 KB |
Output is correct |
60 |
Correct |
1 ms |
736 KB |
Output is correct |
61 |
Correct |
1 ms |
480 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
90 ms |
17388 KB |
Output is correct |
65 |
Correct |
98 ms |
17408 KB |
Output is correct |
66 |
Correct |
94 ms |
17636 KB |
Output is correct |
67 |
Correct |
85 ms |
17508 KB |
Output is correct |
68 |
Correct |
75 ms |
17452 KB |
Output is correct |
69 |
Correct |
74 ms |
17504 KB |
Output is correct |
70 |
Correct |
72 ms |
17316 KB |
Output is correct |
71 |
Correct |
70 ms |
17508 KB |
Output is correct |
72 |
Correct |
90 ms |
17360 KB |
Output is correct |
73 |
Correct |
96 ms |
34200 KB |
Output is correct |
74 |
Correct |
125 ms |
34000 KB |
Output is correct |
75 |
Correct |
89 ms |
18576 KB |
Output is correct |
76 |
Correct |
71 ms |
18580 KB |
Output is correct |
77 |
Correct |
75 ms |
17316 KB |
Output is correct |
78 |
Correct |
108 ms |
17420 KB |
Output is correct |
79 |
Correct |
95 ms |
17412 KB |
Output is correct |
80 |
Correct |
70 ms |
17292 KB |
Output is correct |
81 |
Correct |
60 ms |
17348 KB |
Output is correct |
82 |
Correct |
66 ms |
17532 KB |
Output is correct |
83 |
Correct |
85 ms |
24444 KB |
Output is correct |
84 |
Correct |
95 ms |
27548 KB |
Output is correct |
85 |
Correct |
83 ms |
27040 KB |
Output is correct |
86 |
Correct |
77 ms |
19360 KB |
Output is correct |
87 |
Correct |
103 ms |
18400 KB |
Output is correct |
88 |
Correct |
88 ms |
18636 KB |
Output is correct |
89 |
Correct |
85 ms |
18640 KB |
Output is correct |
90 |
Correct |
66 ms |
18200 KB |
Output is correct |
91 |
Correct |
72 ms |
18464 KB |
Output is correct |
92 |
Correct |
72 ms |
19108 KB |
Output is correct |
93 |
Correct |
74 ms |
22576 KB |
Output is correct |
94 |
Correct |
81 ms |
25028 KB |
Output is correct |
95 |
Correct |
85 ms |
22976 KB |
Output is correct |
96 |
Correct |
86 ms |
20216 KB |
Output is correct |
97 |
Correct |
65 ms |
18156 KB |
Output is correct |
98 |
Correct |
66 ms |
17496 KB |
Output is correct |
99 |
Correct |
70 ms |
17668 KB |
Output is correct |
100 |
Correct |
70 ms |
21632 KB |
Output is correct |
101 |
Correct |
70 ms |
18704 KB |
Output is correct |
102 |
Correct |
99 ms |
17684 KB |
Output is correct |
103 |
Correct |
71 ms |
17844 KB |
Output is correct |
104 |
Correct |
52 ms |
18372 KB |
Output is correct |
105 |
Correct |
104 ms |
29584 KB |
Output is correct |
106 |
Correct |
43 ms |
18112 KB |
Output is correct |
107 |
Correct |
46 ms |
18040 KB |
Output is correct |
108 |
Correct |
88 ms |
35216 KB |
Output is correct |
109 |
Correct |
44 ms |
18128 KB |
Output is correct |
110 |
Correct |
87 ms |
35636 KB |
Output is correct |
111 |
Correct |
105 ms |
17324 KB |
Output is correct |