This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//LCA
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> adj[100001];
long long ma[100001];
long long p[100001],q[100001];
bool com(pair<int,int> a,pair<int,int> b){
long long lc = a.second*b.second/__gcd(a.second,b.second);
long long a1= a.first*(lc/a.second);
long long b1= b.first*(lc/b.second);
if(a1<=b1)return 1;
else return 0;
}
int n ;
void calc(int i,int pr){
vector<pair<int,int>> best;
best.push_back({ma[i],1});
for(auto j:adj[i]){
if(j!=pr){
calc(j,i);
best.push_back({p[j]*ma[i],q[j]+1});
}
}
sort(best.begin(),best.end(),com);
p[i] = best[0].first;
q[i] = best[0].second;
long long gc = __gcd(p[i],q[i]);
p[i] = p[i]/gc;
q[i] = q[i]/gc;
}
long long pp = 1e9, qq = 1;
void dfs(int i,int pr){
if(com({p[i],q[i]},{pp,qq})){
pp=p[i],qq=q[i];
}
vector<pair<int,int>> best;
for(auto j:adj[i]){
if(j==pr)continue;
dfs(j,i);
best.push_back({p[j],q[j]});
}
sort(best.begin(),best.end(),com);
if(best.size()<=1){
return ;
}
long long ans1p = best[0].first*best[1].first , ans1q = best[0].second+best[1].second;
long long gc = __gcd(ans1p,ans1q);
ans1p/=gc , ans1q/=gc;
ans1p*=ma[i] , ans1q++;
gc = __gcd(ans1p,ans1q);
ans1p/=gc , ans1q/=gc;
if(com({ans1p,ans1q},{pp,qq})){
pp=ans1p,qq=ans1q;
}
}
signed main(){
cin>>n;
for(int i = 0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 0;i<n;i++){
cin>>ma[i+1];
}
calc(1,0);
dfs(1,0);
cout<<pp<<"/"<<qq<<endl;
}
# | 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... |