This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
struct frac {
int num = 0;
int den = 1;
frac() {
}
frac(int _num, int _den) {
num = _num;
den = _den;
if (den % num == 0) {
den /= num;
num = 1;
}
}
};
frac min(frac f1, frac f2) {
if (f1.den == 0) {
return f2;
}
if (f2.den == 0) {
return f1;
}
if (f1.num * f2.den <= f2.num * f1.den) {
return f1;
}
else {
return f2;
}
}
const int ms = 1e6 + 20;
vector<int> adj[ms];
int a[ms];
int up[ms];
int low[ms];
int best[ms];
frac res;
void dfs1(int u, int pr) {
if (a[u] == 1) {
low[u] = 1;
}
for (auto v: adj[u]) {
if (v != pr) {
dfs1(v, u);
if (a[u] == 1) {
low[u] = max(low[u], low[v] + 1);
}
}
}
}
void dfs2(int u, int pr) {
best[u] = max(low[u], up[u]);
res = min(res, frac(1, best[u]));
int pref = 0;
for (auto v: adj[u]) {
if (v != pr) {
if (a[v] == 1) {
up[v] = max(up[u] + 1, pref + 1);
}
if (a[u] == 1) {
pref = max(pref, low[v] + 1);
}
}
}
int suf = 0;
reverse(adj[u].begin(), adj[u].end());
for (auto v: adj[u]) {
if (v != pr) {
if (a[v] == 1) {
up[v] = max(up[v], suf + 1);
}
if (a[u] == 1) {
suf = max(suf, low[v] + 1);
}
}
}
for (auto v: adj[u]) {
if (v != pr) {
dfs2(v, u);
}
}
}
void just_do_it() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
res = frac(a[1], 1);
for (int i = 1; i <= n; i++) {
res = min(res, frac(a[i], 1));
}
dfs1(1, -1);
if (a[1] == 1) {
up[1] = 1;
}
else {
up[1] = 0;
}
dfs2(1, -1);
for (int u = 1; u <= n; u++) {
if (a[u] != 2) {
continue;
}
int b1 = 0;
int b2 = 0;
for (auto v: adj[u]) {
if (best[v] > b1) {
b2 = b1;
b1 = best[v];
}
else if (best[v] > b2) {
b2 = best[v];
}
}
res = min(res, frac(2, b1 + b2 + 1));
}
cout << res.num << "/" << res.den;
}
// END MAIN CODE
Compilation message (stderr)
mag.cpp: In function 'void file_io(std::string, std::string)':
mag.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(fi.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(fo.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |