#include<bits/stdc++.h>
using namespace std;
vector<int> dp;
vector<bool> power;
vector<vector<int> > adj;
int ans = 0;
void dfs(int u, int p){
if(power[u]){
int cur = 0;
for(int v : adj[u]){
if(v != p){
dfs(v, u);
dp[u] += dp[v];
cur = max(cur, dp[v] + 1);
}
}
ans = max(ans, cur);
dp[u]--;
dp[u] = max(dp[u], 1);
ans = max(ans, dp[u]);
}
else{
for(int v : adj[u]){
if(v != p){
dfs(v, u);
dp[u] += dp[v];
}
}
ans = max(ans, dp[u]);
}
}
int main(){
int n;
cin >> n;
dp.resize(1 + n);
power.resize(1 + n);
adj.resize(1 + n);
for(int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
string s;
cin >> s;
for(int i = 0; i < n; i++){
if(s[i] == '1'){
power[i + 1] = true;
}
}
dfs(1, 0);
cout << ans << "\n";
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |