This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
int N;
vector <int> adj[N_MAX + 2];
bool plant[N_MAX + 2];
int down[N_MAX + 2];
int sumDown[N_MAX + 2];
void dfsDown (int u, int parent = -1) {
for (int v : adj[u]) {
if (v != parent) {
dfsDown(v, u);
sumDown[u] += down[v];
}
}
down[u] = sumDown[u];
if (plant[u] == true) {
down[u] = max(sumDown[u] - 1, 1);
}
}
int up[N_MAX + 2];
void dfsUp (int u, int parent = -1) {
if (parent != -1) {
up[u] = up[parent] + sumDown[parent] - down[u];
if (plant[parent] == true) {
up[u] = max(up[u] - 1, 1);
}
}
for (int v : adj[u]) {
if (v != parent) {
dfsUp(v, u);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
string str;
cin >> str;
for (int u = 1; u <= N; u++) {
plant[u] = (str[u - 1] == '1');
}
int cntPlants = 0;
for (int u = 1; u <= N; u++) {
cntPlants += plant[u];
}
dfsDown(1);
dfsUp(1);
int answer = min(cntPlants, 2);
for (int u = 1; u <= N; u++) {
int full = up[u] + sumDown[u];
if (plant[u] == true) {
full = max(full - 1, 1);
}
answer = max(answer, full);
}
cout << answer << "\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... |