Submission #622900

# Submission time Handle Problem Language Result Execution time Memory
622900 2022-08-04T18:37:05 Z inksamurai Mag (COCI16_mag) C++17
12 / 120
801 ms 262144 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=1e13;

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};
	vec(vec(pii)) rbts(n);
	vi dist(n);
	auto dfs=[&](auto self,int v,int par,int num)->void{
		dist[v]=num;
		vi chs;
		for(auto &u:adj[v]){
			if(u==par) continue;
			chs.pb(u);
			self(self,u,v,num+1);
		}
		// if(a[v]>1){
		vec(pii) ps;
		{
			for(auto &u:chs){
				pii p={inf,1};
				rep(i,sz(rbts[u])){
					int _u=rbts[u][i].se;
					int _prod=rbts[u][i].fi;
					int _dist=dist[_u]-num;
					if(i==0){
						p={_prod,_dist};
					}else{
						p=compare(pii{_prod,_dist},p);
					}
				}
				if(!sz(ps)){
					ps.pb(p);
				}else{
					ps.pb(compare(ps.back(),p));
				}
			}
		}
		vec(pii) sp;
		{
			per(j,sz(chs)){
				int u=chs[j];
				pii p={-1,-1};
				rep(i,sz(rbts[u])){
					int _u=rbts[u][i].se;
					int _prod=rbts[u][i].fi;
					int _dist=dist[_u]-num;
					if(i==0){
						p={_prod,_dist};
					}else{
						p=compare(pii{_prod,_dist},p);
					}
				}
				if(!sz(sp)){
					sp.pb(p);
				}else{
					sp.pb(compare(sp.back(),p));
				}
			}
			reverse(sp.begin(), sp.end());
		}
		rep(j,sz(chs)){
			int u=chs[j];
			rep(i,sz(rbts[u])){
				int _u=rbts[u][i].se;
				int _prod=rbts[u][i].fi;
				int _dist=dist[_u]-num;
				pii p={inf,1};
				if(j){
					p=compare(p,ps[j-1]);
				}
				if(j+1<sz(chs)){
					p=compare(p,sp[j+1]);
				}
				if(p.fi<(inf/_prod)/a[v]+11){
					pii now={p.fi*_prod*a[v],_dist+p.se};
					pns=compare(pns,now);
				}
			}
		}
		if(sz(ps)){
			pii p=ps.back();
			if(p.fi<inf/a[v]){
				pns=compare(pns,pii(p.fi*a[v],p.se+1));
			}
		}
		rep(j,sz(chs)){
			int u=chs[j];
			for(auto p:rbts[u]){
				int _u=p.se;
				int _prod=p.fi;
				if(_prod<inf/a[v]){
					rbts[v].pb(pii(_prod*a[v],_u));
				}else{
					// 
				}
			}
		}
		// }else{
		// 	rep(j,sz(chs)){
		// 		int u=chs[j];
		// 		for(auto p:rbts[u]){
		// 			int _u=p.se;
		// 			int _prod=p.fi;
		// 			int _dist=dist[_u]-num;
		// 			// print(a[v],_u,_prod);
		// 			rbts[v].pb(pii(_prod,_u));
		// 			pns=compare(pns,pii(_prod,_dist+1));
		// 		}
		// 	}
		// }
		rbts[v].pb(pii(a[v],v));
		pns=compare(pns,pii(a[v],1));
	};
	dfs(dfs,0,-1,0);
	cout<<pns.fi<<"/"<<pns.se<<"\n";
	// print(pns.fi,pns.se);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1236 KB Output is correct
2 Incorrect 4 ms 1484 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 376 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Runtime error 379 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 366 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 742 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 612 ms 187652 KB Output is correct
2 Incorrect 197 ms 48792 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 220 ms 55500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 801 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 732 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -