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 MX = 200042;
int isSwitch[MX];
vector<int> adj[MX];
int ans = 0;
int calc(int node, int par) {
int mx = 0;
int sum = 0;
for (auto& x: adj[node]) {
if (x != par) {
int v = calc(x, node);
mx = max(mx, v);
sum += v;
}
}
ans = max({ans, mx + isSwitch[node], sum - isSwitch[node]});
int comb = max(mx, isSwitch[node]);
if (mx > 1)
comb -= isSwitch[node];
return max({comb, sum - isSwitch[node]});
}
void solve(int tcId) {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++) {
char c;
cin >> c;
isSwitch[i] = c - '0';
}
ans = max(ans, calc(0, -1));
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
bool multi = false;
if (!multi) {
solve(42);
} else {
int t;
cin >> t;
while (t--)
solve(t);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |