#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 200005
#define ALL(x) x.begin(), x.end()
int n, ans, f[N], h[N], pwr[N];
vector<int> g[N];
char s[N];
void dfs(int u, int p)
{
if (s[u]=='1')++pwr[u], f[u]=1;
int C=0,F=0;
for (auto v : g[u]) if (v^p)
{
dfs(v, u);
pwr[u] += pwr[v];
C+=!!pwr[v];
F+=f[v];
}
if (s[u]=='1')
{
f[u]=max(F-(C>1), 1);
}
else f[u]=max(f[u],F);
}
void dfs2(int u, int p)
{
int sib=h[u];
for (auto v:g[u])if (v^p)
{
sib+=f[v];
}
if(s[u]=='1') sib=max(sib-1,1);
if(s[u]=='1')h[u]=max(h[u],1);
for (auto v : g[u]) if (v^p)
{
if (s[v]=='1')h[v]=1;
h[v]=max(h[v],sib-f[v]);
dfs2(v, u);
}
}
void dfs3(int u, int p)
{
ans=max(ans, f[u]+h[p]);
for(auto v:g[u]) if(v-p)dfs3(v,u);
}
int main()
{
ShinLena;
cin >> n;
for (int i = 1, u, v; i < n; ++i) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
cin >> (s+1);
dfs(1, 0);
dfs2(1,0);
dfs3(1,0);
//for(int i=1;i<=n;++i) cerr<<f[i]<< ' '<<h[i]<<endl;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |