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 <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Mat = vector<vector<ll>>;
const ll MOD = 1e9 + 7;
const int MAXN = 2e5 + 111;
const int K = 500;
int n;
vector<int> adj[MAXN];
bool czy_ma[MAXN];
int dp[MAXN];
int ans;
void dfs(int v, int p)
{
int s = 0;
for (auto u : adj[v])
{
if (u == p)
continue;
dfs(u, v);
s += dp[u];
}
if (czy_ma[v])
{
for (auto u : adj[v])
{
if (u == p)
continue;
ans = max(ans, dp[u] + 1);
}
dp[v] += s;
dp[v]--;
dp[v] = max(dp[v], 1);
}
else
{
dp[v] += s;
}
ans = max(ans, dp[v]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
//freopen("art2.in", "r", stdin);
//freopen("art2.out", "w", stdout);
cin >> n;
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++)
{
char c;
cin >> c;
czy_ma[i] = (c - '0');
}
dfs(1, 1);
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |