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>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int NMAX = 2e5 + 1;
int dp[NMAX][3];
char c[NMAX];
vector <vector <int> > g(NMAX);
int ans = 0;
void dfs(int v, int e = -1){
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
}
int x = c[v] - '0';
int sum = -x;
int mx = 0;
for(int to : g[v]){
if(to == e) continue;
sum += max(dp[to][1], dp[to][0]);
mx = max(mx, dp[to][1]);
}
if(x) dp[v][1] = max(sum, 1), ans = max({ans, sum, 1, mx});
else dp[v][1] = sum, ans = max(ans, sum);
if(x){
int mx = 0;
int mx1 = 0;
int sum = 0;
for(int to : g[v]){
mx = max(mx, dp[to][2]);
mx1 = max(mx1, dp[to][1]);
}
dp[v][2] = max(mx1 + 1, mx - 1);
ans = max(ans, mx);
ans = max(ans, mx1 + 1);
}
else{
int mx = 0;
for(int to : g[v]){
mx = max(mx, dp[to][2]);
}
dp[v][2] = mx;
ans = max(ans, mx);
}
}
int main(){
int n; cin >> n;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i = 1; i <= n; i++)
cin >> c[i];
dfs(1);
cout << ans;
return 0;
}
Compilation message (stderr)
power.cpp: In function 'void dfs(int, int)':
power.cpp:37:13: warning: unused variable 'sum' [-Wunused-variable]
37 | int sum = 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... |