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;
#define int int64_t
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define ar array
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define f first
#define s second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define trav(i, a) for (auto& i : a)
template <class T> istream &operator>>(istream& in, vector<T> &v) {trav(i, v) in >> i; return in;}
#ifdef LOCAL
#include "../../lib/debug.h"
#else
#define dbg(...)
#endif
const int N = 2e5 + 5;
vi g[N];
int dp[N]; // no vertical path
string s;
int ans = 0;
void dfs(int u, int p) {
int sum = 0, mx = 0;
trav(v, g[u]) if (v != p) {
dfs(v, u);
sum += dp[v]; mx = max(mx, dp[v]);
}
if (s[u] == '1') {
ans = max(ans, max(sum - 1, mx + 1));
dp[u] = sum - 1;
if (dp[u] < 1) dp[u] = 1;
} else {
ans = max(ans, sum);
dp[u] = sum;
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
FOR(i, 0, n - 1) {
int u, v; cin >> u >> v;
g[u].pb(v); g[v].pb(u);
}
cin >> s; s = '$' + s;
dfs(1, 0);
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |