#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=1e14;
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{
num*=a[v];
len+=1;
// if(v==7 and par==6) print("go",num,len);
pii p={num,len};
if(num>=inf) return p;
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(rt==1) print(u,p.fi,p.se);
if(q.fi<=inf and q.fi<=inf/p.fi/a[rt]+1){
pii now={p.fi*q.fi*a[rt],p.se+q.se+1};
pns=compare(pns,now);
}
// if(rt==6) print(p.fi,p.se,u);
q=compare(q,p);
}
// print(rt);
// if(rt==6) print(q.fi,q.se);
// print(rt,q.fi,q.se);
pns=compare(pns,pii(a[rt],1));
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);
}
// vec(vec(pii)) rbts(n);
// vi ctr(n);
// // center of rabbits
// rep(v,n){
// ctr[v]=v;
// }
// vi dist(n);
// vec(pii) fdp(n);
// auto pre_dfs=[&](auto self,int v,int par)->void{
// int now_ctr=v;
// // print(v,dist[v]);
// for(auto &u:adj[v]){
// if(u==par) continue;
// dist[u]=dist[v]+1;
// self(self,u,v);
// if(sz(rbts[ctr[u]])>sz(rbts[now_ctr])){
// now_ctr=ctr[u];
// }
// }
// for(auto &u:adj[v]){
// if(u==par or ctr[u]==now_ctr) continue;
// for(auto &p:rbts[ctr[u]]){
// rbts[now_ctr].pb(p);
// }
// }
// ctr[v]=now_ctr;
// if(a[v]>1){
// sort(rbts[now_ctr].begin(), rbts[now_ctr].end());
// while(sz(rbts[now_ctr]) and rbts[now_ctr].back().fi>inf/a[v]){
// rbts[now_ctr].pop_back();
// }
// fdp[v]=pii(a[v],1);
// for(auto &p:rbts[now_ctr]){
// p.fi*=a[v];
// fdp[v]=compare(fdp[v],pii(p.fi,dist[p.se]-dist[v]+1));
// }
// }
// else{
// fdp[v]=pii(1,1);
// for(auto &u:adj[v]){
// if(u==par) continue;
// fdp[v]=compare(fdp[v],pii(fdp[u].fi,fdp[u].se+1));
// }
// }
// rbts[now_ctr].pb(pii(a[v],v));
// };
// pre_dfs(pre_dfs,0,-1);
// pii pns={inf,1};
// auto dfs=[&](auto self,int v,int par)->void{
// pii q={inf,1};
// // print(v);
// for(auto &u:adj[v]){
// if(u==par) continue;
// self(self,u,v);
// pii p=fdp[u];
// if(q.fi<=inf and q.fi<=inf/a[v]/p.fi+1){
// pii now={q.fi*p.fi*a[v],q.se+p.se+1};
// pns=compare(pns,now);
// }
// {
// pii now={p.fi*a[v],p.se+1};
// pns=compare(pns,now);
// }
// q=compare(q,p);
// }
// pns=compare(pns,fdp[v]);
// };
// 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
mag.cpp: In function 'int main()':
mag.cpp:105:7: warning: unused variable 'rt' [-Wunused-variable]
105 | int rt=dfs(dfs,0,-1);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
726 ms |
96044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
950 ms |
131804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
545 ms |
85804 KB |
Output is correct |
2 |
Correct |
526 ms |
63240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
88072 KB |
Output is correct |
2 |
Correct |
116 ms |
9484 KB |
Output is correct |
3 |
Correct |
862 ms |
135244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
9444 KB |
Output is correct |
2 |
Correct |
665 ms |
86176 KB |
Output is correct |
3 |
Correct |
748 ms |
46432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
457 ms |
79716 KB |
Output is correct |
2 |
Correct |
479 ms |
84684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
949 ms |
86808 KB |
Output is correct |
2 |
Correct |
699 ms |
46412 KB |
Output is correct |