#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, a[N], dp_down[N], dp_up[N];
int ans1 = -1, ans2 = -1;
vector < int > g[N];
inline void upd(int &x, int y){
if(x < y){
x = y;
}
}
void dfs1(int v, int pr = -1){
for(int to : g[v]){
if(to != pr){
dfs1(to, v);
upd(dp_down[v], dp_down[to]);
}
}
if(a[v] != 1){
dp_down[v] = 0;
}
else{
dp_down[v] += 1;
}
}
void dfs2(int v, int pr = -1){
if(pr != -1){
g[v].erase(find(g[v].begin(), g[v].end(), pr));
}
int sz = (int)g[v].size();
vector < int > pref(sz + 2), suf(sz + 2);
for(int i = 1; i <= sz; i++){
pref[i] = max(pref[i - 1], dp_down[g[v][i - 1]]);
}
for(int i = sz; i >= 1; i--){
suf[i] = max(suf[i + 1], dp_down[g[v][i - 1]]);
}
for(int i = 1; i <= sz; i++){
int to = g[v][i - 1];
if(a[v] == 1){
dp_up[to] = max({pref[i - 1], suf[i + 1], dp_up[v]}) + 1;
}
else{
dp_up[to] = 0;
}
dfs2(to, v);
}
}
inline void check(int x, int y){
/// x / y < ans1 / ans2;
/// x * ans2 < ans1 * y;
if(ans1 == -1){
ans1 = x;
ans2 = y;
}
else if(1LL * x * ans2 < 1LL * ans1 * y){
ans1 = x;
ans2 = y;
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
}
dfs1(1);
dfs2(1);
for(int i = 1; i <= n; i++){
int x = a[i], y = (a[i] != 1) + dp_up[i] + dp_down[i];
check(x, y);
}
int g = __gcd(ans1, ans2);
cout << ans1 / g << "/" << ans2 / g;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
616 ms |
120944 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
23808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
809 ms |
218744 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
624 ms |
66776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
609 ms |
70444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
28280 KB |
Output is correct |
2 |
Incorrect |
675 ms |
71392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
531 ms |
66456 KB |
Output is correct |
2 |
Incorrect |
678 ms |
66144 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
601 ms |
67908 KB |
Output is correct |
2 |
Correct |
599 ms |
46124 KB |
Output is correct |