답안 #706932

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706932 2023-03-08T06:51:36 Z SanguineChameleon CEOI16_icc (CEOI16_icc) C++17
100 / 100
122 ms 608 KB
#include <bits/stdc++.h>
using namespace std;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

#ifndef KAMIRULEZ

#include <icc.h>

#endif

using namespace std;

#ifdef KAMIRULEZ

int pt = 0;
pair<int, int> e[120];
bool f[120][120];

int query(int size_a, int size_b, int a[], int b[]) {
	for (int i = 0; i < size_a; i++) {
		int u = a[i];
		for (int j = 0; j < size_b; j++) {
			int v = b[j];
			assert(u != v);
		}
	}
	for (int i = 0; i < size_a; i++) {
		int u = a[i];
		for (int j = 0; j < size_b; j++) {
			int v = b[j];
			if (f[u][v]) {
				return 1;
			}
		}
	}
	return 0;
}

void add() {
	int u = e[pt].first;
	int v = e[pt].second;
	f[u][v] = true;
	f[v][u] = true;
}

void setRoad(int u, int v) {
	assert(((e[pt].first == u) && (e[pt].second == v)) || ((e[pt].first == v) && (e[pt].second == u)));
	pt++;
	add();
}

#endif

ostream& operator<<(ostream &out, vector<int> q) {
	int sz = q.size();
	for (int i = 0; i < sz; i++) {
		out << q[i];
		if (i < sz - 1) {
			out << " ";
		}
	}
	return out;
}

int query_vec(vector<int> A, vector<int> B) {
	int a[120] = {};
	int b[120] = {};
	int size_a = A.size();
	int size_b = B.size();
	for (int i = 0; i < size_a; i++) {
		a[i] = A[i];
	}
	for (int i = 0; i < size_b; i++) {
		b[i] = B[i];
	}
	return query(size_a, size_b, a, b);
}

void run(int n) {
	vector<vector<int>> comps;
	for (int i = 1; i <= n; i++) {
		comps.push_back({i});
	}
	for (int k = 0; k < n - 1; k++) {
		vector<int> orig(n - k);
		iota(orig.begin(), orig.end(), 0);
		vector<vector<int>> cur = {orig};
		shuffle(cur.begin(), cur.end(), gen);
		while (true) {
			vector<vector<int>> nxt;
			vector<int> ql;
			vector<int> qr;
			for (auto x: cur) {
				int sz = x.size();
				vector<int> lv(x.begin(), x.begin() + sz / 2);
				vector<int> rv(x.begin() + sz / 2, x.end());
				if (!lv.empty()) {
					nxt.push_back(lv);
					for (auto id: lv) {
						ql.insert(ql.end(), comps[id].begin(), comps[id].end());
					}
				}
				if (!rv.empty()) {
					nxt.push_back(rv);
					for (auto id: rv) {
						qr.insert(qr.end(), comps[id].begin(), comps[id].end());
					}
				}
			}
			if (query_vec(ql, qr)) {
				while (true) {
					int sz = ql.size();
					if (sz == 1) {
						break;
					}
					vector<int> lv(ql.begin(), ql.begin() + sz / 2);
					vector<int> rv(ql.begin() + sz / 2, ql.end());
					if (!lv.empty() && query_vec(lv, qr)) {
						ql = lv;
					}
					else {
						ql = rv;
					}
				}
				while (true) {
					int sz = qr.size();
					if (sz == 1) {
						break;
					}
					vector<int> lv(qr.begin(), qr.begin() + sz / 2);
					vector<int> rv(qr.begin() + sz / 2, qr.end());
					if (!lv.empty() && query_vec(lv, ql)) {
						qr = lv;
					}
					else {
						qr = rv;
					}
				}
				int u = ql[0];
				int v = qr[0];
				int cu = -1;
				int cv = -1;
				for (int i = 0; i < n - k; i++) {
					for (auto x: comps[i]) {
						if (u == x) {
							cu = i;
							break;
						}
					}
				}
				for (int i = 0; i < n - k; i++) {
					for (auto x: comps[i]) {
						if (v == x) {
							cv = i;
							break;
						}
					}
				}
				comps[cu].insert(comps[cu].end(), comps[cv].begin(), comps[cv].end());
				comps.erase(comps.begin() + cv);
				setRoad(u, v);
				break;
			}
			else {
				cur.swap(nxt);
			}
		}
	}
}

#ifdef KAMIRULEZ

int main() {
	freopen("kamirulez.inp", "r", stdin);
	freopen("kamirulez.out", "w", stdout);
	int n;
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		e[i] = {u, v};
	}
	add();
	run(n);
}

#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 468 KB Ok! 101 queries used.
2 Correct 8 ms 468 KB Ok! 101 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 468 KB Ok! 537 queries used.
2 Correct 32 ms 492 KB Ok! 654 queries used.
3 Correct 31 ms 468 KB Ok! 662 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 504 KB Ok! 1578 queries used.
2 Correct 106 ms 524 KB Ok! 1619 queries used.
3 Correct 102 ms 468 KB Ok! 1624 queries used.
4 Correct 99 ms 528 KB Ok! 1585 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 512 KB Ok! 1585 queries used.
2 Correct 102 ms 540 KB Ok! 1605 queries used.
3 Correct 106 ms 520 KB Ok! 1619 queries used.
4 Correct 106 ms 608 KB Ok! 1580 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 520 KB Ok! 1615 queries used.
2 Correct 102 ms 528 KB Ok! 1623 queries used.
3 Correct 110 ms 520 KB Ok! 1630 queries used.
4 Correct 106 ms 520 KB Ok! 1640 queries used.
5 Correct 118 ms 524 KB Ok! 1597 queries used.
6 Correct 104 ms 524 KB Ok! 1584 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 524 KB Ok! 1623 queries used.
2 Correct 117 ms 468 KB Ok! 1619 queries used.