#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN=3e5+5;
const int mod=998244353;
const int mxlogN=20;
const int mxK=26;
const int inf=2e9;
const int K=600;
vector<int> adj[mxN];
string s;
int dp[2][mxN];
int solve(int i, bool b, int p)
{
if(~dp[b][i]) return dp[b][i];
if(!b)
{
int ans=0, ans2=0, ans3=0;
for(int j:adj[i])
{
if(j==p) continue;
ans=max(ans,solve(j,0,i));
ans2=max(ans2,solve(j,1,i));
ans3+=solve(j,1,i);
}
ans=max(ans,ans3);
if(s[i]=='1') ans=max(ans,ans2+1);
return dp[b][i]=ans;
}
int ans=0, ans2=0;
if(s[i]=='1') ans=1;
for(int j:adj[i])
{
if(j==p) continue;
ans2+=solve(j,1,i);
}
ans=max(ans,ans2-(s[i]=='1'));
return dp[b][i]=ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for(int i=0; i<n; i++) dp[0][i]=dp[1][i]=-1;
for(int i=1; i<n; i++)
{
int u,v; cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
cout << solve(0,0,0) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7376 KB |
Output is correct |
12 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7376 KB |
Output is correct |
12 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7376 KB |
Output is correct |
12 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |