#include <bits/stdc++.h>
using namespace std;
const int nx=2e5+5;
int n, u, v, dp1[nx], dp2[nx];
vector<int> d[nx];
string s;
void dfs(int u, int p)
{
for (auto v:d[u])
{
if (v==p) continue;
dfs(v, u);
if (s[u-1]=='1') dp1[u]+=dp1[v], dp2[u]=max(dp2[u], dp1[v]+1);
else dp1[u]+=max(dp1[v], dp2[v]-2), dp2[u]=max(dp2[u], dp2[v]);
}
if (s[u-1]=='1') dp1[u]=max(1, dp1[u]-1), dp2[u]=max(dp2[u], 1);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
cin>>s;
dfs(1, 1);
cout<<max({dp1[1], dp2[1], 0});
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |