Submission #720555

# Submission time Handle Problem Language Result Execution time Memory
720555 2023-04-08T13:16:06 Z Ahmed57 Mag (COCI16_mag) C++14
0 / 120
143 ms 12900 KB
//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 = 1e18, 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
1 Correct 3 ms 2644 KB Output is correct
2 Incorrect 2 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 5204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 72 ms 12900 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 5164 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 143 ms 10060 KB Output is correct
2 Runtime error 4 ms 5204 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 66 ms 12828 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -