1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<x<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); mt19937 sd(std::random_device{}()); int qpow (int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n; int w[500005]; vector<int>v[500005]; int dp[500005],ans[500005],pos[500005]; vector<int>pref[500005],suf[500005]; vector<int>ord[500005]; vector<vector<int>>a[500005]; bool cmp(int x,int y){return dp[x]>dp[y];} void dfs0(int x,int pre){ dp[x]=0;int sz=0,cnt=1; for(auto i:v[x])if(i!=pre){ dfs0(i,x);++sz; ord[x].pb(i); } sort(all(ord[x]),cmp); REP(i,0,sz)if(i%w[x]==0)dp[x]=max(dp[x],dp[ord[x][i]]+cnt),++cnt; } void dfs1(int x,int pre){ if(pre!=-1)ord[x].pb(pre); int sz=ord[x].size(),l=0;ans[x]=0; sort(all(ord[x]),cmp); REP(i,0,sz)pref[x].pb(i),suf[x].pb(i); REP(i,0,sz)if(i%w[x]==0)pref[x][i]=ans[x],ans[x]=max(ans[x],dp[ord[x][i]]+l+1),++l; else pref[x][i]=ans[x]; REP(i,0,sz)if(ord[x][i]!=pre)pos[ord[x][i]]=i; int cur=0,l2=0,f=0; REP(i,0,sz)if(i%w[x]==1%w[x])++l2; for(int i=sz-1;i>=0;--i){ if(i%w[x]==1%w[x])--l2; suf[x][i]=cur+(f? l2:0); if(i%w[x]==1%w[x])cur=max(cur,dp[ord[x][i]])+1,f=1; } dp[x]=ans[x]; // REP(i,0,n)cout<<dp[i]<<' '; // cout<<endl; // cout<<x+1<<':'<<w[x]<<endl; // REP(i,0,sz)cout<<ord[x][i]+1<<' '; // cout<<endl; // REP(i,0,sz)cout<<pref[x][i]<<' '; // cout<<endl; // REP(i,0,sz)cout<<suf[x][i]<<' '; // cout<<endl; for(auto i:v[x])if(i!=pre){ int px=dp[x],py=dp[i]; dp[x]=max(pref[x][pos[i]],suf[x][pos[i]]); dfs1(i,x); dp[x]=px;dp[i]=py; } } void Main() { cin>>n; REP(i,0,n)cin>>w[i],v[i].clear(); REP(i,1,n){ int x,y; cin>>x>>y; --x,--y; v[x].pb(y);v[y].pb(x); } dfs0(0,-1); // REP(i,0,n)cout<<dp[i]<<' '; // cout<<endl; dfs1(0,-1); REP(i,0,n)cout<<ans[i]<<endl; } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("air.in","r",stdin); freopen("air.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2910
- Time
- 3000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 2
- Accepted
- 2
- Uploaded By