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;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 5e5 + 23;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define sz(x) ((int)x.size())
#define kill(x) cout<<x , exit(0);
#define all(x) x.begin(),x.end()
#define lc (v<<1)
#define rc ((v<<1)|1)
#define debug(x) cerr<< #x << " : " << x << '\n';
#define int ll
int n;
int dp[N][2];
vector<int> G[N];
bool is[N];
void dfs(int v,int p=-1) {
int sum0 = 0,sum1 = 0,mx1=0,mx0=0;
for(int u : G[v]) if(u != p) {
dfs(u,v);
sum0 += dp[u][0];
sum1 += dp[u][1];
mx1= max(mx1,dp[u][1]);
mx0=max(mx0,dp[u][0]);
}
if(is[v]) {
dp[v][0] = max({mx0,sum1- 1,1 + mx1});
} else {
dp[v][0] = max({mx0,sum1});
}
if(is[v]) {
dp[v][1] = max({1LL,sum1-1});
} else {
dp[v][1] = sum1;
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
for(int i = 1; i< n ; i++) {
int v,u; cin>>v>>u;
G[v].pb(u);
G[u].pb(v);
}
for(int i = 1; i <= n ; i++) {
char c; cin>>c;
is[i] = (c == '1');
}
int ans =0;
dfs(1);
cout<<dp[1][0] << '\n';
return 0;
}
Compilation message (stderr)
power.cpp: In function 'int32_t main()':
power.cpp:62:6: warning: unused variable 'ans' [-Wunused-variable]
62 | int ans =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... |