QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270111#5665. AA Country and King DreamoonSTnofarjo#WA 46ms3476kbC++203.7kb2023-11-30 15:29:432023-11-30 15:29:44

Judging History

你现在查看的是最新测评结果

  • [2023-11-30 15:29:44]
  • 评测
  • 测评结果:WA
  • 用时:46ms
  • 内存:3476kb
  • [2023-11-30 15:29:43]
  • 提交

answer

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

void solve() {
	int n;
	cin >> n;
	vector<vector<int>> adj(n + 1);
	vector<int> pos(2 * n - 1), par(n + 1);
	vector<bool> used(n + 1);
	for (int i = 0; i < 2 * n - 1; i++) {
		cin >> pos[i];
		if (pos[i] != 0) used[pos[i]] = true;
	}
	int a = -1, b = -1, sa = -1, sb = -1;
	for (int i = 0; i < 2 * n - 2; i++) {
		if (pos[i] != 0 && pos[i + 1] != 0) {
			adj[pos[i]].push_back(pos[i + 1]);
			adj[pos[i + 1]].push_back(pos[i]);
		}
		if (pos[i] != 0 && pos[i + 1] == 0) {
			a = pos[i];
			sa = i;
		}
		if (pos[i] == 0 && pos[i + 1] != 0) {
			b = pos[i + 1];
			sb = i + 1;
		}
	}
	if (a == -1 && b == -1) {
		for (int i = 1; i <= 2 * n - 1; i++) {
			if (i & 1) {
				cout << 1 << ' ';
			} else {
				cout << (i / 2) + 1 << ' ';
			}
		}
		cout << '\n';
		return;
	}

	const function<void(int, int)> dfs = [&](int v, int p) -> void {
		for (auto c : adj[v]) {
			if (c == p) continue;
			par[c] = v;
			dfs(c, v);
		}
	};

	dfs(1, 1);
	set<int> unused;
	for (int i = 1; i <= n; i++) {
		if (!used[i]) {
			unused.insert(i);
		}
	}

	vector<int> ret(2 * n - 1);
	for (int i = 0; i < 2 * n - 1; i++) {
		ret[i] = pos[i];
	}

	if (a == -1) {
		ret[0] = 1;
		a = 1;
		sa = 0;
	}

	if (b == -1) {
		ret.back() = 1;
		b = 1;
		sb = 2 * n - 2;
	}

	auto find_path = [&](int u, int v) -> vector<int> {
		int cu = u;
		set<int> se;
		while (cu != 1) {
			se.insert(cu);
			cu = par[cu];
		}
		se.insert(1);
		int cv = v;
		int lca = -1;
		while (cv != 1) {
			if (se.count(cv)) {
				lca = cv;
				break;
			}
			cv = par[cv];
		}
		if (cv == 1) lca = 1;

		cu = u, cv = v;
		vector<int> pp, qq, ret;
		while (cu != lca) pp.push_back(cu), cu = par[cu];
		while (cv != lca) qq.push_back(cv), cv = par[cv];
		reverse(qq.begin(), qq.end());
		for (auto c : pp) ret.push_back(c);
		ret.push_back(lca);
		for (auto c : qq) ret.push_back(c);
		return ret;
	};


	vector<int> path = find_path(a, b);

	// cerr << "PATH: ";
	// for (auto e : path) {
	// 	cerr << e << ' ';
	// }
	// cerr << '\n';

	int idx = 0;
	int cur_par = a;
	int ptr = sa + 1;
	while (ptr < sb) {
		vector<int> tmp;
		while (!unused.empty() && *unused.begin() < cur_par) {
			tmp.push_back(*unused.begin());
			unused.erase(unused.begin());
		}
		for (int i = 0; i < tmp.size(); i++) {
			// cerr << "TES x " << ptr << ' ' << tmp[i] << '\n';
			ret[ptr++] = tmp[i];
		}
		for (int i = (int)tmp.size() - 2; i >= 0; i--) {
			// cerr << "TES y " << ptr << ' ' << tmp[i] << '\n';
			ret[ptr++] = tmp[i];
		}
		if (!tmp.empty()) {
			// cerr << "TES z " << ptr << ' ' << cur_par << '\n';
			ret[ptr++] = cur_par;
		}
		// cerr << tmp.size() << '\n';
		tmp.clear();
		if (idx + 1 < path.size()) {
			int nx_par = path[idx + 1];
			while (!unused.empty() && *unused.begin() < nx_par) {
				tmp.push_back(*unused.begin());
				unused.erase(unused.begin());
			}
			for (int i = 0; i < tmp.size(); i++) {
				// cerr << "TES a " << ptr << ' ' << tmp[i] << '\n';
				ret[ptr++] = tmp[i];
				// cerr << "TES b " << ptr << ' ' << cur_par << '\n';
				ret[ptr++] = cur_par;
			}
			idx++;
			ret[ptr++] = nx_par;
			cur_par = nx_par;
		} else {
			while (!unused.empty()) {
				tmp.push_back(*unused.begin());
				unused.erase(unused.begin());
			}
			for (int i = 0; i < tmp.size(); i++) {
				// cerr << "TES c " << ptr << ' ' << tmp[i] << '\n';
				ret[ptr++] = tmp[i];
				// cerr << "TES d " << ptr << ' ' << cur_par << '\n';
				ret[ptr++] = cur_par;
			}
		}
	}

	for (auto e : ret) {
		cout << e << ' ';
	}
	cout << '\n';
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);

	int tc;
	cin >> tc;
	while (tc--) {
		solve();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3476kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 

result:

ok 9 lines

Test #2:

score: -100
Wrong Answer
time: 46ms
memory: 3408kb

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:

1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3...

result:

wrong answer 35th lines differ - expected: '1 3 1 2 1', found: '1 3 2 3 1 '