// She will give birth to a son, and you are to give him the name Jesus,
// because he will save his people from their sins.
// Matthew 1:21
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifdef DEBUG
#define debug(args...) printf(args)
#else
#define debug(args...)
#endif
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 200005
int n;
vi adj[MAXN];
char s[MAXN];
int memo[MAXN][2];
int dfs(int u, bool take, int p) {
if (memo[u][take] != -1) {
return memo[u][take];
}
int res = s[u] == '1';
int cur = 0;
for (int v : adj[u]) {
if (v == p) continue;
cur += dfs(v, 1, u);
}
cur -= s[u] == '1';
mxto(res, cur);
if (s[u] == '1' && !take) {
for (int v : adj[u]) {
if (v == p) continue;
mxto(res, 1 + dfs(v, 1, u));
}
}
if (!take) {
for (int v : adj[u]) {
if (v == p) continue;
mxto(res, dfs(v, 0, u));
}
}
return memo[u][take] = res;
}
int main() {
int _t = 1;
//scanf("%d", &_t);
while (_t--) {
scanf("%d", &n);
REP (i, 1, n) {
int a, b; scanf("%d%d", &a, &b);
adj[a].pb(b);
adj[b].pb(a);
}
scanf(" %s", s + 1);
memset(memo, -1, sizeof memo);
printf("%d\n", dfs(1, 0, -1));
}
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
power.cpp:78:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | int a, b; scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
power.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf(" %s", s + 1);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6568 KB |
Output is correct |
2 |
Correct |
3 ms |
6476 KB |
Output is correct |
3 |
Correct |
3 ms |
6476 KB |
Output is correct |
4 |
Correct |
3 ms |
6476 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
3 ms |
6476 KB |
Output is correct |
7 |
Correct |
3 ms |
6476 KB |
Output is correct |
8 |
Correct |
3 ms |
6476 KB |
Output is correct |
9 |
Correct |
3 ms |
6476 KB |
Output is correct |
10 |
Correct |
3 ms |
6476 KB |
Output is correct |
11 |
Correct |
3 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6476 KB |
Output is correct |
13 |
Correct |
3 ms |
6476 KB |
Output is correct |
14 |
Correct |
4 ms |
6476 KB |
Output is correct |
15 |
Correct |
3 ms |
6476 KB |
Output is correct |
16 |
Correct |
3 ms |
6476 KB |
Output is correct |
17 |
Correct |
3 ms |
6476 KB |
Output is correct |
18 |
Correct |
3 ms |
6540 KB |
Output is correct |
19 |
Correct |
3 ms |
6476 KB |
Output is correct |
20 |
Correct |
3 ms |
6476 KB |
Output is correct |
21 |
Correct |
3 ms |
6476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6568 KB |
Output is correct |
2 |
Correct |
3 ms |
6476 KB |
Output is correct |
3 |
Correct |
3 ms |
6476 KB |
Output is correct |
4 |
Correct |
3 ms |
6476 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
3 ms |
6476 KB |
Output is correct |
7 |
Correct |
3 ms |
6476 KB |
Output is correct |
8 |
Correct |
3 ms |
6476 KB |
Output is correct |
9 |
Correct |
3 ms |
6476 KB |
Output is correct |
10 |
Correct |
3 ms |
6476 KB |
Output is correct |
11 |
Correct |
3 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6476 KB |
Output is correct |
13 |
Correct |
3 ms |
6476 KB |
Output is correct |
14 |
Correct |
4 ms |
6476 KB |
Output is correct |
15 |
Correct |
3 ms |
6476 KB |
Output is correct |
16 |
Correct |
3 ms |
6476 KB |
Output is correct |
17 |
Correct |
3 ms |
6476 KB |
Output is correct |
18 |
Correct |
3 ms |
6540 KB |
Output is correct |
19 |
Correct |
3 ms |
6476 KB |
Output is correct |
20 |
Correct |
3 ms |
6476 KB |
Output is correct |
21 |
Correct |
3 ms |
6476 KB |
Output is correct |
22 |
Correct |
4 ms |
6604 KB |
Output is correct |
23 |
Correct |
4 ms |
6604 KB |
Output is correct |
24 |
Correct |
4 ms |
6604 KB |
Output is correct |
25 |
Correct |
4 ms |
6604 KB |
Output is correct |
26 |
Correct |
4 ms |
6604 KB |
Output is correct |
27 |
Correct |
5 ms |
6604 KB |
Output is correct |
28 |
Correct |
4 ms |
6604 KB |
Output is correct |
29 |
Correct |
4 ms |
6732 KB |
Output is correct |
30 |
Correct |
4 ms |
6732 KB |
Output is correct |
31 |
Correct |
4 ms |
6604 KB |
Output is correct |
32 |
Correct |
4 ms |
6604 KB |
Output is correct |
33 |
Correct |
4 ms |
6604 KB |
Output is correct |
34 |
Correct |
5 ms |
6604 KB |
Output is correct |
35 |
Correct |
4 ms |
6604 KB |
Output is correct |
36 |
Correct |
4 ms |
6604 KB |
Output is correct |
37 |
Correct |
4 ms |
6604 KB |
Output is correct |
38 |
Correct |
4 ms |
6604 KB |
Output is correct |
39 |
Correct |
4 ms |
6604 KB |
Output is correct |
40 |
Correct |
4 ms |
6604 KB |
Output is correct |
41 |
Correct |
4 ms |
6604 KB |
Output is correct |
42 |
Correct |
4 ms |
6604 KB |
Output is correct |
43 |
Correct |
4 ms |
6540 KB |
Output is correct |
44 |
Correct |
4 ms |
6580 KB |
Output is correct |
45 |
Correct |
4 ms |
6604 KB |
Output is correct |
46 |
Correct |
4 ms |
6612 KB |
Output is correct |
47 |
Correct |
4 ms |
6612 KB |
Output is correct |
48 |
Correct |
4 ms |
6612 KB |
Output is correct |
49 |
Correct |
4 ms |
6612 KB |
Output is correct |
50 |
Correct |
4 ms |
6612 KB |
Output is correct |
51 |
Correct |
4 ms |
6612 KB |
Output is correct |
52 |
Correct |
4 ms |
6604 KB |
Output is correct |
53 |
Correct |
4 ms |
6556 KB |
Output is correct |
54 |
Correct |
4 ms |
6616 KB |
Output is correct |
55 |
Correct |
4 ms |
6604 KB |
Output is correct |
56 |
Correct |
4 ms |
6604 KB |
Output is correct |
57 |
Correct |
5 ms |
6732 KB |
Output is correct |
58 |
Correct |
6 ms |
6604 KB |
Output is correct |
59 |
Correct |
4 ms |
6604 KB |
Output is correct |
60 |
Correct |
4 ms |
6604 KB |
Output is correct |
61 |
Correct |
3 ms |
6604 KB |
Output is correct |
62 |
Correct |
4 ms |
6732 KB |
Output is correct |
63 |
Correct |
4 ms |
6604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6568 KB |
Output is correct |
2 |
Correct |
3 ms |
6476 KB |
Output is correct |
3 |
Correct |
3 ms |
6476 KB |
Output is correct |
4 |
Correct |
3 ms |
6476 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
3 ms |
6476 KB |
Output is correct |
7 |
Correct |
3 ms |
6476 KB |
Output is correct |
8 |
Correct |
3 ms |
6476 KB |
Output is correct |
9 |
Correct |
3 ms |
6476 KB |
Output is correct |
10 |
Correct |
3 ms |
6476 KB |
Output is correct |
11 |
Correct |
3 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6476 KB |
Output is correct |
13 |
Correct |
3 ms |
6476 KB |
Output is correct |
14 |
Correct |
4 ms |
6476 KB |
Output is correct |
15 |
Correct |
3 ms |
6476 KB |
Output is correct |
16 |
Correct |
3 ms |
6476 KB |
Output is correct |
17 |
Correct |
3 ms |
6476 KB |
Output is correct |
18 |
Correct |
3 ms |
6540 KB |
Output is correct |
19 |
Correct |
3 ms |
6476 KB |
Output is correct |
20 |
Correct |
3 ms |
6476 KB |
Output is correct |
21 |
Correct |
3 ms |
6476 KB |
Output is correct |
22 |
Correct |
4 ms |
6604 KB |
Output is correct |
23 |
Correct |
4 ms |
6604 KB |
Output is correct |
24 |
Correct |
4 ms |
6604 KB |
Output is correct |
25 |
Correct |
4 ms |
6604 KB |
Output is correct |
26 |
Correct |
4 ms |
6604 KB |
Output is correct |
27 |
Correct |
5 ms |
6604 KB |
Output is correct |
28 |
Correct |
4 ms |
6604 KB |
Output is correct |
29 |
Correct |
4 ms |
6732 KB |
Output is correct |
30 |
Correct |
4 ms |
6732 KB |
Output is correct |
31 |
Correct |
4 ms |
6604 KB |
Output is correct |
32 |
Correct |
4 ms |
6604 KB |
Output is correct |
33 |
Correct |
4 ms |
6604 KB |
Output is correct |
34 |
Correct |
5 ms |
6604 KB |
Output is correct |
35 |
Correct |
4 ms |
6604 KB |
Output is correct |
36 |
Correct |
4 ms |
6604 KB |
Output is correct |
37 |
Correct |
4 ms |
6604 KB |
Output is correct |
38 |
Correct |
4 ms |
6604 KB |
Output is correct |
39 |
Correct |
4 ms |
6604 KB |
Output is correct |
40 |
Correct |
4 ms |
6604 KB |
Output is correct |
41 |
Correct |
4 ms |
6604 KB |
Output is correct |
42 |
Correct |
4 ms |
6604 KB |
Output is correct |
43 |
Correct |
4 ms |
6540 KB |
Output is correct |
44 |
Correct |
4 ms |
6580 KB |
Output is correct |
45 |
Correct |
4 ms |
6604 KB |
Output is correct |
46 |
Correct |
4 ms |
6612 KB |
Output is correct |
47 |
Correct |
4 ms |
6612 KB |
Output is correct |
48 |
Correct |
4 ms |
6612 KB |
Output is correct |
49 |
Correct |
4 ms |
6612 KB |
Output is correct |
50 |
Correct |
4 ms |
6612 KB |
Output is correct |
51 |
Correct |
4 ms |
6612 KB |
Output is correct |
52 |
Correct |
4 ms |
6604 KB |
Output is correct |
53 |
Correct |
4 ms |
6556 KB |
Output is correct |
54 |
Correct |
4 ms |
6616 KB |
Output is correct |
55 |
Correct |
4 ms |
6604 KB |
Output is correct |
56 |
Correct |
4 ms |
6604 KB |
Output is correct |
57 |
Correct |
5 ms |
6732 KB |
Output is correct |
58 |
Correct |
6 ms |
6604 KB |
Output is correct |
59 |
Correct |
4 ms |
6604 KB |
Output is correct |
60 |
Correct |
4 ms |
6604 KB |
Output is correct |
61 |
Correct |
3 ms |
6604 KB |
Output is correct |
62 |
Correct |
4 ms |
6732 KB |
Output is correct |
63 |
Correct |
4 ms |
6604 KB |
Output is correct |
64 |
Correct |
148 ms |
13116 KB |
Output is correct |
65 |
Correct |
157 ms |
13088 KB |
Output is correct |
66 |
Correct |
160 ms |
13308 KB |
Output is correct |
67 |
Correct |
161 ms |
13112 KB |
Output is correct |
68 |
Correct |
144 ms |
13064 KB |
Output is correct |
69 |
Correct |
149 ms |
13108 KB |
Output is correct |
70 |
Correct |
163 ms |
13140 KB |
Output is correct |
71 |
Correct |
144 ms |
13076 KB |
Output is correct |
72 |
Correct |
153 ms |
13124 KB |
Output is correct |
73 |
Correct |
194 ms |
28264 KB |
Output is correct |
74 |
Correct |
194 ms |
28204 KB |
Output is correct |
75 |
Correct |
189 ms |
12892 KB |
Output is correct |
76 |
Correct |
163 ms |
12996 KB |
Output is correct |
77 |
Correct |
168 ms |
12988 KB |
Output is correct |
78 |
Correct |
153 ms |
12980 KB |
Output is correct |
79 |
Correct |
171 ms |
12884 KB |
Output is correct |
80 |
Correct |
157 ms |
12996 KB |
Output is correct |
81 |
Correct |
128 ms |
13456 KB |
Output is correct |
82 |
Correct |
132 ms |
13156 KB |
Output is correct |
83 |
Correct |
168 ms |
19248 KB |
Output is correct |
84 |
Correct |
170 ms |
21808 KB |
Output is correct |
85 |
Correct |
183 ms |
21444 KB |
Output is correct |
86 |
Correct |
148 ms |
14696 KB |
Output is correct |
87 |
Correct |
153 ms |
13924 KB |
Output is correct |
88 |
Correct |
143 ms |
14460 KB |
Output is correct |
89 |
Correct |
161 ms |
14600 KB |
Output is correct |
90 |
Correct |
156 ms |
14228 KB |
Output is correct |
91 |
Correct |
141 ms |
14436 KB |
Output is correct |
92 |
Correct |
141 ms |
14788 KB |
Output is correct |
93 |
Correct |
155 ms |
18064 KB |
Output is correct |
94 |
Correct |
134 ms |
19900 KB |
Output is correct |
95 |
Correct |
148 ms |
18296 KB |
Output is correct |
96 |
Correct |
140 ms |
16068 KB |
Output is correct |
97 |
Correct |
145 ms |
14404 KB |
Output is correct |
98 |
Correct |
142 ms |
13640 KB |
Output is correct |
99 |
Correct |
144 ms |
13356 KB |
Output is correct |
100 |
Correct |
145 ms |
17168 KB |
Output is correct |
101 |
Correct |
159 ms |
14772 KB |
Output is correct |
102 |
Correct |
145 ms |
13868 KB |
Output is correct |
103 |
Correct |
145 ms |
13676 KB |
Output is correct |
104 |
Correct |
81 ms |
13752 KB |
Output is correct |
105 |
Correct |
185 ms |
24440 KB |
Output is correct |
106 |
Correct |
88 ms |
13784 KB |
Output is correct |
107 |
Correct |
83 ms |
13736 KB |
Output is correct |
108 |
Correct |
179 ms |
29068 KB |
Output is correct |
109 |
Correct |
81 ms |
13712 KB |
Output is correct |
110 |
Correct |
177 ms |
29508 KB |
Output is correct |
111 |
Correct |
135 ms |
12960 KB |
Output is correct |