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;
typedef long long ll;
typedef long double ld;
const int N = (int) 2e5 + 7;
int n;
vector<int> g[N];
int v[N];
int dp[N];
int sol;
void dfs(int a, int p)
{
for (auto &b : g[a])
{
if (b != p)
{
dfs(b, a);
}
}
dp[a] = -v[a];
for (auto &b : g[a])
{
if (b != p)
{
dp[a] += dp[b];
}
}
dp[a] = max(dp[a], v[a]);
}
void reroot(int a, int p)
{
dp[a] = -v[a];
for (auto &b : g[a])
{
if (b != p)
{
dp[a] += dp[b];
}
}
dp[a] = max(dp[a], v[a]);
dp[p] = -v[p];
for (auto &b : g[p])
{
dp[p] += dp[b];
}
dp[p] = max(dp[p], v[p]);
}
void dfs_solve(int a, int p)
{
sol = max(sol, dp[a]);
for (auto &b : g[a])
{
if (b != p)
{
reroot(a, b);
dfs_solve(b, a);
reroot(b, a);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
string s;
cin >> s;
for (int i = 1; i <= n; i++)
{
if (s[i - 1] == '1')
{
v[i] = 1;
sol++;
}
}
sol = min(sol, 2);
dfs(1, -1);
dfs_solve(1, -1);
cout << sol << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |