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;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
const ll N = 2e5 + 50;
const ll Mod = 1e18;
ll n, dp[N][2];
string s;
vector<int> adj[N];
void dfs(int v, int p = 0){
for(int u: adj[v]){
if(u == p) continue;
dfs(u, v);
dp[v][1] += dp[u][1];
dp[v][0] = max(dp[v][0], dp[u][1]);
}
if(s[v] == '1'){
dp[v][1]--;
dp[v][0] = max(dp[v][1], dp[v][0] + 1);
dp[v][1] = max(dp[v][1], 1ll);
}
else{
dp[v][0] = dp[v][1];
}
//cout << dp[v][1] << sep << dp[v][0] << endl;
}
int main(){
fast_io;
cin >> n;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
cin >> s; s = "$" + s;
dfs(1);
ll res = 0;
for(int i = 1; i <= n; i++) res = max(res, dp[i][0]);
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |