Submission #1096406

# Submission time Handle Problem Language Result Execution time Memory
1096406 2024-10-04T12:03:57 Z JuanJL Mag (COCI16_mag) C++17
0 / 120
611 ms 262144 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define pb push_back
#define forn(i,a,b) for(int i = a; i < b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
 
typedef long long ll;
 
const int MAXN = 1000000+5;
 
struct fract{
	__int128 num; 
	__int128 den;
	fract(__int128 num = 1, __int128 den = 1) : num(num) , den(den) {}
};
 
bool operator<(const fract &a, const fract &b) {
	__int128 mcm = (a.den*b.den)/__gcd(a.den,b.den);	
	return (a.num*(mcm/a.den)) < (b.num*(mcm/b.den));
}

fract reduc(fract a){
	ll gcd;
	do{
		gcd=__gcd(a.num,a.den);
		a.num/=gcd;
		a.den/=gcd;
	}while(gcd!=1);
	return a;
}
 
ll n;
vector<ll> adj[MAXN];
ll parent[MAXN];
ll magic[MAXN];
fract down[MAXN];
fract def[MAXN];
 
void downcalc(ll nd){
	down[nd].num=magic[nd];
	down[nd].den=1;
	for(auto i:adj[nd]){
		downcalc(i);
		down[nd] = min(down[nd],fract(down[nd].num*down[i].num,down[nd].den+down[i].den));
	}
}
 
void calc(ll nd){
	def[nd] = down[nd];
	if(parent[nd] != -1){
		def[nd] = min(def[nd],fract(magic[parent[nd]]*def[nd].num,def[nd].den+1));
		for(auto i:adj[parent[nd]]){
			if(i==nd) continue;
			def[nd] = min( def[nd] , fract(def[nd].num*magic[parent[nd]]*down[i].num, def[nd].den+down[i].den+1));
		}
	}
	for(auto i:adj[nd]) calc(i);
}
 
int main(){
	mset(parent,-1);
	cin>>n;
	ll a,b;
	forn(i,0,n-1){
		cin>>a>>b; a--; b--;
		adj[a].pb(b);
		parent[b]=a;
	}
	forn(i,0,n) cin>>magic[i];
	
	ll root = 0;
	forn(i,0,n) if(parent[i] == -1) root = i;
	
	downcalc(root);
	
	/*fract res=down[root];
	forn(i,0,n){ res=min(res,down[i]);}
	cout<<to_string((ll)res.num)<<"/"<<to_string((ll)res.den)<<'\n';*/
	
	calc(root);
	
	fract res=def[root];
	forn(i,0,n){ res=min(res,def[i]);}
	cout<<to_string((ll)res.num)<<"/"<<to_string((ll)res.den)<<'\n';
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 94296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 94292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 611 ms 239596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 41 ms 94292 KB Output is correct
2 Runtime error 582 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 583 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 522 ms 148524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 601 ms 150356 KB Output is correct
2 Incorrect 113 ms 98464 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 121 ms 98132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 524 ms 149328 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 566 ms 148816 KB Output isn't correct
2 Halted 0 ms 0 KB -