#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
typedef long long llong;
const int MAXN = 2e5 + 10;
const int INF = 2e9;
int n;
int a[MAXN];
int b[MAXN];
int sz[MAXN];
char s[MAXN];
int dp[MAXN][2];
bool bl[MAXN][2];
std::vector <int> g[MAXN];
void buildDFS(int node, int par)
{
for (int &u : g[node])
{
if (u == par)
{
std::swap(u, g[node].back());
g[node].pop_back();
break;
}
}
sz[node] = s[node] - '0';
for (const int &u : g[node])
{
buildDFS(u, node);
sz[node] += sz[u];
}
}
int f(int node, bool parentHas)
{
if (bl[node][parentHas])
{
return dp[node][parentHas];
}
bl[node][parentHas] = true;
dp[node][parentHas] = std::max(dp[node][parentHas], (int)(s[node] == '1'));
if (!parentHas && s[node] == '1')
{
for (const int &u : g[node])
{
dp[node][parentHas] = std::max(dp[node][parentHas], f(u, true) + 1);
}
}
int curr = 0;
for (const int &u : g[node])
{
curr += f(u, true);
}
dp[node][parentHas] = std::max(dp[node][parentHas], curr - (s[node] == '1'));
return dp[node][parentHas];
}
void solve()
{
buildDFS(1, 0);
std::cout << f(1, 0) << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i < n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
std::cin >> s + 1;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
power.cpp: In function 'void input()':
power.cpp:89:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | std::cin >> s + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |