# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1212557 | cpdreamer | Power Plant (JOI20_power) | C++20 | 79 ms | 31168 KiB |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e12;
typedef long long ll;
const ll MOD=(ll) 998244353;
#define P pair
#define S second
#define F first
#define pb push_back
#define V vector
#define all(v) v.begin(),v.end()
void file() {
freopen("input.txt.txt", "r", stdin);
freopen("output.txt.txt", "w", stdout);
}
V<int>adj[(int)2e5+1];
int dp[(int)2e5+1][2][2];
string s;
void dfs(int n,int p){
int maxd=0;
int maxd1=0;
int sum=0;
for(auto u:adj[n]){
if(u==p)continue;
dfs(u,n);
maxd=max(maxd,max(dp[u][0][0],dp[u][1][0]));
maxd1=max(maxd1,max(dp[u][0][1],dp[u][1][1]));
sum+=max(dp[u][0][1],dp[u][1][1]);
}
dp[n][0][0]=max(0,max(maxd,sum-(s[n]=='1')));
dp[n][0][1]=max(0,sum-(s[n]=='1'));
dp[n][1][1]=(s[n]=='1');
dp[n][1][0]=max(0,maxd1+(s[n]=='1'));
}
void solve(){
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
cin>>s;
s='.'+s;
for(int i=1;i<=n;i++){
for(int j=0;j<2;j++){
for(int g=0;g<2;g++){
dp[i][j][g]=0;
}
}
}
dfs(1,-1);
int ans=0;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
ans=max(dp[1][i][j],ans);
}
}
cout<<ans<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
//file();
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |