Submission #698644

# Submission time Handle Problem Language Result Execution time Memory
698644 2023-02-14T06:42:11 Z radal Mag (COCI16_mag) C++17
120 / 120
388 ms 162368 KB
#include <bits/stdc++.h>
//#pragma GCC target("sse,sse2,avx2")
//#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;

using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cp;
typedef pair<int,int> pll;
constexpr int N = 1e6+10,mod = 998244353;
constexpr ll inf = 1e9+10;
const ld pi = acos(-1),eps = 4e-1;
inline int mkay(int a,int b){
    if (a+b >= mod) return a+b-mod;
    if (a+b < 0) return a+b+mod;
    return a+b;
}
 
inline int poww(int a,int k){
    if (k < 0) return 0;
    int z = 1;
    while (k){
        if (k&1) z = 1ll*z*a%mod;
        a = 1ll*a*a%mod;
        k >>= 1;
    } 
    return z; 
}

vector<int> adj[N];
int mx[N],a[N],dpu[N];
pll ans;

void dfs(int v,int p){
	int m1 = 0,m2 = 0;
	for (int u : adj[v]){
		if (u != p){
			dfs(u,v);
			if (mx[u] >= m1){
				m2 = m1;
				m1 = mx[u];
			}
			else if (mx[u] > m2)
				m2 = mx[u];
		}
	}
	if (1ll*ans.Y*a[v] < 1ll*ans.X*(1+m1+m2)){
		ans.X = a[v];
		ans.Y = 1+m1+m2;
        int g = gcd(ans.X,ans.Y);
        ans.X /= g;
        ans.Y /= g;
	}
	if (a[v] == 1) mx[v] = m1+1;
}

void dfs2(int v,int p){
    int m1 = 0,m2 = 0;
    for (int u : adj[v]){
        if (u != p){
            if (mx[u] >= m1){
                m2 = m1;
                m1 = mx[u];
            }
            else if (mx[u] > m2)
                m2 = mx[u];
        }
    }
	for (int u : adj[v]){
		if (u != p && a[v] == 1){
			dpu[u] = dpu[v]+1;
			if (mx[u] == m1) dpu[u] = max(dpu[u],m2+1);
			else dpu[u] = max(dpu[u],m1+1);
		}
		if (u != p) dfs2(u,v);
	}
	if (1ll*a[v]*ans.Y < 1ll*ans.X*(1+m1+dpu[v])){
		ans.X = a[v];
		ans.Y = 1+m1+dpu[v];
		int g = gcd(ans.X,ans.Y);
		ans.X /= g;
		ans.Y /= g;
	}

}

int main(){
	ios :: sync_with_stdio(0); cin.tie(0);  cout.tie(0);
	int n;
	cin >> n;
	rep(i,1,n){
		int u,v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	rep(i,1,n+1) cin >> a[i];
	ans = {a[1],1};
	dfs(1,0);
	dfs2(1,0);
	cout << ans.X << '/' << ans.Y;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23820 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23816 KB Output is correct
2 Correct 12 ms 23852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 289 ms 103496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 370 ms 159376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 388 ms 158972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 336 ms 81788 KB Output is correct
2 Correct 229 ms 66636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 84888 KB Output is correct
2 Correct 65 ms 29704 KB Output is correct
3 Correct 388 ms 162368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 29588 KB Output is correct
2 Correct 333 ms 82496 KB Output is correct
3 Correct 260 ms 53412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 79872 KB Output is correct
2 Correct 318 ms 83108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 82672 KB Output is correct
2 Correct 335 ms 53552 KB Output is correct