#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
return 0 <= x && 0 <= y && x < n && y < m;
}
template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }
vector <vector <int> > edge;
string s;
vector <int> dp[2];
inline void dfs (int v, int p = -1) {
dp[0][v] = s[v] == '1';
dp[1][v] = 0;
int sum = 0;
for (int to : edge[v]) {
if (to != p) {
dfs(to, v);
umax(dp[1][v], dp[1][to]);
if (s[v] == '1') {
umax(dp[1][v], dp[0][to] + 1);
}
sum += dp[0][to];
}
}
umax(dp[0][v], sum - bool(s[v] == '1'));
}
main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
edge.resize(n);
dp[0].resize(n);
dp[1].resize(n);
for (int i = 1, a, b; i < n; i++) {
cin >> a >> b;
--a, --b;
edge[a].pb(b);
edge[b].pb(a);
}
cin >> s;
dfs(0);
cout << max(dp[0][0], dp[1][0]) << '\n';
}
Compilation message
power.cpp:71:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main()
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
2 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
2 ms |
536 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
1 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
512 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
2 ms |
640 KB |
Output is correct |
63 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
2 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
2 ms |
536 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
1 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
512 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
2 ms |
640 KB |
Output is correct |
63 |
Correct |
2 ms |
512 KB |
Output is correct |
64 |
Correct |
172 ms |
16204 KB |
Output is correct |
65 |
Correct |
171 ms |
16216 KB |
Output is correct |
66 |
Correct |
165 ms |
16208 KB |
Output is correct |
67 |
Correct |
172 ms |
16092 KB |
Output is correct |
68 |
Correct |
166 ms |
16208 KB |
Output is correct |
69 |
Correct |
165 ms |
16088 KB |
Output is correct |
70 |
Correct |
175 ms |
16244 KB |
Output is correct |
71 |
Correct |
168 ms |
16096 KB |
Output is correct |
72 |
Correct |
169 ms |
16216 KB |
Output is correct |
73 |
Correct |
196 ms |
28728 KB |
Output is correct |
74 |
Correct |
196 ms |
28728 KB |
Output is correct |
75 |
Correct |
161 ms |
16092 KB |
Output is correct |
76 |
Correct |
172 ms |
16008 KB |
Output is correct |
77 |
Correct |
176 ms |
16084 KB |
Output is correct |
78 |
Correct |
181 ms |
16220 KB |
Output is correct |
79 |
Correct |
182 ms |
16056 KB |
Output is correct |
80 |
Correct |
173 ms |
16000 KB |
Output is correct |
81 |
Correct |
138 ms |
16372 KB |
Output is correct |
82 |
Correct |
183 ms |
16280 KB |
Output is correct |
83 |
Correct |
187 ms |
21164 KB |
Output is correct |
84 |
Correct |
192 ms |
23368 KB |
Output is correct |
85 |
Correct |
195 ms |
22980 KB |
Output is correct |
86 |
Correct |
171 ms |
17464 KB |
Output is correct |
87 |
Correct |
187 ms |
16820 KB |
Output is correct |
88 |
Correct |
152 ms |
17268 KB |
Output is correct |
89 |
Correct |
161 ms |
17260 KB |
Output is correct |
90 |
Correct |
167 ms |
17020 KB |
Output is correct |
91 |
Correct |
166 ms |
17284 KB |
Output is correct |
92 |
Correct |
184 ms |
17660 KB |
Output is correct |
93 |
Correct |
160 ms |
19972 KB |
Output is correct |
94 |
Correct |
166 ms |
21876 KB |
Output is correct |
95 |
Correct |
154 ms |
20336 KB |
Output is correct |
96 |
Correct |
158 ms |
18612 KB |
Output is correct |
97 |
Correct |
158 ms |
17020 KB |
Output is correct |
98 |
Correct |
184 ms |
16628 KB |
Output is correct |
99 |
Correct |
188 ms |
16472 KB |
Output is correct |
100 |
Correct |
166 ms |
19492 KB |
Output is correct |
101 |
Correct |
154 ms |
17536 KB |
Output is correct |
102 |
Correct |
161 ms |
16772 KB |
Output is correct |
103 |
Correct |
163 ms |
16620 KB |
Output is correct |
104 |
Correct |
100 ms |
16876 KB |
Output is correct |
105 |
Correct |
210 ms |
25400 KB |
Output is correct |
106 |
Correct |
97 ms |
16748 KB |
Output is correct |
107 |
Correct |
104 ms |
16748 KB |
Output is correct |
108 |
Correct |
195 ms |
29632 KB |
Output is correct |
109 |
Correct |
106 ms |
16876 KB |
Output is correct |
110 |
Correct |
203 ms |
29752 KB |
Output is correct |
111 |
Correct |
166 ms |
16100 KB |
Output is correct |