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;
typedef long long ll;
const int N = 222222;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> g[N];
int dp[N][2];
bool lamp[N];
void dfs(int v, int p){
dp[v][1] = lamp[v];
int sum = (lamp[v] ? -1 : 0);
for (int u : g[v]){
if (u == p) continue;
dfs(u, v);
if (lamp[u]){
sum += max({1, dp[u][0], dp[u][1] - lamp[u]});
} else sum += dp[u][0];
dp[v][0] = max({dp[v][0], dp[u][0], dp[u][1]});
dp[v][1] = max({dp[v][1], dp[u][0] + 1, dp[u][1]});
}
dp[v][0] = max(dp[v][0], sum - lamp[v]);
if (!lamp[v]) dp[v][1] = 0;
}
void solve(){
int n;
cin >> n;
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
string s;
cin >> s;
for (int i = 0; i < n; i++) lamp[i + 1] = s[i] == '1';
dfs(1, 1);
//for (int i = 1; i <= n; i++) cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << endl; cout << endl;
cout << max(dp[1][0], dp[1][1]);
}
int main(){
//freopen("output.txt", "w", stdout);
int tests = 1;
//cin >> tests;
while (tests--){
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... |