#include<bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
int n;
int i, gc;
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
return (long long)a.first * b.second < (long long)a.second * b.first;
}
int p[MAX];
int dpup[MAX], dpdown[MAX];
vector<int> adj[MAX];
pair<int, int> res = {1e9, 1};
void dfsdown(int node, int par) {
vector<int> L, R;
L.push_back(0);
for (auto &it : adj[node]) {
if (it != par) {
dfsdown(it, node);
L.push_back(max(L.back(), p[it] == 1 ? dpdown[it] + 1 : 0));
}
}
R.push_back(0);
for (i = (int)adj[node].size() - 1; i >= 0; i--) {
auto &it = adj[node][i];
if (it != par) {
dpup[it] = (p[node] == 1 ? (1 + max(L[L.size() - R.size() - 1], R.back())): 0);
R.push_back(max(R.back(), p[it] == 1 ? dpdown[it] + 1 : 0));
}
}
dpdown[node] = L.back();
L.clear(); R.clear();
}
pair<int, int> tmp, a;
int mx, val;
void dfsup(int node, int par) {
if (node) {
dpup[node] = (p[par] == 1 ? max(dpup[node], 1 + dpup[par]) : 0);
}
a = {p[node], 1};
if (cmp(a, res)) {
res = a;
}
mx = dpup[node];
for (auto &it : adj[node]) {
if (it == par) {
continue;
}
val = (p[it] == 1 ? dpdown[it] + 1 : 0);
tmp = {p[node], 1 + mx + val};
if (cmp(tmp, res)) {
res = tmp;
}
mx = max(mx, val);
dfsup(it, node);
}
}
int main() {
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> n;
int a, b;
for (i = 0; i < n - 1; i++) {
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (i = 0; i < n; i++) {
cin >> p[i];
}
dfsdown(0, -1);
dfsup(0, -1);
gc = __gcd(res.first, res.second);
cout << res.first / gc << "/" << res.second / gc << endl;
return 0;
}
Compilation message
mag.cpp: In function 'int main()':
mag.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23884 KB |
Output is correct |
2 |
Incorrect |
13 ms |
23872 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
512 ms |
140408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Runtime error |
548 ms |
262148 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
634 ms |
262148 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
569 ms |
67692 KB |
Output is correct |
2 |
Incorrect |
405 ms |
56512 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
524 ms |
72136 KB |
Output is correct |
2 |
Incorrect |
100 ms |
28996 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
144 ms |
28952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
501 ms |
68156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
579 ms |
69120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |