/*
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 h[maxn], d[maxn]; ///h : update from v to u, d : update from par to u
void dfs(int u, int p)
{
d[u] = h[u] = (c[u] == 1);
int ma[2] = {};
for (int v:a[u]) if (v != p)
{
d[v] = (c[v] == 1) ? d[u] + 1 : 0;
dfs(v, u);
if (h[v] > ma[0]) ma[1] = ma[0], ma[0] = h[v];
else if (h[v] > ma[1]) ma[1] = h[v];
if (c[u] == 1) h[u] = max(h[u], (c[v] == 1) ? h[v] + 1 : 0);
}
vector <int> tmp;
tmp.push_back(ma[0]);
tmp.push_back(ma[1]);
tmp.push_back(d[p]);
//cout << u << ' ' << ma[0] << ' ' << ma[1]; el;
sort(tmp.begin(), tmp.end());
frac cur(c[u], tmp[1] + tmp[2] + 1);
if (cur < ans) ans = cur;
}
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);
ans.output();
}
Compilation message
mag.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
51 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24016 KB |
Output is correct |
2 |
Correct |
12 ms |
23828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
357 ms |
122192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
442 ms |
204824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
386 ms |
81924 KB |
Output is correct |
2 |
Correct |
277 ms |
66704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
84836 KB |
Output is correct |
2 |
Correct |
63 ms |
29664 KB |
Output is correct |
3 |
Incorrect |
457 ms |
209500 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
29552 KB |
Output is correct |
2 |
Correct |
363 ms |
82928 KB |
Output is correct |
3 |
Correct |
299 ms |
53508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
349 ms |
80592 KB |
Output is correct |
2 |
Correct |
381 ms |
83052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
83084 KB |
Output is correct |
2 |
Correct |
319 ms |
53412 KB |
Output is correct |