#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
}
const int N = 2e5+5;
int n, m, e[N];
string s;
vector<int> tree[N];
int dp[N];
void dfs(int u, int p, int root) {
// only root
dp[u] = e[u];
// if (u == 5) {
// cout << e[u] << "\n";
// }
int sum = 0;
fore(v, tree[u]) if (v != p) {
dfs(v, u, root);
// only one child
if (u == root) dp[u] = max(dp[u], dp[v] + 1);
// take more than two child
sum += dp[v];
}
// if (u == 5) cout << dp[5] << "\n";
// if (u == 5) cout << sum - e[u] << "\n";
dp[u] = max(dp[u], sum - e[u]);
}
int main() {
setIO();
cin >> n;
foru(i, 2, n) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
cin >> s;
foru(i, 1, n) e[i] = (s[i-1] == '1');
// dfs(4, 4, 4);
// cout << dp[4] << " " << dp[3] << "\n";
// cout << dp[1] << " " << dp[2] << "\n";
// cout << dp[5] << "\n";
int ans = 0;
foru(i, 1, n) if (e[i]) {
dfs(i, i, i);
ans = max(ans, dp[i]);
// if (dp[i] == 4) {
// cout << "ddm\n";
// cout << i;
// return 0;
// }
}
cout << ans;
}
Compilation message
power.cpp: In function 'void setIO()':
power.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5772 KB |
Output is correct |
6 |
Correct |
1 ms |
5976 KB |
Output is correct |
7 |
Correct |
1 ms |
5724 KB |
Output is correct |
8 |
Correct |
1 ms |
5776 KB |
Output is correct |
9 |
Correct |
1 ms |
5980 KB |
Output is correct |
10 |
Correct |
1 ms |
5724 KB |
Output is correct |
11 |
Correct |
1 ms |
5776 KB |
Output is correct |
12 |
Correct |
1 ms |
5724 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5724 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
1 ms |
5724 KB |
Output is correct |
18 |
Correct |
2 ms |
5720 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
2 ms |
5724 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5772 KB |
Output is correct |
6 |
Correct |
1 ms |
5976 KB |
Output is correct |
7 |
Correct |
1 ms |
5724 KB |
Output is correct |
8 |
Correct |
1 ms |
5776 KB |
Output is correct |
9 |
Correct |
1 ms |
5980 KB |
Output is correct |
10 |
Correct |
1 ms |
5724 KB |
Output is correct |
11 |
Correct |
1 ms |
5776 KB |
Output is correct |
12 |
Correct |
1 ms |
5724 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5724 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
1 ms |
5724 KB |
Output is correct |
18 |
Correct |
2 ms |
5720 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
2 ms |
5724 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
22 |
Correct |
37 ms |
5724 KB |
Output is correct |
23 |
Correct |
35 ms |
5724 KB |
Output is correct |
24 |
Correct |
29 ms |
5724 KB |
Output is correct |
25 |
Correct |
19 ms |
5724 KB |
Output is correct |
26 |
Correct |
11 ms |
5724 KB |
Output is correct |
27 |
Correct |
6 ms |
5720 KB |
Output is correct |
28 |
Correct |
2 ms |
5844 KB |
Output is correct |
29 |
Correct |
58 ms |
6000 KB |
Output is correct |
30 |
Correct |
30 ms |
5976 KB |
Output is correct |
31 |
Correct |
33 ms |
5720 KB |
Output is correct |
32 |
Correct |
18 ms |
5724 KB |
Output is correct |
33 |
Correct |
35 ms |
5724 KB |
Output is correct |
34 |
Correct |
21 ms |
5972 KB |
Output is correct |
35 |
Correct |
11 ms |
5724 KB |
Output is correct |
36 |
Correct |
5 ms |
5724 KB |
Output is correct |
37 |
Correct |
23 ms |
5720 KB |
Output is correct |
38 |
Correct |
18 ms |
5872 KB |
Output is correct |
39 |
Correct |
46 ms |
5920 KB |
Output is correct |
40 |
Correct |
24 ms |
5724 KB |
Output is correct |
41 |
Correct |
13 ms |
5724 KB |
Output is correct |
42 |
Correct |
22 ms |
5724 KB |
Output is correct |
43 |
Correct |
6 ms |
5720 KB |
Output is correct |
44 |
Correct |
38 ms |
5724 KB |
Output is correct |
45 |
Correct |
29 ms |
5720 KB |
Output is correct |
46 |
Correct |
21 ms |
5720 KB |
Output is correct |
47 |
Correct |
11 ms |
5724 KB |
Output is correct |
48 |
Correct |
41 ms |
5724 KB |
Output is correct |
49 |
Correct |
41 ms |
5720 KB |
Output is correct |
50 |
Correct |
41 ms |
5720 KB |
Output is correct |
51 |
Correct |
45 ms |
5980 KB |
Output is correct |
52 |
Correct |
36 ms |
5724 KB |
Output is correct |
53 |
Correct |
27 ms |
5724 KB |
Output is correct |
54 |
Correct |
39 ms |
5724 KB |
Output is correct |
55 |
Correct |
36 ms |
5724 KB |
Output is correct |
56 |
Correct |
2 ms |
5724 KB |
Output is correct |
57 |
Correct |
2 ms |
5980 KB |
Output is correct |
58 |
Correct |
24 ms |
5724 KB |
Output is correct |
59 |
Correct |
24 ms |
5976 KB |
Output is correct |
60 |
Correct |
2 ms |
5980 KB |
Output is correct |
61 |
Correct |
2 ms |
5724 KB |
Output is correct |
62 |
Correct |
3 ms |
5980 KB |
Output is correct |
63 |
Correct |
30 ms |
5852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5772 KB |
Output is correct |
6 |
Correct |
1 ms |
5976 KB |
Output is correct |
7 |
Correct |
1 ms |
5724 KB |
Output is correct |
8 |
Correct |
1 ms |
5776 KB |
Output is correct |
9 |
Correct |
1 ms |
5980 KB |
Output is correct |
10 |
Correct |
1 ms |
5724 KB |
Output is correct |
11 |
Correct |
1 ms |
5776 KB |
Output is correct |
12 |
Correct |
1 ms |
5724 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5724 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
1 ms |
5724 KB |
Output is correct |
18 |
Correct |
2 ms |
5720 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
2 ms |
5724 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
22 |
Correct |
37 ms |
5724 KB |
Output is correct |
23 |
Correct |
35 ms |
5724 KB |
Output is correct |
24 |
Correct |
29 ms |
5724 KB |
Output is correct |
25 |
Correct |
19 ms |
5724 KB |
Output is correct |
26 |
Correct |
11 ms |
5724 KB |
Output is correct |
27 |
Correct |
6 ms |
5720 KB |
Output is correct |
28 |
Correct |
2 ms |
5844 KB |
Output is correct |
29 |
Correct |
58 ms |
6000 KB |
Output is correct |
30 |
Correct |
30 ms |
5976 KB |
Output is correct |
31 |
Correct |
33 ms |
5720 KB |
Output is correct |
32 |
Correct |
18 ms |
5724 KB |
Output is correct |
33 |
Correct |
35 ms |
5724 KB |
Output is correct |
34 |
Correct |
21 ms |
5972 KB |
Output is correct |
35 |
Correct |
11 ms |
5724 KB |
Output is correct |
36 |
Correct |
5 ms |
5724 KB |
Output is correct |
37 |
Correct |
23 ms |
5720 KB |
Output is correct |
38 |
Correct |
18 ms |
5872 KB |
Output is correct |
39 |
Correct |
46 ms |
5920 KB |
Output is correct |
40 |
Correct |
24 ms |
5724 KB |
Output is correct |
41 |
Correct |
13 ms |
5724 KB |
Output is correct |
42 |
Correct |
22 ms |
5724 KB |
Output is correct |
43 |
Correct |
6 ms |
5720 KB |
Output is correct |
44 |
Correct |
38 ms |
5724 KB |
Output is correct |
45 |
Correct |
29 ms |
5720 KB |
Output is correct |
46 |
Correct |
21 ms |
5720 KB |
Output is correct |
47 |
Correct |
11 ms |
5724 KB |
Output is correct |
48 |
Correct |
41 ms |
5724 KB |
Output is correct |
49 |
Correct |
41 ms |
5720 KB |
Output is correct |
50 |
Correct |
41 ms |
5720 KB |
Output is correct |
51 |
Correct |
45 ms |
5980 KB |
Output is correct |
52 |
Correct |
36 ms |
5724 KB |
Output is correct |
53 |
Correct |
27 ms |
5724 KB |
Output is correct |
54 |
Correct |
39 ms |
5724 KB |
Output is correct |
55 |
Correct |
36 ms |
5724 KB |
Output is correct |
56 |
Correct |
2 ms |
5724 KB |
Output is correct |
57 |
Correct |
2 ms |
5980 KB |
Output is correct |
58 |
Correct |
24 ms |
5724 KB |
Output is correct |
59 |
Correct |
24 ms |
5976 KB |
Output is correct |
60 |
Correct |
2 ms |
5980 KB |
Output is correct |
61 |
Correct |
2 ms |
5724 KB |
Output is correct |
62 |
Correct |
3 ms |
5980 KB |
Output is correct |
63 |
Correct |
30 ms |
5852 KB |
Output is correct |
64 |
Execution timed out |
1525 ms |
16020 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |