Submission #720561

# Submission time Handle Problem Language Result Execution time Memory
720561 2023-04-08T13:22:18 Z Ahmed57 Mag (COCI16_mag) C++14
0 / 120
4000 ms 262144 KB
//LCA
#include <bits/stdc++.h>

using namespace std;
#define int long long
vector<int> adj[1000001];
long long ma[1000001];
__int128 p[1000001],q[1000001];
//gcd
__int128 gcd(__int128 a,__int128 b){
    if(a==0)return b;
    return gcd(b%a,a);
}
bool com(pair<__int128,__int128> a,pair<__int128,__int128> b){
    __int128 lc = a.second*b.second/gcd(a.second,b.second);
    __int128 a1= a.first*(lc/a.second);
    __int128 b1= b.first*(lc/b.second);
    if(a1<=b1)return 1;
    else return 0;
}
int n ;
void calc(int i,int pr){
    vector<pair<__int128,__int128>> best;
    best.push_back({ma[i],1});
    for(auto j:adj[i]){
        if(j!=pr){
            calc(j,i);
            __int128 mm = ma[i];
            best.push_back({p[j]*mm,q[j]+1});
        }
    }
    sort(best.begin(),best.end(),com);
    p[i] = best[0].first;
    q[i] = best[0].second;
    __int128 gc = gcd(p[i],q[i]);
    p[i] = p[i]/gc;
    q[i] = q[i]/gc;
}
__int128 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<__int128,__int128>> 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 ;
    }
    __int128 mm = ma[i];
    __int128 ans1p = best[0].first*best[1].first , ans1q = best[0].second+best[1].second;
    __int128 gc = gcd(ans1p,ans1q);
    ans1p/=gc , ans1q/=gc;
    ans1p*=mm , 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);
    long long ppp = pp , qqq = qq;
    cout<<ppp<<"/"<<qqq<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1016 ms 200372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 980 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1114 ms 109404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1058 ms 233204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 184 ms 31448 KB Output is correct
2 Incorrect 1148 ms 111332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4067 ms 110924 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1065 ms 110212 KB Output isn't correct
2 Halted 0 ms 0 KB -