답안 #696245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696245 2023-02-06T04:06:34 Z SanguineChameleon Mag (COCI16_mag) C++17
120 / 120
434 ms 133588 KB
// 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

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);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 16 ms 23744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 11 ms 23828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 86524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 434 ms 132732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 371 ms 131348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 348 ms 70844 KB Output is correct
2 Correct 240 ms 58544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 335 ms 72004 KB Output is correct
2 Correct 67 ms 28600 KB Output is correct
3 Correct 399 ms 133588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 29016 KB Output is correct
2 Correct 341 ms 71160 KB Output is correct
3 Correct 344 ms 48096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 67836 KB Output is correct
2 Correct 345 ms 70268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 397 ms 71492 KB Output is correct
2 Correct 254 ms 48216 KB Output is correct