#include <bits/stdc++.h>
#define gsn "Mag"
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 1000005
#define gb(i, j) ((i >> j) & 1)
#define pii pair<int, int>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r) (rng);
}
int n;
vector<int> o[maxn];
int a[maxn], dp[maxn][2], f[maxn];
void dfs(int u, int par)
{
for(int v : o[u])
{
if(v == par) continue;
dfs(v, u);
if(a[v] == 1)
{
if(dp[v][0] + a[v] > dp[u][0])
{
dp[u][1] = dp[u][0];
dp[u][0] = dp[v][0] + a[v];
}
else if(dp[v][0] + a[v] > dp[u][1])
{
dp[u][1] = dp[v][0] + a[v];
}
}
}
}
void dfs2(int u, int par)
{
for(int v : o[u])
{
if(v == par) continue;
if(a[u] == 1)
{
if(f[u] + a[u] > f[v]) f[v] = f[u] + a[u];
if(dp[u][0] == dp[u][1]) f[v] = max(f[v], dp[u][0] + a[u]);
else if(dp[v][0] + 1 == dp[u][0]) f[v] = max(f[v], dp[u][1] + a[u]);
else f[v] = max(f[v], dp[u][0] + a[u]);
}
dfs2(v, u);
}
}
int main()
{
// freopen(gsn".inp","r",stdin);
// freopen(gsn".out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
fi(i, 1, n - 1)
{
int x, y;
cin >> x >> y;
o[x].push_back(y);
o[y].push_back(x);
}
fi(i, 1, n) cin >> a[i];
dfs(1, 0);
dfs2(1, 0);
ll P = -1, Q = -1;
fi(i, 1, n)
{
ll tu = a[i];
ll mau = max({ dp[i][0] + dp[i][1], dp[i][0] + f[i], dp[i][1] + f[i] }) + 1;
ll Gcd = __gcd(tu, mau);
tu /= Gcd;
mau /= Gcd;
if(P == -1) P = tu, Q = mau;
else if(P * mau > Q * tu) P = tu, Q = mau;
}
cout << P << "/" << Q ;
/// ------------------check time!-----------------///
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
26972 KB |
Output is correct |
2 |
Correct |
7 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
26972 KB |
Output is correct |
2 |
Correct |
7 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
94804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
26972 KB |
Output is correct |
2 |
Correct |
381 ms |
148132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
146356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
70484 KB |
Output is correct |
2 |
Correct |
207 ms |
60548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
71688 KB |
Output is correct |
2 |
Correct |
45 ms |
31460 KB |
Output is correct |
3 |
Correct |
377 ms |
149448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
31312 KB |
Output is correct |
2 |
Correct |
283 ms |
71252 KB |
Output is correct |
3 |
Correct |
215 ms |
51024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
69068 KB |
Output is correct |
2 |
Correct |
285 ms |
70232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
71180 KB |
Output is correct |
2 |
Correct |
215 ms |
51100 KB |
Output is correct |