#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fore(i,a,b) for(lli i = (a), abcdxd = (b); i < abcdxd; i++)
#define f first
#define s second
#define pb push_back
#define ENDL '\n'
#define sz(s) lli((s).size())
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using namespace std;
typedef long long lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
typedef vector<ii> vii;
typedef long double ld;
#define deb(x) cout << #x << ": " << x << endl;
const lli N = 2e5 + 12;
lli n;
lli ans = 0;
vi adj[N];
bool pw[N];
lli dfs(lli u, lli p){
lli sum = 0, mx = 0;
for (auto i : adj[u]) if (i != p) {
lli xx = dfs(i, u);
sum += xx;
mx = max(mx, xx);
}
if (pw[u]){
ans = max({ans, sum - 1ll, mx + 1ll});
}
return max(sum - lli(pw[u]), lli(pw[u]));
}
void solve(){
cin >> n;
fore(i,0,n-1){
lli a, b; cin >> a >> b; a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
fore(i,0,n){
char c; cin >> c;
pw[i] = (c == '1');
}
fore(i,0,n) if (pw[i]) { dfs(i, -1); break; }
cout << ans << ENDL;
}
int main(){ _
// freopen("tracing.in", "r", stdin);
// freopen("tracing.out", "w", stdout);
// lli t; cin >> t;
// while (t--)
solve();
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... |