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>
using namespace std;
const int N = 2e5 + 5;
vector <int> g[N];
int dp[N], n;
char a[N];
int dfs(int nod, int dad)
{
int st = a[nod] - '0', bst_son = 0, bst = 0;
for (auto x : g[nod])
{
if (x == dad)
continue;
bst = max(bst, dfs(x, nod));
dp[nod] += dp[x];
bst_son = max(bst_son, dp[x]);
}
dp[nod] = max(st, dp[nod] - st); ///a or b...
return max(bst, max(dp[nod], bst_son + st));
}
int main()
{
cin >> n;
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> a + 1;
cout << dfs(1, 0);
return 0;
}
Compilation message (stderr)
power.cpp: In function 'int main()':
power.cpp:32:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | cin >> a + 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |