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>
#define int long long
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 1e6 + 5;
int n, a[N], dp[N];
vector<int> g[N];
struct Frac {
int num, deto;
Frac(int num = -1, int deto = -1): num(num), deto(deto) {};
bool operator<(const Frac &rhs) const {
return num * rhs.deto < rhs.num * deto;
}
};
Frac ans = Frac(inf, 1);
void dfs(int u, int p) {
if (Frac(a[u], 1) < ans) ans = Frac(a[u], 1);
//Only take this vertex.
vector<int> lol;
fa (v, g[u]) if (v != p) {
dfs(v, u);
lol.pb(dp[v]);
}
sort(lol.begin(), lol.end());
if (a[u] == 1) {
if (lol.size() == 0) dp[u] = 1;
else {
dp[u] = lol.back() + 1;
}
} else dp[u] = 0;
int len = 1;
if (lol.size() == 1) {
len = lol.back() + 1;
} else if (lol.size() > 1) {
len = lol[lol.size() - 1] + lol[lol.size() - 2] + 1;
}
// cout << "get " << a[u] << " " << len << "\n";
if (Frac(a[u], len) < ans) ans = Frac(a[u], len);
}
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
fw (i, 1, n) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].pb(v), g[v].pb(u);
}
fw (i, 0, n) cin >> a[i];
dfs(0, -1);
cout << ans.num << "/" << ans.deto;
return 0;
}
# | 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... |