Submission #623065

# Submission time Handle Problem Language Result Execution time Memory
623065 2022-08-05T06:51:21 Z inksamurai Mag (COCI16_mag) C++17
84 / 120
936 ms 141636 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{
			num*=a[v];
			len+=1;
			pii p={num,len};
			if(num>=inf) return p;
			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*a[rt]<=inf/p.fi){
					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));
			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);
	}
	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:99:7: warning: unused variable 'rt' [-Wunused-variable]
   99 |   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 284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 742 ms 104924 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 936 ms 131792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 581 ms 96196 KB Output is correct
2 Correct 552 ms 63232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 365 ms 88164 KB Output is correct
2 Correct 142 ms 9544 KB Output is correct
3 Correct 840 ms 141636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 9548 KB Output is correct
2 Correct 661 ms 96472 KB Output is correct
3 Correct 700 ms 46432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 478 ms 90384 KB Output is correct
2 Correct 478 ms 84684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 763 ms 97332 KB Output is correct
2 Correct 620 ms 46452 KB Output is correct