답안 #381146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381146 2021-03-24T16:25:18 Z BlancaHM 사육제 (CEOI14_carnival) C++14
100 / 100
10 ms 384 KB
#include <iostream>
#include <vector>
#include <map>
#include <unordered_set>
#include <set>
#include <algorithm>
using namespace std;

vector<int> par;
vector<int> roots;

int root(int a) {
	if (a == par[a])
		return a;
	return par[a] = root(par[a]);
}

void merge(int i, int j) {
	if (i == j)
		return;
	par[j] = i;
	auto it = find(roots.begin(), roots.end(), j);
	roots.erase(it, it+1);
}

void solve(int N, int C) {
	if (C == 1) {
		par.assign(N, 0);
		return;
	}
	par = vector<int>(N);
	for (int i = 0; i < N; i++)
		par[i] = i;
	roots = {};
	int ans, costumesDiscovered = 0;
	for (int i = 0; i < N; i++) {
		if (N - i == C - costumesDiscovered)
			continue;
		if(roots.size()) {
			cout << (int)roots.size()+1 << " ";
			for (int r: roots)
				cout << r+1 << ' ';
			cout << i+1 << '\n';
			cin >> ans;
			if (ans == (int) roots.size()) {
				// not a new root
				int lo = 0, hi = (int) roots.size()-1, mid, k = 0;
				while(lo <= hi) {
					if (lo == hi) {
						k = lo;
						break;
					}
					mid = lo + (hi-lo)/2;
					cout << (mid-lo+2) << " ";
					for (int j = lo; j <= mid; j++)
						cout << roots[j]+1 << ' ';
					cout << i+1 << '\n';
					cin >> ans;
					if (ans == (mid-lo+1)) {
						hi = mid;
						k = mid;
					} else lo = mid+1;
				}
				merge(i, roots[k]);
			}
		}
		if (par[i] == i) {
			costumesDiscovered++;
			roots.push_back(i);
		}
	}
}

int main() {
	int N, C;
	cin >> N;
	cout << N;
	for (int i = 1; i <= N; i++)
		cout << " " << i;
	cout << '\n';
	cin >> C;
	solve(N, C);
	map<int, int> roots;
	int c = 1;
	for (int i = 0; i < N; i++) {
		if (roots.find(root(i)) == roots.end()) {
			roots[root(i)] = c++;
		}
	}
	int costumes[N];
	for (int i = 0; i < N; i++)
		costumes[i] = roots[root(i)];
	cout << "0";
	for (int i = 0; i < N; i++)
		cout << " " << costumes[i];
	cout << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 364 KB Output is correct
2 Correct 9 ms 364 KB Output is correct
3 Correct 6 ms 364 KB Output is correct
4 Correct 5 ms 364 KB Output is correct
5 Correct 4 ms 364 KB Output is correct
6 Correct 4 ms 364 KB Output is correct
7 Correct 7 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 364 KB Output is correct
2 Correct 10 ms 364 KB Output is correct
3 Correct 5 ms 364 KB Output is correct
4 Correct 5 ms 364 KB Output is correct
5 Correct 7 ms 364 KB Output is correct
6 Correct 6 ms 364 KB Output is correct
7 Correct 6 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 7 ms 364 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 364 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 7 ms 376 KB Output is correct
7 Correct 7 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 364 KB Output is correct
2 Correct 6 ms 364 KB Output is correct
3 Correct 7 ms 364 KB Output is correct
4 Correct 5 ms 364 KB Output is correct
5 Correct 7 ms 364 KB Output is correct
6 Correct 9 ms 364 KB Output is correct
7 Correct 6 ms 380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 364 KB Output is correct
2 Correct 8 ms 364 KB Output is correct
3 Correct 7 ms 364 KB Output is correct
4 Correct 7 ms 364 KB Output is correct
5 Correct 4 ms 380 KB Output is correct
6 Correct 6 ms 364 KB Output is correct
7 Correct 4 ms 364 KB Output is correct