/*
www.youtube.com/YugiHackerChannel
oj.vnoi.info/user/YugiHackerKhongCopCode
*/
#include<bits/stdc++.h>
#define el cout<<"\n"
#define f0(i,n) for(int i=0;i<n;++i)
#define f1(i,n) for(int i=1;i<=n;++i)
#define maxn 1000006
using namespace std;
struct frac
{
long long x, y;
frac(){}
frac(long long x, long long y): x(x), y(y){}
friend bool operator < (frac a, frac b)
{
return a.x * b.y < a.y * b.x;
}
void output()
{
long long g = __gcd(x, y);
cout << x/g << '/' << y/g;
}
}ans(1e9, 1);
int n;
vector <int> a[maxn];
int c[maxn];
int down[2][maxn], up[maxn];
void dfs(int u, int p)
{
if (c[u] > 2) return;
for (int v:a[u]) if (v != p)
{
dfs(v, u);
if (c[v] == 1)
{
if (down[0][u] < down[0][v] + 1)
down[1][u] = down[0][u], down[0][u] = down[0][v] + 1;
else if (down[1][u] < down[0][v] + 1)
down[1][u] = down[0][v] + 1;
}
}
}
void dfs$(int u, int p)
{
if (c[u] > 2) return;
for (int v:a[u]) if (v != p)
{
if (c[u] == 1)
{
up[v] = up[u] + 1;
if (c[v] == 1)
{
if (down[0][v] + 1 == down[0][u]) up[v] = max(up[v], down[1][u] + 1);
}
else up[v] = max(up[v], down[0][u] + 1);
}
dfs$(v, u);
}
vector <int> tmp;
tmp.push_back(up[u]);
tmp.push_back(down[0][u]);
tmp.push_back(down[1][u]);
sort(tmp.begin(), tmp.end(), greater<int>());
ans = min(ans, frac(c[u], tmp[0] + tmp[1] + 1));
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
f1 (i, n-1)
{
int u, v; cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
f1 (i, n) cin >> c[i];
dfs(1, 0);
dfs$(1, 0);
ans.output();
}
Compilation message
mag.cpp:69:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
69 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Incorrect |
13 ms |
23856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
105156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
461 ms |
178892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
294 ms |
58060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
377 ms |
70464 KB |
Output is correct |
2 |
Incorrect |
208 ms |
49472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
71572 KB |
Output is correct |
2 |
Correct |
67 ms |
28620 KB |
Output is correct |
3 |
Correct |
482 ms |
180308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
28628 KB |
Output is correct |
2 |
Correct |
388 ms |
71596 KB |
Output is correct |
3 |
Correct |
335 ms |
47756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
346 ms |
68296 KB |
Output is correct |
2 |
Correct |
369 ms |
69784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
396 ms |
71504 KB |
Output is correct |
2 |
Correct |
310 ms |
47820 KB |
Output is correct |