// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n;
int a[maxN];
vector<int> adj[maxN];
int f[maxN][2];
int res0 = 0, res1 = 0;
void ReadInput()
{
cin >> n;
for(int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1; i<=n; i++)
cin >> a[i];
}
void dfs(int u, int par)
{
if(u != 1 && adj[u].size() == 1)
{
if(a[u] == 1)
{
f[u][0] = 1;
res0 = max(res0, f[u][0]);
}
return;
}
for(int v : adj[u])
{
if(v == par) continue;
dfs(v, u);
}
vector<pair<int, int>> vc0, vc1;
for(int v : adj[u])
{
if(v == par) continue;
if(a[u] == 1)
{
f[u][0] = max(f[u][0], f[v][0] + 1);
f[u][1] = max(f[u][1], f[v][1] + 1);
}
if(a[u] == 2)
f[u][1] = max(f[u][1], f[v][1]);
vc0.pb({f[v][0], v});
vc1.pb({f[v][1], v});
}
sort(vc0.begin(), vc0.end(), greater<pair<int, int>>());
res0 = max(res0, f[u][0]);
res1 = max(res1, f[u][1]);
if(a[u] == 2 && vc0.size() > 1) res1 = max(res1, vc0[0].fi + vc0[1].fi);
if(a[u] == 1 && vc0.size() > 1) res0 = max(res0, vc0[0].fi + vc0[1].fi + 1);
if(a[u] == 1)
{
if(vc0[0].se != vc1[0].se) res1 = max(res1, vc0[0].fi + vc1[0].fi + 1);
else
{
if(vc1.size() > 1) res1 = max(res1, vc0[0].fi + vc1[1].fi + 1);
if(vc0.size() > 1) res1 = max(res1, vc0[1].fi + vc1[0].fi + 1);
}
}
}
void Solve()
{
int val = *min_element(a + 1, a + n + 1);
if(val > 1)
{
cout << val << '/' << 1;
return;
}
dfs(1, 0);
pair<int, int> p = {2, res1 + 1}, q = {1, res0};
if(p.fi * q.se > q.fi * p.se) p = q;
int t = __gcd(p.fi, p.se);
p.fi /= t;
p.se /= t;
cout << p.fi << "/" << p.se;
}
int32_t main()
{
//freopen("sol.inp", "r", stdin);
//freopen("sol.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
12 ms |
23824 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
143032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
455 ms |
262144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
78780 KB |
Output is correct |
2 |
Incorrect |
273 ms |
64616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
346 ms |
93080 KB |
Output is correct |
2 |
Correct |
61 ms |
30224 KB |
Output is correct |
3 |
Runtime error |
419 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
30148 KB |
Output is correct |
2 |
Correct |
354 ms |
80184 KB |
Output is correct |
3 |
Correct |
274 ms |
54512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
327 ms |
80468 KB |
Output is correct |
2 |
Correct |
349 ms |
78024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
350 ms |
80040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |