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>
#define F first
#define S second
#define all(a) begin(a), end(a)
using namespace std;
int const N = 200005;
int n;
vector<int> adj[N];
string s;
void input() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> s;
}
int ans = 0;
int dp(int ver, int par) {
int ret = s[ver] == '1';
int suma = 0;
for (auto u : adj[ver]) if (u != par) {
int a = dp(u, ver);
if (a > 0) suma += a;
if (s[ver] == '1') {
ans = max(ans, a + 1);
}
}
ret = max(ret, suma - (s[ver] == '1'));
ans = max(ans, ret);
return ret;
}
void solve() {
dp(0, -1);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
input();
solve();
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... |