#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 1000000 + 3;
int n, z[N], p[N], udp[N], ddp[N];
vector <vector <int> > v, mx;
void udfs(int x){
p[x] = 1;
for (auto u: v[x]){
if (p[u]) continue;
udfs(u);
mx[x].push_back(udp[u]);
udp[x] = max(udp[x], udp[u]);
}
if (z[x] == 1) udp[x]++;
else udp[x] = 0;
sort(all(mx[x]));
reverse(all(mx[x]));
}
int ans ;
int e_ans ;
int mn = inf;
void ddfs(int x, int way){
p[x] = 1;
mn = min(mn, z[x]);
e_ans = max(e_ans, way);
if (z[x] == 2){
if (!mx[x].empty()){
vector <int> buff;
buff.push_back(way);
buff.push_back(mx[x][0]);
if (mx[x].size() > 1)
buff.push_back(mx[x][1]);
sort(all(buff));
// cout << x << " : ";
// for (auto u: v[x]) cout << u << " "; cout << "\n";
ans = max(ans, buff[(buff.size() - 1) / 2] * 2 + 1);
}
}
for (auto u: v[x]){
if (p[u]) continue;
if (z[x] != 1) {
ddfs(u, 0);
continue;
}
e_ans = max(e_ans, udp[u]);
if (udp[u] == mx[x][0]){
ddfs(u, max(way, mx[x][1]));
}else{
ddfs(u, max(way, mx[x][0]));
}
}
if (mx[x].size()){
int t = mx[x][0];
if (mx[x].size() > 1) t += mx[x][1];
if (z[x] == 1) e_ans = max(e_ans, t + 1);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n;
v.resize(n);
mx.resize(n);
for (int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
--a; --b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 0; i < n; i++) cin >> z[i];
udfs(0);
memset(p, 0, sizeof p);
ddfs(0, 0);
if (ans){
if (e_ans * 2 < ans)
cout << "2/"<<ans;
else
cout << "1/"<<e_ans;
}else
if (e_ans){
cout << "1/"<<e_ans;
}else
cout << mn<<"/1";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4608 KB |
Output is correct |
2 |
Correct |
7 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
4352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
722 ms |
147680 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
4352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
607 ms |
210804 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
534 ms |
132712 KB |
Output is correct |
2 |
Correct |
394 ms |
101368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
518 ms |
113172 KB |
Output is correct |
2 |
Correct |
95 ms |
16120 KB |
Output is correct |
3 |
Incorrect |
621 ms |
233036 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
15992 KB |
Output is correct |
2 |
Correct |
536 ms |
134072 KB |
Output is correct |
3 |
Correct |
447 ms |
63608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
121296 KB |
Output is correct |
2 |
Correct |
517 ms |
119672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
543 ms |
134164 KB |
Output is correct |
2 |
Correct |
483 ms |
63736 KB |
Output is correct |