답안 #779197

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
779197 2023-07-11T08:55:05 Z Elias 슈퍼트리 잇기 (IOI20_supertrees) C++17
96 / 100
251 ms 22300 KB
#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>

using namespace std;

#ifdef _DEBUG
int construct(std::vector<std::vector<int>> p);
void build(std::vector<std::vector<int>> b);
#endif

#ifndef _DEBUG
#include <supertrees.h>
#endif

int construct(std::vector<std::vector<int>> p)
{
	int n = p.size();
	std::vector<std::vector<int>> answer(n, vector<int>(n));

	vector<int> all(n);
	for (int i = 0; i < n; i++)
		all[i] = i;

	auto split = [&](vector<int> a, int splitter)
	{
		int n = a.size();

		vector<set<int>> components;
		map<int, int> componentId;
		for (int i = 0; i < n; i++)
		{
			components.push_back({a[i]});
			componentId[a[i]] = i;
		}

		auto merge = [&](int a, int b)
		{
			a = componentId[a];
			b = componentId[b];

			assert(components[a].size() && components[b].size());

			if (a == b)
				return;

			if (components[a].size() < components[b].size())
				swap(a, b);

			for (auto x : components[b])
			{
				components[a].insert(x);
				componentId[x] = a;
			}

			components[b].clear();
		};

		for (int i = 0; i < n; i++)
			for (int j = i + 1; j < n; j++)
			{
				if (p[a[i]][a[j]] != splitter)
					merge(a[i], a[j]);
			}

		for (int i = 0; i < n; i++)
			for (int j = i + 1; j < n; j++)
			{
				if (p[a[i]][a[j]] == splitter && componentId[a[i]] == componentId[a[j]])
					return vector<set<int>>{};
			}

		components.erase(remove_if(components.begin(), components.end(), [](const set<int> &x)
								   { return x.size() == 0; }),
						 components.end());

		return components;
	};

	auto components = split(all, 0);

	if (components.size() == 0)
		return 0;

	for (auto component : components)
	{
		if (component.size() == 0)
			continue;

		vector<int> all_in_comp(component.begin(), component.end());

		set<int> values;

		for (int x : component)
			for (int y : component)
			{
				values.insert(p[x][y]);
			}

		if (values.count(2) && values.count(3))
			return 0;

		bool threes = values.count(3);

		auto sub_components = split(all_in_comp, threes ? 3 : 2);

		if (sub_components.size() == 0 || sub_components.size() == 2 || (threes && sub_components.size() == 3))
			return 0;

		int prev_root = -1;
		int first_root = -1;
		int second_root = -1;
		for (auto sub_component : sub_components)
		{
			if (sub_component.size() == 0)
				continue;

			int prev = -1;
			for (auto x : sub_component)
			{
				if (prev != -1)
					answer[x][prev] = answer[prev][x] = 1;
				prev = x;
			}
			int x = *sub_component.begin();
			if (prev_root != -1)
				answer[x][prev_root] = answer[prev_root][x] = 1;
			else
				first_root = x;

			if (first_root != -1 && second_root == -1)
				second_root = x;

			prev_root = x;
		}
		if (first_root != -1 && first_root != prev_root)
			answer[first_root][prev_root] = answer[prev_root][first_root] = 1;

		if (prev_root != -1 && second_root != -1 && prev_root != second_root)
			answer[second_root][prev_root] = answer[prev_root][second_root] = 1;
	}

	build(answer);
	return 1;
}

#ifdef _DEBUG

#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>

static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message)
{
	if (!cond)
	{
		printf("%s\n", message.c_str());
		fclose(stdout);
		exit(0);
	}
}

void build(std::vector<std::vector<int>> _b)
{
	check(!called, "build is called more than once");
	called = true;
	check((int)_b.size() == n, "Invalid number of rows in b");
	for (int i = 0; i < n; i++)
	{
		check((int)_b[i].size() == n, "Invalid number of columns in b");
	}
	b = _b;
}

int main()
{
	// freopen("3-21.in", "r", stdin);

	assert(scanf("%d", &n) == 1);

	p.resize(n);
	for (int i = 0; i < n; i++)
	{
		p[i].resize(n);
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			assert(scanf("%d", &p[i][j]) == 1);
		}
	}
	fclose(stdin);

	int possible = construct(p);

	check(possible == 0 || possible == 1, "Invalid return value of construct");
	if (possible == 1)
	{
		check(called, "construct returned 1 without calling build");
	}
	else
	{
		check(!called, "construct called build but returned 0");
	}

	printf("%d\n", possible);
	if (possible == 1)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (j)
				{
					printf(" ");
				}
				printf("%d", b[i][j]);
			}
			printf("\n");
		}
	}
	fclose(stdout);
}

#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 13 ms 1108 KB Output is correct
7 Correct 224 ms 22156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 13 ms 1108 KB Output is correct
7 Correct 224 ms 22156 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 11 ms 1204 KB Output is correct
13 Correct 247 ms 22132 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 99 ms 12280 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 68 ms 5744 KB Output is correct
21 Correct 199 ms 22192 KB Output is correct
22 Correct 214 ms 22076 KB Output is correct
23 Correct 204 ms 22160 KB Output is correct
24 Correct 202 ms 22168 KB Output is correct
25 Correct 73 ms 12292 KB Output is correct
26 Correct 73 ms 12164 KB Output is correct
27 Correct 229 ms 22160 KB Output is correct
28 Correct 216 ms 22128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 11 ms 1188 KB Output is correct
9 Correct 198 ms 22136 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 9 ms 1216 KB Output is correct
13 Correct 251 ms 22140 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 756 KB Output is correct
17 Correct 88 ms 12176 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 47 ms 5760 KB Output is correct
22 Correct 225 ms 22196 KB Output is correct
23 Correct 200 ms 22148 KB Output is correct
24 Correct 248 ms 22168 KB Output is correct
25 Correct 94 ms 12164 KB Output is correct
26 Correct 74 ms 12292 KB Output is correct
27 Correct 200 ms 22140 KB Output is correct
28 Correct 222 ms 22128 KB Output is correct
29 Correct 96 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 45 ms 5732 KB Output is correct
5 Correct 208 ms 22224 KB Output is correct
6 Correct 182 ms 22144 KB Output is correct
7 Correct 205 ms 22156 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 45 ms 5832 KB Output is correct
10 Correct 190 ms 22184 KB Output is correct
11 Correct 185 ms 22300 KB Output is correct
12 Correct 205 ms 22080 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 46 ms 5732 KB Output is correct
17 Correct 189 ms 22144 KB Output is correct
18 Correct 185 ms 22160 KB Output is correct
19 Correct 204 ms 22156 KB Output is correct
20 Correct 182 ms 22156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 13 ms 1108 KB Output is correct
7 Correct 224 ms 22156 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 11 ms 1204 KB Output is correct
13 Correct 247 ms 22132 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 99 ms 12280 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 68 ms 5744 KB Output is correct
21 Correct 199 ms 22192 KB Output is correct
22 Correct 214 ms 22076 KB Output is correct
23 Correct 204 ms 22160 KB Output is correct
24 Correct 202 ms 22168 KB Output is correct
25 Correct 73 ms 12292 KB Output is correct
26 Correct 73 ms 12164 KB Output is correct
27 Correct 229 ms 22160 KB Output is correct
28 Correct 216 ms 22128 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 11 ms 1188 KB Output is correct
37 Correct 198 ms 22136 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 9 ms 1216 KB Output is correct
41 Correct 251 ms 22140 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 756 KB Output is correct
45 Correct 88 ms 12176 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 47 ms 5760 KB Output is correct
50 Correct 225 ms 22196 KB Output is correct
51 Correct 200 ms 22148 KB Output is correct
52 Correct 248 ms 22168 KB Output is correct
53 Correct 94 ms 12164 KB Output is correct
54 Correct 74 ms 12292 KB Output is correct
55 Correct 200 ms 22140 KB Output is correct
56 Correct 222 ms 22128 KB Output is correct
57 Correct 96 ms 12160 KB Output is correct
58 Correct 0 ms 232 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 724 KB Output is correct
61 Correct 88 ms 12188 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 45 ms 5836 KB Output is correct
66 Correct 72 ms 12164 KB Output is correct
67 Correct 73 ms 12288 KB Output is correct
68 Correct 67 ms 12176 KB Output is correct
69 Correct 88 ms 12236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 13 ms 1108 KB Output is correct
7 Correct 224 ms 22156 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 11 ms 1204 KB Output is correct
13 Correct 247 ms 22132 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 99 ms 12280 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 68 ms 5744 KB Output is correct
21 Correct 199 ms 22192 KB Output is correct
22 Correct 214 ms 22076 KB Output is correct
23 Correct 204 ms 22160 KB Output is correct
24 Correct 202 ms 22168 KB Output is correct
25 Correct 73 ms 12292 KB Output is correct
26 Correct 73 ms 12164 KB Output is correct
27 Correct 229 ms 22160 KB Output is correct
28 Correct 216 ms 22128 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 11 ms 1188 KB Output is correct
37 Correct 198 ms 22136 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 9 ms 1216 KB Output is correct
41 Correct 251 ms 22140 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 756 KB Output is correct
45 Correct 88 ms 12176 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 47 ms 5760 KB Output is correct
50 Correct 225 ms 22196 KB Output is correct
51 Correct 200 ms 22148 KB Output is correct
52 Correct 248 ms 22168 KB Output is correct
53 Correct 94 ms 12164 KB Output is correct
54 Correct 74 ms 12292 KB Output is correct
55 Correct 200 ms 22140 KB Output is correct
56 Correct 222 ms 22128 KB Output is correct
57 Correct 96 ms 12160 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 45 ms 5732 KB Output is correct
62 Correct 208 ms 22224 KB Output is correct
63 Correct 182 ms 22144 KB Output is correct
64 Correct 205 ms 22156 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 45 ms 5832 KB Output is correct
67 Correct 190 ms 22184 KB Output is correct
68 Correct 185 ms 22300 KB Output is correct
69 Correct 205 ms 22080 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 46 ms 5732 KB Output is correct
74 Correct 189 ms 22144 KB Output is correct
75 Correct 185 ms 22160 KB Output is correct
76 Correct 204 ms 22156 KB Output is correct
77 Correct 182 ms 22156 KB Output is correct
78 Correct 0 ms 232 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 724 KB Output is correct
81 Correct 88 ms 12188 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 45 ms 5836 KB Output is correct
86 Correct 72 ms 12164 KB Output is correct
87 Correct 73 ms 12288 KB Output is correct
88 Correct 67 ms 12176 KB Output is correct
89 Correct 88 ms 12236 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 5 ms 824 KB Output is correct
93 Correct 92 ms 12284 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 19 ms 3340 KB Output is correct
98 Correct 69 ms 12288 KB Output is correct
99 Correct 70 ms 12288 KB Output is correct
100 Correct 68 ms 12164 KB Output is correct
101 Correct 92 ms 12176 KB Output is correct
102 Incorrect 210 ms 22100 KB Answer gives possible 1 while actual possible 0
103 Halted 0 ms 0 KB -