Submission #256477

# Submission time Handle Problem Language Result Execution time Memory
256477 2020-08-02T18:07:21 Z amiratou Mag (COCI16_mag) C++14
120 / 120
688 ms 114344 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define rando mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ld long double
#define ll long long
#define int ll
#define ii pair<int,int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF (int)(1e18)
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
const int MX = 1000006;
vii vec[MX];
int val[MX],two,one;
int solve(int node,int p){
	v g;
	int h=0;
	for(auto &it:vec[node]){
		if(it.fi!=p && (val[it.fi]==1)){
			if(it.se==-1)it.se=solve(it.fi,node);
			g.pb(it.se);
			h++;
		}
	}
	sort(all(g),greater<int>());
	if((val[node]==2) && (node==p))
		for (int i = 0; i+1 < sz(g); ++i)
			if(g[i]==g[i+1])two=max(two,g[i]+g[i+1]+1);
	if((node==p) && (val[node]==1) && (sz(g)>=1))one=max(one,g[0]+1);
	return (sz(g)?g[0]:0)+(val[node]==1);
}
int32_t main(){
	boost;
	//freopen(".in","r",stdin);
	ii ans={INF,1};
	int n,a,b;
	cin>>n;
	for (int i = 0; i < n-1; ++i)
	{
		cin>>a>>b;
		a--,b--;
		vec[a].pb({b,-1});
		vec[b].pb({a,-1});
	}
	for (int i = 0; i < n; ++i){
		cin>>val[i];
		if(val[i]<ans.fi)ans.fi=val[i];
	}
	for (int i = 0; i < n; ++i)
		solve(i,i);
	if(one || two){
		if(!two)ans={1,one};
		else{
			if((2*one)>=two)ans={1,one};
			else ans={2,two};
		}
	}
	int g=__gcd(ans.fi,ans.se);
	cout<<ans.fi/g<<"/"<<ans.se/g<<"\n";
	return 0;
}

//do smth instead of nothing and stay organized
//long long
//array bounds
//special cases
//binary search
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23936 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 17 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 550 ms 114344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 498 ms 80760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 608 ms 79780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 601 ms 81244 KB Output is correct
2 Correct 458 ms 66484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 443 ms 85456 KB Output is correct
2 Correct 127 ms 31352 KB Output is correct
3 Correct 609 ms 80376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 31096 KB Output is correct
2 Correct 621 ms 81524 KB Output is correct
3 Correct 688 ms 57592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 510 ms 78032 KB Output is correct
2 Correct 613 ms 78240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 623 ms 82296 KB Output is correct
2 Correct 638 ms 61068 KB Output is correct