//#include <bits/stdc++.h>
#include <random>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#define fr first
#define sc second
#define ad push_back
using namespace std;
using ll = long long;
mt19937 rnd(348502);
const int N = 200005;
int n;
vector<int> v[N];
int dp[N], a[N];
void Dfs(int g, int p)
{
for (auto to : v[g])
{
if (to != p)
{
Dfs(to, g);
}
}
if (p == -1)
{
int ma = 0, gum = 0;
for(auto to: v[g])
{
ma = max(ma, dp[to]);
gum += dp[to];
}
dp[g] = max(ma + 1, gum - 1);
return;
}
int gum = 0;
for (auto to : v[g])
{
if (to != p)
gum += dp[to];
}
if (a[g] == 1)
dp[g] = max(gum - 1, 1);
else
dp[g] = gum;
}
int ans = 0;
void Dfs1(int g, int p, int arj)
{
int gum = arj, ma = arj, ma1 = 0;
for (auto to : v[g])
{
if (to != p)
{
gum += dp[to];
if (ma <= dp[to])
{
ma1 = max(ma, ma1);
ma = max(ma, dp[to]);
}
else
{
ma1 = max(ma1, dp[to]);
}
ma = max(ma, dp[to]);
}
}
if (a[g] == 1)
{
ans = max(max(ans, ma + 1), max(1, gum - 1));
}
for (auto to : v[g])
{
if (to != p)
{
if (a[to] == 1)
{
if (dp[to] == ma)
Dfs1(to, g, max(1, gum - dp[to] - 1));
else
Dfs1(to, g, max(1, gum - dp[to] - 1));
}
else
{
if (dp[to] == ma)
Dfs1(to, g, gum - dp[to]);
else
Dfs1(to, g, gum - dp[to]);
}
}
}
}
void solve()
{
int i, j, x, y;
cin >> n;
for (i = 0; i < n - 1; i++)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
char c;
for (i = 1; i <= n; i++)
{
cin >> c;
a[i] = c - '0';
}
for ( i = 1; i <= n; i++)
{
if (a[i] == 1)
{
Dfs(i, -1);
Dfs1(i, -1, 0);
cout << ans;
break;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
return 0;
}
Compilation message
power.cpp: In function 'void solve()':
power.cpp:114:12: warning: unused variable 'j' [-Wunused-variable]
114 | int i, j, x, y;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4904 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4904 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5088 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5052 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5024 KB |
Output is correct |
38 |
Correct |
7 ms |
5048 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
3 ms |
5100 KB |
Output is correct |
52 |
Correct |
3 ms |
5048 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5000 KB |
Output is correct |
62 |
Correct |
3 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4904 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5088 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5052 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5024 KB |
Output is correct |
38 |
Correct |
7 ms |
5048 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
3 ms |
5100 KB |
Output is correct |
52 |
Correct |
3 ms |
5048 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5000 KB |
Output is correct |
62 |
Correct |
3 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
84 ms |
15616 KB |
Output is correct |
65 |
Correct |
85 ms |
15564 KB |
Output is correct |
66 |
Correct |
81 ms |
15660 KB |
Output is correct |
67 |
Correct |
113 ms |
15644 KB |
Output is correct |
68 |
Correct |
114 ms |
15564 KB |
Output is correct |
69 |
Correct |
88 ms |
15648 KB |
Output is correct |
70 |
Correct |
82 ms |
15604 KB |
Output is correct |
71 |
Correct |
124 ms |
15604 KB |
Output is correct |
72 |
Correct |
80 ms |
15680 KB |
Output is correct |
73 |
Correct |
117 ms |
28264 KB |
Output is correct |
74 |
Correct |
111 ms |
23392 KB |
Output is correct |
75 |
Correct |
85 ms |
15436 KB |
Output is correct |
76 |
Correct |
79 ms |
15496 KB |
Output is correct |
77 |
Correct |
107 ms |
15484 KB |
Output is correct |
78 |
Correct |
120 ms |
15488 KB |
Output is correct |
79 |
Correct |
78 ms |
15540 KB |
Output is correct |
80 |
Correct |
91 ms |
15432 KB |
Output is correct |
81 |
Correct |
71 ms |
15796 KB |
Output is correct |
82 |
Correct |
75 ms |
15688 KB |
Output is correct |
83 |
Correct |
95 ms |
20708 KB |
Output is correct |
84 |
Correct |
101 ms |
22816 KB |
Output is correct |
85 |
Correct |
135 ms |
23336 KB |
Output is correct |
86 |
Correct |
84 ms |
16588 KB |
Output is correct |
87 |
Correct |
85 ms |
16636 KB |
Output is correct |
88 |
Correct |
80 ms |
16808 KB |
Output is correct |
89 |
Correct |
112 ms |
16832 KB |
Output is correct |
90 |
Correct |
102 ms |
16568 KB |
Output is correct |
91 |
Correct |
81 ms |
17000 KB |
Output is correct |
92 |
Correct |
81 ms |
17128 KB |
Output is correct |
93 |
Correct |
89 ms |
19604 KB |
Output is correct |
94 |
Correct |
115 ms |
21212 KB |
Output is correct |
95 |
Correct |
81 ms |
20004 KB |
Output is correct |
96 |
Correct |
95 ms |
18296 KB |
Output is correct |
97 |
Correct |
77 ms |
16720 KB |
Output is correct |
98 |
Correct |
78 ms |
16220 KB |
Output is correct |
99 |
Correct |
98 ms |
16000 KB |
Output is correct |
100 |
Correct |
85 ms |
19132 KB |
Output is correct |
101 |
Correct |
77 ms |
17160 KB |
Output is correct |
102 |
Correct |
74 ms |
16332 KB |
Output is correct |
103 |
Correct |
79 ms |
16184 KB |
Output is correct |
104 |
Correct |
50 ms |
16448 KB |
Output is correct |
105 |
Correct |
114 ms |
31140 KB |
Output is correct |
106 |
Correct |
54 ms |
16188 KB |
Output is correct |
107 |
Correct |
55 ms |
16172 KB |
Output is correct |
108 |
Correct |
107 ms |
31120 KB |
Output is correct |
109 |
Correct |
67 ms |
16348 KB |
Output is correct |
110 |
Correct |
111 ms |
31140 KB |
Output is correct |
111 |
Correct |
80 ms |
15516 KB |
Output is correct |