#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int NMAX = 2e5 + 1;
int dp[NMAX][3];
char c[NMAX];
vector <vector <int> > g(NMAX);
int ans = 0;
void dfs(int v, int e = -1){
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
}
int x = c[v] - '0';
int sum = -x;
for(int to : g[v]){
if(to == e) continue;
sum += max(dp[to][1], dp[to][0]);
}
if(x) dp[v][1] = max(sum, 1), ans = max({ans, sum + x, 1});
else dp[v][1] = sum, ans = max(ans, sum);
if(x){
int mx = 0;
int mx1 = 0;
int sum = 0;
for(int to : g[v]){
mx = max(mx, dp[to][2]);
mx1 = max(mx1, dp[to][1]);
}
dp[v][2] = max(mx1 + 1, mx - 1);
ans = max(ans, mx);
ans = max(ans, mx1 + 1);
}
else{
int mx = 0;
for(int to : g[v]){
mx = max(mx, dp[to][2]);
}
dp[v][2] = mx;
ans = max(ans, mx);
}
}
int main(){
int n; cin >> n;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i = 1; i <= n; i++)
cin >> c[i];
dfs(1);
cout << ans;
return 0;
}
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:35:13: warning: unused variable 'sum' [-Wunused-variable]
35 | int sum = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |