#include "bits/stdc++.h"
using namespace std;
const int N=200001;
int n;
vector<int> adj[N];
int dp[N][2];
string s;
void dfs(int x, int par){
dp[x][1]=(s[x]=='1'?1:-1e9);
dp[x][0]=(s[x]=='1')*(-1);
for(int k:adj[x]){
if(k==par) continue;
dfs(k, x);
dp[x][0]+=max({0, dp[k][0], dp[k][1]});
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
s='0'+s;
dfs(1, 0);
int ans=max(1, dp[1][0]);
for(int k:adj[1]){
ans=max(ans, dp[1][1]+max(dp[k][0], dp[k][1]));
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
5016 KB |
Output is correct |
4 |
Correct |
2 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
5016 KB |
Output is correct |
4 |
Correct |
2 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
5016 KB |
Output is correct |
4 |
Correct |
2 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |