# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
726404 |
2023-04-18T21:10:00 Z |
YugiHacker |
Mag (COCI16_mag) |
C++17 |
|
544 ms |
197736 KB |
/*
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]; ///h : update from v to u, d : update from par to u
void dfs(int u, int p)
{
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)
{
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>());
//cout << u << ' ' << down[0][u] << ' ' << down[1][u] << ' ' << up[u], el;
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:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23820 KB |
Output is correct |
2 |
Correct |
17 ms |
23824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
23852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
119264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
544 ms |
194292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
513 ms |
193480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
429 ms |
85884 KB |
Output is correct |
2 |
Correct |
315 ms |
69580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
88808 KB |
Output is correct |
2 |
Correct |
96 ms |
30184 KB |
Output is correct |
3 |
Correct |
516 ms |
197736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
29984 KB |
Output is correct |
2 |
Correct |
423 ms |
86664 KB |
Output is correct |
3 |
Correct |
311 ms |
55364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
390 ms |
83892 KB |
Output is correct |
2 |
Correct |
407 ms |
86944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
431 ms |
86876 KB |
Output is correct |
2 |
Correct |
428 ms |
55440 KB |
Output is correct |