QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#783#323258#8215. Isomorphic DelightAakkarunaFailed.2024-08-19 17:07:562024-08-19 17:07:56

Details

Extra Test:

Accepted
time: 3ms
memory: 15636kb

input:

7

output:

YES
6
1 2
2 3
3 1
1 4
2 5
5 6

result:

ok Everything ok

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#323258#8215. Isomorphic DelightkarunaAC ✓67ms25652kbC++202.4kb2024-02-09 01:37:422024-02-09 01:37:42

answer

#include <bits/stdc++.h>
using namespace std;

const int SZ = 21;

vector<vector<pair<int, int>>> R[SZ + 1]; // rooted trees
vector<vector<pair<int, int>>> T[SZ + 1]; // non-rooted

vector<pair<int, int>> E;

void f(int n, int s, int m, int j, bool h) {
	if (s == n) {
		if (!h) R[n].push_back(E);
		else T[n].push_back(E);
		return;
	}
	assert(n - s >= m);
	if (n - s < 2 * m) {
		if (m != n - s) j = 0;
		m = n - s;
	}
	if (h && m >= (n + 1) / 2) return;

	// don't use m
	if (s + m + 1 <= n)
		f(n, s, m + 1, 0, h);

	// use m
	for (int k = j; k < R[m].size(); k++) {
		E.push_back({ 1, s + 1 });
		for (auto [u, v] : R[m][k]) {
			E.push_back({ s + u, s + v });
		}

		f(n, s + m, m, k + 1, h);

		for (int l = 0; l < m; l++) E.pop_back();
	}
}

void generate(int n) {
	if (n <= 10) f(n, 1, 1, 0, false);
	f(n, 1, 1, 0, true);
	if (n % 2 == 0) {
		for (int i = 0; i < R[n / 2].size(); i++) for (int j = 0; j < i; j++) {
			vector<pair<int, int>> E;
			E.push_back({ 1, n / 2 + 1 });
			for (auto [u, v] : R[n / 2][i]) E.push_back({ u, v });
			for (auto [u, v] : R[n / 2][j]) E.push_back({ n / 2 + u, n / 2 + v });
			T[n].push_back(E);
		}
	}
}
int main() {
	for (int n = 1; n <= SZ; n++) {
		generate(n);
	}

	int n;
	cin >> n;

	if (n == 1) {
		cout << "YES\n";
		cout << 0 << '\n';
	}
	else if (n <= 5) {
		cout << "NO\n";
	}
	else if (n <= 7) {
		cout << "YES\n";
		cout << 6 << '\n';
		cout << "1 2\n";
		cout << "2 3\n";
		cout << "3 1\n";
		cout << "1 4\n";
		cout << "2 5\n";
		cout << "5 6\n";
	}
	else {
		int m = 7, p = 0, s = 1;
		vector<pair<int, int>> V;
		while (s != n) {
			while (p == T[m].size()) {
				++m;
				p = 0;
			}
			bool f = false;
			if (s + m + (p + 1 == T[m].size() ? m + 1 : m) > n) {
				f = true;
			}
			if (s + m == n) f = false;
			if (!f) {
				//cout << m << ' ';
				for (auto [u, v] : T[m][p]) {
					V.push_back({ s + u, s + v });
				}
				++p;
				s += m;
			}
			else {
				//cout << n - s << endl;
				V.push_back({ s + 1, s + 2 });
				V.push_back({ s + 1, s + 3 });
				V.push_back({ s + 3, s + 4 });
				V.push_back({ s + 1, s + 5 });
				for (int i = 6; i <= n - s; i++) {
					V.push_back({ s + i - 1, s + i });
				}
				break;
			}
		}
		cout << "YES\n";
		cout << V.size() << '\n';
		for (auto [u, v] : V) {
			cout << u << ' ' << v << '\n';
		}
	}
}