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>
//#pragma GCC optimize ("O2,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define pc __builtin_popcount
#define cl __builtin_clz
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define debug(x) cerr<<#x<<" : &"<<x<<"&\n"
#define wall() cerr<<'\n'<<"-------------------------------\n"
typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;
typedef pair<bool , pii> piii;
const int
MX=2e5+5,
M=600,
MOD = 1e9+7,
inf = 1e9+20,
//infl = 1e16,
LOG = 18,
A = 27,
del = 10067;
int n, dp[MX][3];
bool mark[MX];
vector<int> adj[MX];
// 0: vasat valid, 1: marz=vasat invalid, 2: birun
void dfs(int u, int p=0){
int cnt=0;
dp[u][0] = -mark[u];
dp[u][1] = -inf;
for(int v:adj[u]) if(v!=p) {
dfs(v, u);
if(max(max((mark[v]?1:-inf), dp[v][0]), dp[v][1]-2*mark[v])>-inf){
cnt++;
dp[u][0]+=max(max((mark[v]?1:-inf), dp[v][0]), dp[v][1]-2*mark[v]);
}
dp[u][1] = max(dp[u][1], mark[u]+max(max((mark[v]?1:-inf), dp[v][0]), dp[v][1]-2*mark[v]));
dp[u][2] = max(max(dp[u][2], dp[v][0]), max(dp[v][2], (mark[v]?dp[v][1]:-inf)));
dp[u][2] = max(dp[u][2], (int)mark[v]);
}
if(cnt<2) dp[u][0] = -inf;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
FOR(i, 1, n-1){
int u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
FOR(i, 1, n){
char c;
cin>>c;
mark[i] = c=='1';
}
dfs(1);
int res=0;
res = max(dp[1][0], res);
res = max(dp[1][2], res);
if(mark[1]) res = max(res, dp[1][1]);
if(mark[1] and !res) res=1;
cout<<res<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |