#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];
}
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);
int g=gcd(pns.fi,pns.se);
pns.fi/=g;
pns.se/=g;
cout<<pns.fi<<"/"<<pns.se<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
373 ms |
192204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
354 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
395 ms |
262148 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
501 ms |
149556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
442 ms |
161560 KB |
Output is correct |
2 |
Correct |
88 ms |
22764 KB |
Output is correct |
3 |
Runtime error |
346 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
23244 KB |
Output is correct |
2 |
Incorrect |
490 ms |
150728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
468 ms |
145328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
621 ms |
158132 KB |
Output is correct |
2 |
Correct |
440 ms |
117812 KB |
Output is correct |