#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2e5 + 10;
const ll mod = 1e9+7;
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n;
string s;
vector < int > adj[maxn];
/*
int dfs(int v , int par){
int res = 0;
for(int u : adj[v])if(u ^ par)
res += dfs(u , v);
if(s[v] == '1')
res = max(res - 1 , 1);
return res;
}
int solve(int v){
int res = 0;
for(auto u : adj[v])
res = max(res , dfs(u , v));
return res + 1;
}
*/
int sz[maxn];
bool hide[maxn];
void plant(int v){
sz[v] = 1, hide[v] = 1;
for(auto u : adj[v])if(!hide[u])
plant(u), sz[v] += sz[u];
hide[v] = 0;
}
int cen(int v , int n , int par = 0 , bool found = 0){
while(!found){
found = 1;
for(auto u : adj[v])if(!hide[u] and u^par and sz[u] * 2 > n){
found = 0 , par = v , v = u;
break;
}
}
return v;
}
int dfs(int v){
int res = 0;
hide[v] = 1;
for(int u : adj[v])if(!hide[u])
res += dfs(u);
hide[v] = 0;
if(s[v] == '1')
res = max(res - 1 , 1);
return res;
}
int Solve(int v){
hide[v] = 1;
int ans = 0;
if(s[v] == '1'){
int mx = 0 , sum = 0;
for(int u : adj[v])if(!hide[u]){
int res = dfs(u);
mx = max(mx , res);
sum += max(res , 0);
}
ans = max(ans, mx + 1);
ans = max(ans, sum - 1);
}
else{
for(int u : adj[v])if(!hide[u])
ans += max(0 , dfs(u));
}
return ans;
}
int solve(int v = 1){
plant(v);
v = cen(v , sz[v]);
int ans = Solve(v);
for(auto u : adj[v])if(!hide[u])
ans = max(ans, solve(u));
return ans;
}
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1 ; i < n ; i ++){
int u , v;
cin >> u >> v;
u -- , v --;
adj[u].pb(v);
adj[v].pb(u);
}
cin >> s;
cout << max(0 , solve());
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |