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 ("Ofast")
typedef long long ll;
const ll maxn = 2e5 + 17;
vector<ll> adj[maxn];
string s;
ll dp[maxn] , ans = 0;
void DFS(ll r , ll par){
ll h = 0 , m = -1;
for(auto i : adj[r]){
if(i == par) continue;
DFS(i , r);
h += dp[i];
m = max(m , dp[i]);
}
ans = max(ans , m + (s[r] == '1'));
if(h == 0){
dp[r] = (s[r] == '1');
} else {
dp[r] = max((ll)(s[r] == '1') , h - (s[r] == '1'));
}
ans = max(ans , dp[r]);
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n;
cin>>n;
for(ll i = 1 ; i < n ; i++){
ll v , u;
cin>>v>>u; v--; u--;
adj[v].push_back(u); adj[u].push_back(v);
}
cin>>s;
DFS(0 , -1);
cout<<ans<<'\n';
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... |