//#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], 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(gum - dp[to] - 1, ma1 + 1));
else
Dfs1(to, g, max(gum - dp[to] - 1, ma + 1));
}
else
{
if (dp[to] == ma)
Dfs1(to, g, max(gum - dp[to], ma1));
else
Dfs1(to, g, max(gum - dp[to], ma));
}
}
}
}
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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |