답안 #623070

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
623070 2022-08-05T07:02:01 Z inksamurai Mag (COCI16_mag) C++17
96 / 120
1051 ms 135136 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{
			if(num>inf/a[v]) return {num,len};
			num*=a[v];
			len+=1;
			pii p={num,len};
			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);
      |       ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 861 ms 99520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1051 ms 131796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 604 ms 85816 KB Output is correct
2 Correct 585 ms 63240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 378 ms 88156 KB Output is correct
2 Correct 166 ms 9552 KB Output is correct
3 Correct 970 ms 135136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 9464 KB Output is correct
2 Correct 692 ms 86228 KB Output is correct
3 Correct 667 ms 46428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 499 ms 79992 KB Output is correct
2 Correct 502 ms 84672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 817 ms 86868 KB Output is correct
2 Correct 589 ms 46556 KB Output is correct