# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
623067 |
2022-08-05T07:00:19 Z |
inksamurai |
Mag (COCI16_mag) |
C++17 |
|
1012 ms |
131792 KB |
#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];
}
pii pns{inf,1};
vi ct_par(n,-1);
{
int num=0;
vi usd(n,0),chs(n,0);
auto refill=[&](auto self,int v,int par)->void{
num+=1;
chs[v]=1;
for(auto u:adj[v]){
if(u==par or usd[u]) continue;
self(self,u,v);
chs[v]+=chs[u];
}
};
auto find_centroid=[&](auto self,int v,int par)->int{
for(auto &u:adj[v]){
if(u==par or usd[u]) continue;
if(chs[u]*2>num){
return self(self,u,v);
}
}
return v;
};
auto rfs=[&](auto self,int v,int par,int num,int len)->pii{
pii p={num,len};
if(num>inf/a[v]) return p;
num*=a[v];
len+=1;
for(auto u:adj[v]){
if(u==par or usd[u]) continue;
p=compare(p,self(self,u,v,num,len));
}
return p;
};
auto dfs=[&](auto self,int v,int par)->int{
num=0;
refill(refill,v,par);
int rt=find_centroid(find_centroid,v,par);
usd[rt]=1;
pii q={inf,1};
for(auto &u:adj[rt]){
if(u==par or usd[u]) continue;
pii p=rfs(rfs,u,rt,1,0);
if(q.fi<=inf and q.fi<=(inf/p.fi+a[rt]-1)/a[rt]){
pii now={p.fi*q.fi*a[rt],p.se+q.se+1};
pns=compare(pns,now);
}
q=compare(q,p);
}
pns=compare(pns,pii(a[rt],1));
if(q.fi<=inf/a[v]){
pns=compare(pns,pii(q.fi*a[rt],q.se+1));
}
for(auto u:adj[rt]){
if(u==par or usd[u]) continue;
self(self,u,rt);
}
return rt;
};
int rt=dfs(dfs,0,-1);
}
// print(pns.fi,pns.se);
// int g=gcd(pns.fi,pns.se);
// pns.fi/=g;
// pns.se/=g;
cout<<pns.fi<<"/"<<pns.se<<"\n";
}
Compilation message
mag.cpp: In function 'int main()':
mag.cpp:101:7: warning: unused variable 'rt' [-Wunused-variable]
101 | int rt=dfs(dfs,0,-1);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
752 ms |
99712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1012 ms |
131792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
586 ms |
85768 KB |
Output is correct |
2 |
Correct |
581 ms |
63232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
378 ms |
88140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
120 ms |
9544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
549 ms |
79888 KB |
Output is correct |
2 |
Correct |
478 ms |
84796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
899 ms |
86700 KB |
Output is correct |
2 |
Incorrect |
621 ms |
46544 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |