This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |