답안 #1077977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077977 2024-08-27T11:20:59 Z Tsovak 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
170 ms 29096 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;
int c[N][N];
bool ans[N][N];
int n;

int p[N];

int gp(int u)
{
	if (u == p[u]) return u;
	return p[u] = gp(p[u]);
}

void us(int u, int v)
{
	u = gp(u); v = gp(v);
	p[v] = u;

	return;
}

vector<int> vv[N];

bool allare(vector<int>& v, int h)
{
	int i, j;

	for (i = 0; i < v.size() - 1; i++) {
		for (j = i + 1; j < v.size(); j++) {
			if (c[v[i]][v[j]] != h) return false;
		}
	}

	return true;
}

int construct(vector<vector<int>> p0)
{
	int i, j, k;
	int u, v;

	n = p0.size();
	for (i = 0; i < n; i++) for (j = 0; j < n; j++) c[i + 1][j + 1] = p0[i][j];


	for (i = 1; i <= n; i++) p[i] = i;

	for (i = 1; i < n; i++) {
		for (j = i + 1; j <= n; j++) {
			if (c[i][j] == 3) return 0;

			if (c[i][j]) us(i, j);
		}
	}

	for (i = 1; i < n; i++) {
		for (j = i + 1; j <= n; j++) {
			if (c[i][j]) continue;

			if (gp(i) == gp(j)) return 0;
		}
	}

	for (i = 1; i <= n; i++) vv[gp(i)].push_back(i);

	for (i = 1; i <= n; i++) {
		if (vv[i].size() <= 1) continue;

		if (allare(vv[i], 1)) {
			for (j = 0; j < vv[i].size() - 1; j++) ans[vv[i][j]][vv[i][j + 1]] = ans[vv[i][j + 1]][vv[i][j]] = 1;

			continue;
		}

		if (allare(vv[i], 2)) {
			if (vv[i].size() == 2) return 0;

			for (j = 0; j < vv[i].size() - 1; j++) ans[vv[i][j]][vv[i][j + 1]] = ans[vv[i][j + 1]][vv[i][j]] = 1;
			ans[vv[i].back()][vv[i].front()] = ans[vv[i].front()][vv[i].back()] = 1;

			continue;
		}


		for (j = 1; j <= n; j++) p[j] = j;

		for (j = 0; j < vv[i].size() - 1; j++) {
			for (k = j + 1; k < vv[i].size(); k++) {
				u = vv[i][j]; v = vv[i][k];

				if (c[u][v] == 1) us(u, v);
			}
		}

		for (j = 0; j < vv[i].size() - 1; j++) {
			for (k = j + 1; k < vv[i].size(); k++) {
				u = vv[i][j]; v = vv[i][k];

				if (c[u][v] == 2 && gp(u) == gp(v)) return 0;
			}
		}


		set<int> st;

		for (j = 0; j < vv[i].size(); j++) {
			u = vv[i][j];

			st.insert(gp(u));

			if (u != gp(u)) ans[u][gp(u)] = ans[gp(u)][u] = 1;
		}

		vector<int> ve;
		for (auto x : st) ve.push_back(x);

		if (ve.size() <= 2) return 0;

		for (j = 0; j < ve.size() - 1; j++) ans[ve[j]][ve[j + 1]] = ans[ve[j + 1]][ve[j]] = 1;
		ans[ve.back()][ve.front()] = ans[ve.front()][ve.back()] = 1;
	}

	vector<vector<int>> res(n, vector<int>(n));

	for (i = 0; i < n; i++) for (j = 0; j < n; j++) res[i][j] = ans[i + 1][j + 1];

	build(res);

	return 1;
}

Compilation message

supertrees.cpp: In function 'bool allare(std::vector<int>&, int)':
supertrees.cpp:32:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |  for (i = 0; i < v.size() - 1; i++) {
      |              ~~^~~~~~~~~~~~~~
supertrees.cpp:33:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |   for (j = i + 1; j < v.size(); j++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:74:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |    for (j = 0; j < vv[i].size() - 1; j++) ans[vv[i][j]][vv[i][j + 1]] = ans[vv[i][j + 1]][vv[i][j]] = 1;
      |                ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |    for (j = 0; j < vv[i].size() - 1; j++) ans[vv[i][j]][vv[i][j + 1]] = ans[vv[i][j + 1]][vv[i][j]] = 1;
      |                ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:91:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |   for (j = 0; j < vv[i].size() - 1; j++) {
      |               ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |    for (k = j + 1; k < vv[i].size(); k++) {
      |                    ~~^~~~~~~~~~~~~~
supertrees.cpp:99:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |   for (j = 0; j < vv[i].size() - 1; j++) {
      |               ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:100:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |    for (k = j + 1; k < vv[i].size(); k++) {
      |                    ~~^~~~~~~~~~~~~~
supertrees.cpp:110:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |   for (j = 0; j < vv[i].size(); j++) {
      |               ~~^~~~~~~~~~~~~~
supertrees.cpp:123:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |   for (j = 0; j < ve.size() - 1; j++) ans[ve[j]][ve[j + 1]] = ans[ve[j + 1]][ve[j]] = 1;
      |               ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 5 ms 2392 KB Output is correct
7 Correct 164 ms 29008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 5 ms 2392 KB Output is correct
7 Correct 164 ms 29008 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 472 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 5 ms 2140 KB Output is correct
13 Correct 124 ms 28104 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1628 KB Output is correct
17 Correct 61 ms 14164 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 32 ms 8860 KB Output is correct
21 Correct 126 ms 29080 KB Output is correct
22 Correct 116 ms 28956 KB Output is correct
23 Correct 133 ms 28876 KB Output is correct
24 Correct 132 ms 28240 KB Output is correct
25 Correct 54 ms 14104 KB Output is correct
26 Correct 80 ms 14168 KB Output is correct
27 Correct 122 ms 28888 KB Output is correct
28 Correct 118 ms 28244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 6 ms 2140 KB Output is correct
9 Correct 160 ms 28052 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 432 KB Output is correct
12 Correct 6 ms 2392 KB Output is correct
13 Correct 170 ms 29008 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 1628 KB Output is correct
17 Correct 58 ms 14124 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 31 ms 8792 KB Output is correct
22 Correct 121 ms 29008 KB Output is correct
23 Correct 123 ms 29012 KB Output is correct
24 Correct 127 ms 29008 KB Output is correct
25 Correct 56 ms 14420 KB Output is correct
26 Correct 51 ms 14116 KB Output is correct
27 Correct 132 ms 29012 KB Output is correct
28 Correct 125 ms 29012 KB Output is correct
29 Correct 88 ms 14228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 31 ms 8808 KB Output is correct
5 Correct 119 ms 29012 KB Output is correct
6 Correct 120 ms 29008 KB Output is correct
7 Correct 163 ms 28876 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 30 ms 8724 KB Output is correct
10 Correct 137 ms 28964 KB Output is correct
11 Correct 120 ms 29012 KB Output is correct
12 Correct 120 ms 28876 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 32 ms 8784 KB Output is correct
17 Correct 123 ms 29096 KB Output is correct
18 Correct 125 ms 28956 KB Output is correct
19 Correct 119 ms 28880 KB Output is correct
20 Correct 122 ms 28120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 5 ms 2392 KB Output is correct
7 Correct 164 ms 29008 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 472 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 5 ms 2140 KB Output is correct
13 Correct 124 ms 28104 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1628 KB Output is correct
17 Correct 61 ms 14164 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 32 ms 8860 KB Output is correct
21 Correct 126 ms 29080 KB Output is correct
22 Correct 116 ms 28956 KB Output is correct
23 Correct 133 ms 28876 KB Output is correct
24 Correct 132 ms 28240 KB Output is correct
25 Correct 54 ms 14104 KB Output is correct
26 Correct 80 ms 14168 KB Output is correct
27 Correct 122 ms 28888 KB Output is correct
28 Correct 118 ms 28244 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 6 ms 2140 KB Output is correct
37 Correct 160 ms 28052 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 432 KB Output is correct
40 Correct 6 ms 2392 KB Output is correct
41 Correct 170 ms 29008 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 3 ms 1628 KB Output is correct
45 Correct 58 ms 14124 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 31 ms 8792 KB Output is correct
50 Correct 121 ms 29008 KB Output is correct
51 Correct 123 ms 29012 KB Output is correct
52 Correct 127 ms 29008 KB Output is correct
53 Correct 56 ms 14420 KB Output is correct
54 Correct 51 ms 14116 KB Output is correct
55 Correct 132 ms 29012 KB Output is correct
56 Correct 125 ms 29012 KB Output is correct
57 Correct 88 ms 14228 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 1628 KB Output is correct
61 Correct 58 ms 14164 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 32 ms 8828 KB Output is correct
66 Correct 82 ms 14124 KB Output is correct
67 Correct 69 ms 14232 KB Output is correct
68 Correct 51 ms 14160 KB Output is correct
69 Correct 50 ms 14160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 5 ms 2392 KB Output is correct
7 Correct 164 ms 29008 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 472 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 5 ms 2140 KB Output is correct
13 Correct 124 ms 28104 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1628 KB Output is correct
17 Correct 61 ms 14164 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 32 ms 8860 KB Output is correct
21 Correct 126 ms 29080 KB Output is correct
22 Correct 116 ms 28956 KB Output is correct
23 Correct 133 ms 28876 KB Output is correct
24 Correct 132 ms 28240 KB Output is correct
25 Correct 54 ms 14104 KB Output is correct
26 Correct 80 ms 14168 KB Output is correct
27 Correct 122 ms 28888 KB Output is correct
28 Correct 118 ms 28244 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 6 ms 2140 KB Output is correct
37 Correct 160 ms 28052 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 432 KB Output is correct
40 Correct 6 ms 2392 KB Output is correct
41 Correct 170 ms 29008 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 3 ms 1628 KB Output is correct
45 Correct 58 ms 14124 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 31 ms 8792 KB Output is correct
50 Correct 121 ms 29008 KB Output is correct
51 Correct 123 ms 29012 KB Output is correct
52 Correct 127 ms 29008 KB Output is correct
53 Correct 56 ms 14420 KB Output is correct
54 Correct 51 ms 14116 KB Output is correct
55 Correct 132 ms 29012 KB Output is correct
56 Correct 125 ms 29012 KB Output is correct
57 Correct 88 ms 14228 KB Output is correct
58 Correct 1 ms 600 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 31 ms 8808 KB Output is correct
62 Correct 119 ms 29012 KB Output is correct
63 Correct 120 ms 29008 KB Output is correct
64 Correct 163 ms 28876 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 30 ms 8724 KB Output is correct
67 Correct 137 ms 28964 KB Output is correct
68 Correct 120 ms 29012 KB Output is correct
69 Correct 120 ms 28876 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 32 ms 8784 KB Output is correct
74 Correct 123 ms 29096 KB Output is correct
75 Correct 125 ms 28956 KB Output is correct
76 Correct 119 ms 28880 KB Output is correct
77 Correct 122 ms 28120 KB Output is correct
78 Correct 1 ms 344 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 1628 KB Output is correct
81 Correct 58 ms 14164 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 32 ms 8828 KB Output is correct
86 Correct 82 ms 14124 KB Output is correct
87 Correct 69 ms 14232 KB Output is correct
88 Correct 51 ms 14160 KB Output is correct
89 Correct 50 ms 14160 KB Output is correct
90 Correct 1 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 3 ms 1628 KB Output is correct
93 Correct 74 ms 14104 KB Output is correct
94 Correct 0 ms 344 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 1 ms 472 KB Output is correct
97 Correct 24 ms 4836 KB Output is correct
98 Correct 53 ms 14164 KB Output is correct
99 Correct 52 ms 14240 KB Output is correct
100 Correct 49 ms 14168 KB Output is correct
101 Correct 93 ms 14160 KB Output is correct
102 Correct 48 ms 13992 KB Output is correct
103 Correct 51 ms 13992 KB Output is correct
104 Correct 62 ms 14164 KB Output is correct
105 Correct 61 ms 14116 KB Output is correct