#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 |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
23 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
23928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
449 ms |
99616 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
568 ms |
148532 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
518 ms |
82092 KB |
Output is correct |
2 |
Correct |
386 ms |
69468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
91384 KB |
Output is correct |
2 |
Correct |
94 ms |
30712 KB |
Output is correct |
3 |
Incorrect |
606 ms |
159764 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
30592 KB |
Output is correct |
2 |
Correct |
514 ms |
84420 KB |
Output is correct |
3 |
Correct |
422 ms |
58056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
468 ms |
80872 KB |
Output is correct |
2 |
Correct |
513 ms |
83608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
532 ms |
83980 KB |
Output is correct |
2 |
Correct |
432 ms |
57480 KB |
Output is correct |