Submission #550220

# Submission time Handle Problem Language Result Execution time Memory
550220 2022-04-17T15:28:57 Z jesus_coconut Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
253 ms 28044 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
using namespace std;

int n;
struct DSU {
	vector<int> par, rnk;

	DSU (int n) {
		par.resize(n);
		rnk.resize(n);
		iota(all(par), 0);
	}

	int fnd(int x) { return (par[x] == x ? x : (par[x] = fnd(par[x]))); }

	bool unite(int x, int y) {
		x = fnd(x), y = fnd(y);
		if (x == y) return false;
		if (rnk[x] < rnk[y]) swap(x, y);
		if (rnk[x] == rnk[y]) rnk[x]++;
		par[y] = x;
		return true;
	}
};

vector<vector<int>> b, comp, p;

void spoji(int x, int y) {
	b[x][y] = 1;
	b[y][x] = 1;
}

int solve(vector<int> const &v) {
	vector<vector<int>> lines;
	vector<int> bio(size(v));
	for (int i = 0; i < size(v); ++i) {
		if (!bio[i]) {
			bio[i] = 1;
			lines.emplace_back();
			lines.back().push_back(v[i]);
			for (int j = i + 1; j < size(v); ++j) {
				if (p[v[i]][v[j]] == 1) {
					bio[j] = 1;
					spoji(v[i], v[j]);
					lines.back().push_back(v[j]);
				}
			}
			for (int j = 0; j < size(lines.back()); ++j) {
				for (int k = j + 1; k < size(lines.back()); ++k) {
					if (p[lines.back()[j]][lines.back()[k]] != 1) return 0;
				}
			}
		}
	}
	if (lines.size() == 1) return 1;
	if (lines.size() == 2) return 0;
	for (int i = 0; i < size(lines); ++i) {
		for (auto ver : lines[i]) {
			for (int j = 0; j < size(lines); ++j) {
				if (i == j) continue;
				for (auto uer : lines[j]) {
					if (p[ver][uer] != 2) {
						return 0;
					}
				}
			}
		}
	}
	for (int i = 0; i < size(lines); ++i) {
		spoji(lines[i][0], lines[(i + 1) % size(lines)][0]);
	}
	return 1;
}

int construct(std::vector<std::vector<int>> p) {
	::p = p;
	n = size(p);
	b.resize(n, vector<int>(n));
	comp.resize(n);
	DSU subtree(n), cyc(n);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (p[i][j] == 3 || p[i][j] != p[j][i]) return 0;
			if (p[i][j] == 0) continue;
			if (i == j) {
				if (p[i][j] != 1) return 0;
				else continue;
			}
			subtree.unite(i, j);
			if (p[i][j] == 2) cyc.unite(i, j);
		}
	}
	for (int i = 0; i < n; ++i) comp[subtree.fnd(i)].push_back(i);
	for (int i = 0; i < n; ++i) {
		for (auto v : comp[i]) {
			for (int j = i + 1; j < n; ++j) {
				for (auto u : comp[j]) {
					if (p[v][u] != 0) return 0;
				}
			}
		}
	}
	for (auto &v : comp) {
		if (size(v) <= 1) continue;
		if (!solve(v)) return 0;
	}
	//
	build(b);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int solve(const std::vector<int>&)':
supertrees.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  for (int i = 0; i < size(v); ++i) {
      |                  ~~^~~~~~~~~
supertrees.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |    for (int j = i + 1; j < size(v); ++j) {
      |                        ~~^~~~~~~~~
supertrees.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |    for (int j = 0; j < size(lines.back()); ++j) {
      |                    ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:51:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int k = j + 1; k < size(lines.back()); ++k) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for (int i = 0; i < size(lines); ++i) {
      |                  ~~^~~~~~~~~~~~~
supertrees.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |    for (int j = 0; j < size(lines); ++j) {
      |                    ~~^~~~~~~~~~~~~
supertrees.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 0; i < size(lines); ++i) {
      |                  ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 300 KB Output is correct
6 Correct 9 ms 1336 KB Output is correct
7 Correct 206 ms 27888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 300 KB Output is correct
6 Correct 9 ms 1336 KB Output is correct
7 Correct 206 ms 27888 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1336 KB Output is correct
13 Correct 185 ms 28044 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 980 KB Output is correct
17 Correct 96 ms 18036 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 46 ms 7156 KB Output is correct
21 Correct 195 ms 27980 KB Output is correct
22 Correct 182 ms 27980 KB Output is correct
23 Correct 212 ms 27880 KB Output is correct
24 Correct 231 ms 28020 KB Output is correct
25 Correct 80 ms 18040 KB Output is correct
26 Correct 73 ms 18088 KB Output is correct
27 Correct 203 ms 27944 KB Output is correct
28 Correct 184 ms 28000 KB Output is correct
# Verdict Execution time Memory 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 8 ms 1404 KB Output is correct
9 Correct 191 ms 28020 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 10 ms 1492 KB Output is correct
13 Correct 212 ms 27900 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 5 ms 980 KB Output is correct
17 Correct 108 ms 18044 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 56 ms 7276 KB Output is correct
22 Correct 208 ms 27984 KB Output is correct
23 Correct 197 ms 27980 KB Output is correct
24 Correct 229 ms 28020 KB Output is correct
25 Correct 76 ms 17996 KB Output is correct
26 Correct 83 ms 18024 KB Output is correct
27 Correct 216 ms 27888 KB Output is correct
28 Correct 228 ms 27996 KB Output is correct
29 Correct 85 ms 18048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 62 ms 7248 KB Output is correct
5 Correct 216 ms 27884 KB Output is correct
6 Correct 221 ms 27948 KB Output is correct
7 Correct 237 ms 27892 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 52 ms 7208 KB Output is correct
10 Correct 208 ms 27948 KB Output is correct
11 Correct 222 ms 27992 KB Output is correct
12 Correct 242 ms 27932 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 59 ms 7284 KB Output is correct
17 Correct 253 ms 27896 KB Output is correct
18 Correct 200 ms 27948 KB Output is correct
19 Correct 204 ms 27900 KB Output is correct
20 Correct 197 ms 28032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 300 KB Output is correct
6 Correct 9 ms 1336 KB Output is correct
7 Correct 206 ms 27888 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1336 KB Output is correct
13 Correct 185 ms 28044 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 980 KB Output is correct
17 Correct 96 ms 18036 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 46 ms 7156 KB Output is correct
21 Correct 195 ms 27980 KB Output is correct
22 Correct 182 ms 27980 KB Output is correct
23 Correct 212 ms 27880 KB Output is correct
24 Correct 231 ms 28020 KB Output is correct
25 Correct 80 ms 18040 KB Output is correct
26 Correct 73 ms 18088 KB Output is correct
27 Correct 203 ms 27944 KB Output is correct
28 Correct 184 ms 28000 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 8 ms 1404 KB Output is correct
37 Correct 191 ms 28020 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 10 ms 1492 KB Output is correct
41 Correct 212 ms 27900 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 5 ms 980 KB Output is correct
45 Correct 108 ms 18044 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 56 ms 7276 KB Output is correct
50 Correct 208 ms 27984 KB Output is correct
51 Correct 197 ms 27980 KB Output is correct
52 Correct 229 ms 28020 KB Output is correct
53 Correct 76 ms 17996 KB Output is correct
54 Correct 83 ms 18024 KB Output is correct
55 Correct 216 ms 27888 KB Output is correct
56 Correct 228 ms 27996 KB Output is correct
57 Correct 85 ms 18048 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 8 ms 924 KB Output is correct
61 Correct 130 ms 18080 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 296 KB Output is correct
65 Correct 55 ms 7248 KB Output is correct
66 Correct 96 ms 18036 KB Output is correct
67 Correct 89 ms 18032 KB Output is correct
68 Correct 84 ms 17988 KB Output is correct
69 Correct 86 ms 18096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 300 KB Output is correct
6 Correct 9 ms 1336 KB Output is correct
7 Correct 206 ms 27888 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1336 KB Output is correct
13 Correct 185 ms 28044 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 980 KB Output is correct
17 Correct 96 ms 18036 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 46 ms 7156 KB Output is correct
21 Correct 195 ms 27980 KB Output is correct
22 Correct 182 ms 27980 KB Output is correct
23 Correct 212 ms 27880 KB Output is correct
24 Correct 231 ms 28020 KB Output is correct
25 Correct 80 ms 18040 KB Output is correct
26 Correct 73 ms 18088 KB Output is correct
27 Correct 203 ms 27944 KB Output is correct
28 Correct 184 ms 28000 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 8 ms 1404 KB Output is correct
37 Correct 191 ms 28020 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 10 ms 1492 KB Output is correct
41 Correct 212 ms 27900 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 5 ms 980 KB Output is correct
45 Correct 108 ms 18044 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 56 ms 7276 KB Output is correct
50 Correct 208 ms 27984 KB Output is correct
51 Correct 197 ms 27980 KB Output is correct
52 Correct 229 ms 28020 KB Output is correct
53 Correct 76 ms 17996 KB Output is correct
54 Correct 83 ms 18024 KB Output is correct
55 Correct 216 ms 27888 KB Output is correct
56 Correct 228 ms 27996 KB Output is correct
57 Correct 85 ms 18048 KB Output is correct
58 Correct 1 ms 292 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 300 KB Output is correct
61 Correct 62 ms 7248 KB Output is correct
62 Correct 216 ms 27884 KB Output is correct
63 Correct 221 ms 27948 KB Output is correct
64 Correct 237 ms 27892 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 52 ms 7208 KB Output is correct
67 Correct 208 ms 27948 KB Output is correct
68 Correct 222 ms 27992 KB Output is correct
69 Correct 242 ms 27932 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 296 KB Output is correct
73 Correct 59 ms 7284 KB Output is correct
74 Correct 253 ms 27896 KB Output is correct
75 Correct 200 ms 27948 KB Output is correct
76 Correct 204 ms 27900 KB Output is correct
77 Correct 197 ms 28032 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 8 ms 924 KB Output is correct
81 Correct 130 ms 18080 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 296 KB Output is correct
85 Correct 55 ms 7248 KB Output is correct
86 Correct 96 ms 18036 KB Output is correct
87 Correct 89 ms 18032 KB Output is correct
88 Correct 84 ms 17988 KB Output is correct
89 Correct 86 ms 18096 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 296 KB Output is correct
92 Correct 6 ms 980 KB Output is correct
93 Correct 98 ms 18040 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 19 ms 4732 KB Output is correct
98 Correct 78 ms 18036 KB Output is correct
99 Correct 81 ms 18048 KB Output is correct
100 Correct 74 ms 17980 KB Output is correct
101 Correct 73 ms 18036 KB Output is correct
102 Correct 86 ms 18036 KB Output is correct
103 Correct 90 ms 18040 KB Output is correct
104 Correct 94 ms 18028 KB Output is correct
105 Correct 87 ms 18040 KB Output is correct