This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("bmi,bmi2,lzcnt,popcnt")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
//#pragma expected_value
//#pragma isolated_call
//#pragma disjoint
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
//#define int long long
//#define double long double
#define Fi first
#define Se second
#define Rep(i,a,b) for (int i=a;i<=b;++i)
#define Repu(i,b,a) for (int i=b;i>=a;--i)
#define pb push_back
#define ms(a,i) memset(a,i,sizeof(a))
#define sz size()
#define mp make_pair
#define endl '\n'
#define sef setprecision(12)<<fixed
#define cer cout<<"cak"<<endl;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<double> va;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<va> vva;
//const double EPS=1e-9;
const double PI=acos(-1);
//const int oo=1e18;
const int MN=2e5+5;
const int mod=1e9+7;
using cd=complex<double>;
//typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> index_set;
int n;
vi adj[MN];
char s[MN];
int siz[MN];
void dfs(int u,int pre) {
int sum = 0;
for(int v:adj[u]) {
if(v==pre) continue;
dfs(v,u);
sum += siz[v];
}
if(s[u]=='1') {
sum--;
siz[u] = max(1,sum);
}
else {
siz[u] = sum;
}
}
void solve(int u) {
for(int v:adj[u]) {
dfs(v,u);
siz[u] = max(siz[u], siz[v]);
}
siz[u]++;
}
void process(int u) {
for(int v:adj[u]) {
dfs(v,u);
siz[u] += siz[v];
}
}
signed main() {
//freopen(".inp","r",stdin); freopen(".out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
Rep(i,1,n-1) {
int u,v; cin>>u>>v;
adj[u].pb(v); adj[v].pb(u);
}
Rep(i,1,n) cin>>s[i];
int ans=0;
if(n<=2000) {
Rep(i,1,n) {
if(s[i]=='1') {
Rep(i,1,n) siz[i] = 0;
solve(i);
//cout<<"Root at "<<i<<endl;
//Rep(i,1,n) cout<<siz[i]<<" ";
//cout<<endl;
ans=max(ans,siz[i]);
}
else {
Rep(i,1,n) siz[i] = 0;
process(i);
ans=max(ans,siz[i]);
}
}
cout<<ans;
return 0;
}
Rep(i,1,n) {
if(s[i]=='1') {
Rep(i,1,n) siz[i] = 0;
solve(i);
ans=max(ans,siz[i]);
break;
}
}
Rep(i,1,n) {
if(s[i]=='0') {
Rep(i,1,n) siz[i] = 0;
process(i);
ans=max(ans,siz[i]);
break;
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |