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 f first
#define s second
#define pb push_back
using namespace std;
const int N=3e5+5;
long long par[N],dp[N],dp1[N],n,a,b,bb,ans;
vector <long long> v[N];
string s;
void dfs (int a, int p) {
int ff=0;
for (int i=0; i<v[a].size(); i++) {
if (v[a][i]!=p) {
dfs(v[a][i],a);
dp[a]+=dp[v[a][i]];
if (s[a]=='1' && !ff) dp[a]--, ff=1;
dp1[a]+=dp1[v[a][i]];
}
}
dp1[a]++;
if (s[a]=='1') dp[a]=max(1LL, dp[a]);
if (s[a]=='0') dp[a]=max(0LL,dp[a]);
}
int main() {
cin>>n;
for (int i=1; i<=n-1; i++) {
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
cin>>s;
s="@"+s;
dfs(1,0);
for (int i=1; i<=n; i++) {
if (dp1[1]-dp1[i]>0) bb=1; else bb=0;
ans=max(ans,dp[i]+bb);
}
cout<<ans<<endl;
}
Compilation message (stderr)
power.cpp: In function 'void dfs(int, int)':
power.cpp:12:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (int i=0; i<v[a].size(); i++) {
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |