This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |