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 fi first
#define se second
#define ll long long
using namespace std ;
const int N = 2e5 ;
int n, s, ans, dp[N + 1] ;
char c[N + 1] ;
vector<int> v[N + 1] ;
void dfs(int city, int last)
{
int sum = 0, mx = 0 ;
for(int i : v[city])
{
if(i == last)
continue ;
dfs(i, city) ;
sum += max(0, dp[i]) ;
mx = max(mx, dp[i]) ;
}
dp[city] = max(c[city] - '0', sum - ((c[city] ^ 1) == '0')) ;
if(c[city] == '1')
ans = max(ans, mx + 1) ;
ans = max(ans, dp[city]) ;
}
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
for(int i = 1 ; i < n ; i++)
{
int a, b ;
cin >> a >> b ;
v[a].push_back(b) ;
v[b].push_back(a) ;
}
for(int i = 1 ; i <= n ; i++)
cin >> c[i] ;
dfs(1, 0) ;
cout << ans ;
return 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... |