/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"
typedef long long ll;
typedef long double ld;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string s;
int dp[maxn];
int re[maxn];
vector<int>G[maxn];
int sum[maxn];
int n;
void dfs(int u,int pa){
for(auto v:G[u]){
if(v==pa)continue;
dfs(v,u);
sum[u]+=dp[v];
}
dp[u]=max(s[u]-'0',sum[u]-(s[u]-'0'));
}
int ans=0;
void re_dfs(int u,int pa){
if(u!=1){
re[u]=max(s[pa]-'0',sum[pa]-dp[u]+re[pa]-(s[pa]-'0'));
}
if(s[u]=='1'){
ans=max(ans,max(1,re[u]+sum[u]-1));
}
else{
ans=max(ans,re[u]+sum[u]);
}
for(auto v:G[u]){
if(v==pa)continue;
re_dfs(v,u);
}
}
signed main()
{
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
//freopen(task".inp" , "r" , stdin);
//freopen(task".out" , "w" , stdout);
cin>>n;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
cin>>s;
s=' '+s;
dfs(1,-1);
re_dfs(1,-1);
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23860 KB |
Output is correct |
2 |
Correct |
13 ms |
23776 KB |
Output is correct |
3 |
Correct |
16 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23776 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23836 KB |
Output is correct |
8 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23860 KB |
Output is correct |
2 |
Correct |
13 ms |
23776 KB |
Output is correct |
3 |
Correct |
16 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23776 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23836 KB |
Output is correct |
8 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23860 KB |
Output is correct |
2 |
Correct |
13 ms |
23776 KB |
Output is correct |
3 |
Correct |
16 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23776 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23836 KB |
Output is correct |
8 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |