#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 2*1e5;
int n, dp[MAX_N][2];
string s;
vector<int> adj[MAX_N];
void dfs(int id, int anc){
for(int i = 0; i<adj[id].size(); i++){
if(adj[id][i] == anc){
adj[id].erase(adj[id].begin() +i);
break;
}
}
for(auto e: adj[id]){
dfs(e, id);
}
}
void find_dp(int id){
dp[id][0]= 0;
dp[id][1] = 0;
bool gen = s[id] == '1';
int s1=0;
int best1= 0;
int best0= 0;
for(auto e: adj[id]){
find_dp(e);
s1 += dp[e][1];
best1 = max(best1, dp[e][1]);
best0 = max(best0, dp[e][0]);
}
if(!gen){
dp[id][0] = max(max(s1, best0), 0LL);
dp[id][1] = max(s1, 0LL);
}
else{
dp[id][0] = max(s1-1, best1+1);
dp[id][1] = max(s1-1, 1LL);
}
//cout<<id<<" "<<dp[id][0]<<" "<<dp[id][1]<<endl;
}
signed main(){
cin>>n;
for(int i = 0; i<n-1; i++){
int a, b;
cin>>a>>b;
a--;b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0, -1);
cin>>s;
find_dp(0);
cout<<dp[0][0]<<endl;
}
Compilation message
power.cpp: In function 'void dfs(long long int, long long int)':
power.cpp:12:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int i = 0; i<adj[id].size(); i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4928 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4928 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4928 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |