//#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] - 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] - i + 1);
dp[g][i + 1] = max(dp[to][i], dp[g][i + 1]);
ma = max(ma, dp[to][i] - 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 |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
380 KB |
Output is correct |
7 |
Correct |
1 ms |
380 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
380 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
380 KB |
Output is correct |
7 |
Correct |
1 ms |
380 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
380 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
380 KB |
Output is correct |
7 |
Correct |
1 ms |
380 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
380 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |