//#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 = 2005;
int n;
vector<int> v[N];
int dp[N][N], a[N];
void Dfs(int g, int p)
{
for (auto to : v[g])
{
if (to != p)
{
Dfs(to, g);
}
}
if (a[g] == 0)
{
int gum = 0;
for (auto to : v[g])
{
if (to != p)
{
int ma = 0;
for (int i = 0; i <= n; i++)
{
dp[g][i] = max(dp[g][i], dp[to][i]);
ma = max(ma, dp[to][i] - 2 * i);
}
gum += ma;
}
}
dp[g][0] = max(gum, dp[g][0]);
}
else
{
int gum = 0;
for (auto to : v[g])
{
if (to != p)
{
int ma = 0;
for (int i = 0; i <= n; i++)
{
dp[g][1] = max(dp[g][1], dp[to][i] - 2 * i + 1);
dp[g][i + 1] = max(dp[to][i], dp[g][i + 1]);
ma = max(ma, dp[to][i] - 2 * i);
}
gum += ma;
}
}
dp[g][0] = max(dp[g][0], gum - 1);
dp[g][0] = max(dp[g][0], 1);
}
}
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';
}
Dfs(1, -1);
int ans = 0;
for (int i = 0; i <= n; i++)
{
ans = max(dp[1][i], ans);
}
cout << ans;
}
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:83:12: warning: unused variable 'j' [-Wunused-variable]
83 | int i, j, x, y;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
380 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
0 ms |
380 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
380 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
0 ms |
380 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
20 ms |
13012 KB |
Output is correct |
23 |
Correct |
19 ms |
13000 KB |
Output is correct |
24 |
Correct |
17 ms |
13012 KB |
Output is correct |
25 |
Correct |
13 ms |
12884 KB |
Output is correct |
26 |
Correct |
13 ms |
12940 KB |
Output is correct |
27 |
Correct |
12 ms |
13012 KB |
Output is correct |
28 |
Correct |
13 ms |
13012 KB |
Output is correct |
29 |
Correct |
21 ms |
16244 KB |
Output is correct |
30 |
Correct |
18 ms |
16212 KB |
Output is correct |
31 |
Correct |
20 ms |
12248 KB |
Output is correct |
32 |
Correct |
16 ms |
12244 KB |
Output is correct |
33 |
Correct |
20 ms |
14156 KB |
Output is correct |
34 |
Correct |
17 ms |
14164 KB |
Output is correct |
35 |
Correct |
14 ms |
14164 KB |
Output is correct |
36 |
Correct |
13 ms |
14160 KB |
Output is correct |
37 |
Correct |
18 ms |
11496 KB |
Output is correct |
38 |
Correct |
15 ms |
12372 KB |
Output is correct |
39 |
Correct |
21 ms |
13584 KB |
Output is correct |
40 |
Correct |
16 ms |
13540 KB |
Output is correct |
41 |
Correct |
14 ms |
13516 KB |
Output is correct |
42 |
Correct |
16 ms |
13132 KB |
Output is correct |
43 |
Correct |
13 ms |
13140 KB |
Output is correct |
44 |
Correct |
22 ms |
12256 KB |
Output is correct |
45 |
Correct |
18 ms |
12244 KB |
Output is correct |
46 |
Correct |
15 ms |
12228 KB |
Output is correct |
47 |
Correct |
15 ms |
12200 KB |
Output is correct |
48 |
Correct |
16 ms |
12300 KB |
Output is correct |
49 |
Correct |
16 ms |
12376 KB |
Output is correct |
50 |
Correct |
15 ms |
12372 KB |
Output is correct |
51 |
Correct |
15 ms |
12372 KB |
Output is correct |
52 |
Correct |
16 ms |
12500 KB |
Output is correct |
53 |
Correct |
20 ms |
11852 KB |
Output is correct |
54 |
Correct |
20 ms |
12244 KB |
Output is correct |
55 |
Correct |
20 ms |
12492 KB |
Output is correct |
56 |
Correct |
18 ms |
8404 KB |
Output is correct |
57 |
Correct |
17 ms |
16156 KB |
Output is correct |
58 |
Correct |
18 ms |
8532 KB |
Output is correct |
59 |
Correct |
15 ms |
8428 KB |
Output is correct |
60 |
Correct |
18 ms |
16180 KB |
Output is correct |
61 |
Correct |
10 ms |
8448 KB |
Output is correct |
62 |
Correct |
14 ms |
16212 KB |
Output is correct |
63 |
Correct |
20 ms |
10960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
380 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
0 ms |
380 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
20 ms |
13012 KB |
Output is correct |
23 |
Correct |
19 ms |
13000 KB |
Output is correct |
24 |
Correct |
17 ms |
13012 KB |
Output is correct |
25 |
Correct |
13 ms |
12884 KB |
Output is correct |
26 |
Correct |
13 ms |
12940 KB |
Output is correct |
27 |
Correct |
12 ms |
13012 KB |
Output is correct |
28 |
Correct |
13 ms |
13012 KB |
Output is correct |
29 |
Correct |
21 ms |
16244 KB |
Output is correct |
30 |
Correct |
18 ms |
16212 KB |
Output is correct |
31 |
Correct |
20 ms |
12248 KB |
Output is correct |
32 |
Correct |
16 ms |
12244 KB |
Output is correct |
33 |
Correct |
20 ms |
14156 KB |
Output is correct |
34 |
Correct |
17 ms |
14164 KB |
Output is correct |
35 |
Correct |
14 ms |
14164 KB |
Output is correct |
36 |
Correct |
13 ms |
14160 KB |
Output is correct |
37 |
Correct |
18 ms |
11496 KB |
Output is correct |
38 |
Correct |
15 ms |
12372 KB |
Output is correct |
39 |
Correct |
21 ms |
13584 KB |
Output is correct |
40 |
Correct |
16 ms |
13540 KB |
Output is correct |
41 |
Correct |
14 ms |
13516 KB |
Output is correct |
42 |
Correct |
16 ms |
13132 KB |
Output is correct |
43 |
Correct |
13 ms |
13140 KB |
Output is correct |
44 |
Correct |
22 ms |
12256 KB |
Output is correct |
45 |
Correct |
18 ms |
12244 KB |
Output is correct |
46 |
Correct |
15 ms |
12228 KB |
Output is correct |
47 |
Correct |
15 ms |
12200 KB |
Output is correct |
48 |
Correct |
16 ms |
12300 KB |
Output is correct |
49 |
Correct |
16 ms |
12376 KB |
Output is correct |
50 |
Correct |
15 ms |
12372 KB |
Output is correct |
51 |
Correct |
15 ms |
12372 KB |
Output is correct |
52 |
Correct |
16 ms |
12500 KB |
Output is correct |
53 |
Correct |
20 ms |
11852 KB |
Output is correct |
54 |
Correct |
20 ms |
12244 KB |
Output is correct |
55 |
Correct |
20 ms |
12492 KB |
Output is correct |
56 |
Correct |
18 ms |
8404 KB |
Output is correct |
57 |
Correct |
17 ms |
16156 KB |
Output is correct |
58 |
Correct |
18 ms |
8532 KB |
Output is correct |
59 |
Correct |
15 ms |
8428 KB |
Output is correct |
60 |
Correct |
18 ms |
16180 KB |
Output is correct |
61 |
Correct |
10 ms |
8448 KB |
Output is correct |
62 |
Correct |
14 ms |
16212 KB |
Output is correct |
63 |
Correct |
20 ms |
10960 KB |
Output is correct |
64 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
65 |
Halted |
0 ms |
0 KB |
- |