This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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=1e12;
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];
}
pii pns{inf,1};
vi ct_par(n,-1);
{
int num=0;
vi usd(n,0),chs(n,0);
auto refill=[&](auto self,int v,int par)->void{
num+=1;
chs[v]=1;
for(auto u:adj[v]){
if(u==par or usd[u]) continue;
self(self,u,v);
chs[v]+=chs[u];
}
};
auto find_centroid=[&](auto self,int v,int par)->int{
for(auto &u:adj[v]){
if(u==par or usd[u]) continue;
if(chs[u]*2>num){
return self(self,u,v);
}
}
return v;
};
auto rfs=[&](auto self,int v,int par,int num,int len)->pii{
if(num>inf/a[v]) return {num,len};
num*=a[v];
len+=1;
pii p={num,len};
for(auto u:adj[v]){
if(u==par or usd[u]) continue;
p=compare(p,self(self,u,v,num,len));
}
return p;
};
auto dfs=[&](auto self,int v,int par)->int{
num=0;
refill(refill,v,par);
int rt=find_centroid(find_centroid,v,par);
usd[rt]=1;
pii q={inf,1};
for(auto &u:adj[rt]){
if(u==par or usd[u]) continue;
pii p=rfs(rfs,u,rt,1,0);
if(q.fi<=inf and q.fi<=(inf/p.fi+a[rt]-1)/a[rt]){
pii now={p.fi*q.fi*a[rt],p.se+q.se+1};
pns=compare(pns,now);
}
q=compare(q,p);
}
pns=compare(pns,pii(a[rt],1));
if(q.fi<=inf/a[v]){
pns=compare(pns,pii(q.fi*a[rt],q.se+1));
}
for(auto u:adj[rt]){
if(u==par or usd[u]) continue;
self(self,u,rt);
}
return rt;
};
int rt=dfs(dfs,0,-1);
}
// print(pns.fi,pns.se);
int g=gcd(pns.fi,pns.se);
pns.fi/=g;
pns.se/=g;
cout<<pns.fi<<"/"<<pns.se<<"\n";
}
Compilation message (stderr)
mag.cpp: In function 'int main()':
mag.cpp:101:7: warning: unused variable 'rt' [-Wunused-variable]
101 | int rt=dfs(dfs,0,-1);
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |