#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];
char v[N];
lli calc(lli u, lli p = -1){
lli res = 0, cc = 0, mx = 0;
for (auto & i : adj[u]) if (i != p){
lli xx = calc(i, u);
if (xx > 0) cc++;
res += xx;
mx = max(mx, xx);
}
if (v[u] == '1'){
ans = max({ans, res + (cc > 1 ? -1 : 1), mx + 1});
}
res = max(lli(v[u] - '0'), res - lli(v[u] - '0'));
return res;
}
void solve(){
cin >> n;
fore(i,1,n){
lli a, b; cin >> a >> b; a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
fore(i,0,n) cin >> v[i];
fore(i,0,n) if (v[i] == '1'){
calc(i);
cout << ans << endl;
exit(0);
}
cout << 0 << 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... |