Submission #622911

#TimeUsernameProblemLanguageResultExecution timeMemory
622911inksamuraiMag (COCI16_mag)C++17
12 / 120
618 ms262144 KiB
#include <bits/stdc++.h> #define int ll using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define vec(...) vector<__VA_ARGS__> #define _3SgiE60 ios::sync_with_stdio(0),cin.tie(0) typedef long long ll; using pii=pair<int,int>; using vi=vector<int>; void print(){cout<<'\n';} template<class h,class...t> void print(const h&v,const t&...u){cout<<v<<' ',print(u...);} // e const int inf=1e13; pii compare(const pii&l,const pii&r){ return l.fi*r.se<r.fi*l.se?l:r; } signed main(){ _3SgiE60; int n; cin>>n; vec(vi) adj(n); rep(i,n-1){ int u,v; cin>>u>>v; u-=1,v-=1; adj[u].pb(v); adj[v].pb(u); } vi a(n); rep(i,n){ cin>>a[i]; } vi dist(n); vec(vi) nadj(n); auto pre_dfs=[&](auto self,int v,int par,int up)->void{ if(up!=-1){ nadj[v].pb(up); nadj[up].pb(v); } up=(a[v]>1?v:up); for(auto &u:adj[v]){ if(u==par) continue; dist[u]=dist[v]+1; self(self,u,v,up); } }; pre_dfs(pre_dfs,0,-1,-1); adj=nadj; pii pns={inf,1}; vec(vec(pii)) rbts(n); auto dfs=[&](auto self,int v,int par)->void{ vi chs; for(auto &u:adj[v]){ if(u==par) continue; chs.pb(u); self(self,u,v); } // if(a[v]>1){ vec(pii) ps; { for(auto &u:chs){ pii p={inf,1}; rep(i,sz(rbts[u])){ int _u=rbts[u][i].se; int _prod=rbts[u][i].fi; int _dist=dist[_u]-dist[v]; if(i==0){ p={_prod,_dist}; }else{ p=compare(pii{_prod,_dist},p); } } if(!sz(ps)){ ps.pb(p); }else{ ps.pb(compare(ps.back(),p)); } } } vec(pii) sp; { per(j,sz(chs)){ int u=chs[j]; pii p={inf,1}; rep(i,sz(rbts[u])){ int _u=rbts[u][i].se; int _prod=rbts[u][i].fi; int _dist=dist[_u]-dist[v]; if(i==0){ p={_prod,_dist}; }else{ p=compare(pii{_prod,_dist},p); } } if(!sz(sp)){ sp.pb(p); }else{ sp.pb(compare(sp.back(),p)); } } reverse(sp.begin(), sp.end()); } rep(j,sz(chs)){ int u=chs[j]; rep(i,sz(rbts[u])){ int _u=rbts[u][i].se; int _prod=rbts[u][i].fi; int _dist=dist[_u]-dist[v]; pii p={inf,1}; if(j){ p=compare(p,ps[j-1]); } if(j+1<sz(chs)){ p=compare(p,sp[j+1]); } if(p.fi<inf and p.fi<inf/a[v] and a[v]*_prod<inf/p.fi){ pii now={p.fi*_prod*a[v],_dist+p.se+1}; pns=compare(pns,now); } } } if(sz(ps)){ pii p=ps.back(); if(p.fi<inf/a[v]){ pns=compare(pns,pii(p.fi*a[v],p.se+1)); } } rep(j,sz(chs)){ int u=chs[j]; for(auto p:rbts[u]){ int _u=p.se; int _prod=p.fi; if(_prod<inf/a[v]){ rbts[v].pb(pii(_prod*a[v],_u)); }else{ // } } } rbts[v].pb(pii(a[v],v)); pns=compare(pns,pii(a[v],1)); }; dfs(dfs,0,-1); int g=gcd(pns.fi,pns.se); pns.fi/=g; pns.se/=g; cout<<pns.fi<<"/"<<pns.se<<"\n"; // print(pns.fi,pns.se); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...