#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int N = (int)2e5+2;
int n;
string gen;
vector<int> adj[N];
int dp[N][3];
int dfs(int s, int p){
vector<int> v; v.clear();
for(auto u : adj[s]){
if(u==p)continue;
v.pb(dfs(u,s));
}
if(!sz(v)) return (gen[s]=='1');
int sum = 0, best = 0;
for(auto u : v) sum+=u,best=max(best,u);
if(gen[s]=='0') return sum;
return max(sum-1,best);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].pb(b), adj[b].pb(a);
}
cin >> gen; gen='$'+gen; int ans = 0;
for(int i = 1; i <= n; i++) ans=max(ans,dfs(i,-1));
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5456 KB |
Output is correct |
2 |
Correct |
2 ms |
5200 KB |
Output is correct |
3 |
Correct |
3 ms |
5200 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
5372 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
5200 KB |
Output is correct |
8 |
Incorrect |
2 ms |
5200 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5456 KB |
Output is correct |
2 |
Correct |
2 ms |
5200 KB |
Output is correct |
3 |
Correct |
3 ms |
5200 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
5372 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
5200 KB |
Output is correct |
8 |
Incorrect |
2 ms |
5200 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5456 KB |
Output is correct |
2 |
Correct |
2 ms |
5200 KB |
Output is correct |
3 |
Correct |
3 ms |
5200 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
5372 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
5200 KB |
Output is correct |
8 |
Incorrect |
2 ms |
5200 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |