Submission #288858

# Submission time Handle Problem Language Result Execution time Memory
288858 2020-09-02T02:35:24 Z Marlov Mag (COCI16_mag) C++14
0 / 120
747 ms 262144 KB
/*
Code by @marlov       
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <utility>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;

#define maxN 1000005
#define INF (ll)1000000001
ll N;
vector<ll> adj[maxN];
ll magic[maxN];

pair<ll,ll> dp[maxN];
pair<ll,ll> dp2[maxN];
ll top=INF;
ll bottom=1;

ll GCD(ll a,ll b){
	if(a==0) return b;
	GCD(b%a,a);
}

void ps(ll t1,int b1){
	if(top*b1>t1*bottom){
		top=t1;
		bottom=b1;
	}
}

void dfs(int n,int p){
	for(int i:adj[n]) if(i!=p){
		dfs(i,n);
	}
	//go through all children and try to connect\
	//if magic greater than 2 return
	if(magic[n]>2) return;
	for(int i:adj[n]) if(i!=p){
		//possible route through that node
		ps(dp[n].first*dp[i].first,dp[n].second+dp[i].second);
		if(magic[n]==1){
		if(dp[n].first*dp[i].second>dp[i].first*dp[n].second){
			dp[n].first=dp[i].first;
			dp[n].second=dp[i].second+1;
		}
		if(dp2[n].first*dp2[i].second>=dp2[i].first*dp2[n].second){
			dp2[n].first=2;
			dp2[n].second=dp2[i].second+1;
		}
		}else if(magic[n]==2){
			if(dp[i].second>=dp2[n].second){
				dp2[n].first=2;
				dp2[i].second=dp[i].second+1;
			}
		}
	}
	ps(dp2[n].first,dp2[n].second);
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>N;
	ll u,v;
	for(int i=0;i<N-1;i++){
		cin>>u>>v;
		u--;
		v--;
		adj[u].push_back(v);
		adj[v].push_back(u);
	} 
	for(int i=0;i<N;i++){
		cin>>magic[i];
		top=min(magic[i],top);
		dp[i].first=magic[i];
		dp[i].second=1;
		dp2[i].first=INF;
		dp2[i].second=1;
	}
	dfs(0,-1);
	/*
	for(int i=0;i<N;i++){
		cout<<i+1<<" has "<<dp[i].first<<" and "<<dp[i].second<<'\n';
	}
	*/
	cout<<top/GCD(top,bottom)<<"/"<<bottom/GCD(top,bottom)<<'\n';
    return 0;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1,n=0?)
	* do smth instead of nothing and stay organized
*/

Compilation message

mag.cpp:52:2: warning: multi-line comment [-Wcomment]
   52 |  //go through all children and try to connect\
      |  ^
mag.cpp: In function 'll GCD(ll, ll)':
mag.cpp:38:5: warning: control reaches end of non-void function [-Wreturn-type]
   38 |  GCD(b%a,a);
      |  ~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 49 ms 48248 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 49 ms 48376 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 513 ms 238344 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 48 ms 48248 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 747 ms 262144 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 512 ms 190000 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 516 ms 194780 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 123 ms 63608 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 487 ms 181356 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 531 ms 192252 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -