#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[v][0] == dp[v][1]) f[v] = max(f[v], dp[v][0] + 1);
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] + 1);
}
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 |
10 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
26972 KB |
Output is correct |
2 |
Correct |
8 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
94872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
26968 KB |
Output is correct |
2 |
Correct |
361 ms |
148140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
146364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
275 ms |
70484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
262 ms |
71668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
31368 KB |
Output is correct |
2 |
Incorrect |
273 ms |
71108 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
68848 KB |
Output is correct |
2 |
Incorrect |
275 ms |
70188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
71372 KB |
Output is correct |
2 |
Correct |
225 ms |
51100 KB |
Output is correct |