#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int mx = 200'000;
int N;
vi edge[1+mx];
int DP[1+mx][3];
vi power;
vi children[1+mx];
int res = 1;
void dfs(int u, int p)
{
for(int v : edge[u])
{
if(v == p) continue;
children[u].push_back(v);
dfs(v, u);
}
if(power[u]) DP[u][0] = 1;
else DP[u][0] = 0;
if(power[u]) DP[u][1] = -1;
else DP[u][1] = 0;
for(int v : children[u])
DP[u][1] += max({DP[v][0], DP[v][1], 0});
int curr;
if(power[u]) curr = 1;
else curr = 0;
int currres = -1'000'000'000;
for(int v : children[u])
currres = max(currres, curr + DP[v][1]);
res = max(res, currres);
if(power[u]) curr = -1;
else curr = 1;
for(int v : children[u])
curr += max(0, DP[v][1]);
res = max(res, curr);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for(int e = 1; e <= N-1; e++)
{
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
power = vi(1+N);
for(int i = 1; i <= N; i++)
{
char c;
cin >> c;
if(c == '1') power[i] = 1;
else power[i] = 0;
}
dfs(1, 0);
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
6 ms |
9724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
6 ms |
9724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
6 ms |
9724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |