Submission #720561

#TimeUsernameProblemLanguageResultExecution timeMemory
720561Ahmed57Mag (COCI16_mag)C++14
0 / 120
4067 ms262144 KiB
//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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...