#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NAME"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(FNAME".inp","r")){
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
const int MAXN = 2e5 + 5;
int n;
vector<int> graph[MAXN];
vector<int> hasPower;
void init(){
cin>>n;
hasPower.assign(n+1,0);
for (int i=1;i<n;i++){
int u,v;
cin>>u>>v;
graph[u].push_back(v);
graph[v].push_back(u);
}
string str;
cin>>str;
for (int i=0;i<(int)str.size();i++) hasPower[i+1] = (str[i]=='1' ? 1 : 0);
}
// ? dp[u] maximum profit at node u
vector<int> dp;
int res;
void dfs(int u,int pre){
int curMax=0;
int curSum=0;
for (int v : graph[u]){
if (v^pre){
dfs(v,u);
maximize(curMax,dp[v]);
curSum+=dp[v];
}
}
if (hasPower[u]){
/*
* curMax + 1 = take the maximum path to its children + itself -> cannot be broken
* curSum - 1 = take all the children path - the repair fee of itself -> can be broken
*/
int opt = max(curMax+1,curSum-1);
maximize(res,opt);
} else{
maximize(res,curSum);
}
dp[u] = max(hasPower[u], curSum - hasPower[u]);
}
void sol(){
dp.assign(n+1,0);
dfs(1,1);
cout<<res;
}
int main(){
setup();
init();
sol();
}
Compilation message (stderr)
power.cpp: In function 'void setup()':
power.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
power.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |