#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
using int64 = long long;
using ld = long double;
constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
class dsu {
int n;
std::vector<int> e;
int pfind(const int x) {
return (e[x] < 0 ? x : pfind(e[x]));
}
public:
dsu() : n(0), comp(0) {}
explicit dsu(const int n) : n(n), comp(n), e(n, -1) {}
int comp;
int find(const int x) {
assert(0 <= x and x < n);
return pfind(x);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
comp--;
return true;
}
bool same(const int x, const int y) {
return (find(x) == find(y));
}
int size(const int x) {
return -e[find(x)];
}
std::vector<std::vector<int>> components() {
std::vector<std::vector<int>> res(n);
for (int x = 0; x < n; x++) {
res[pfind(x)].push_back(x);
}
res.erase(
std::remove_if(res.begin(), res.end(), [&](const std::vector<int> &v) { return v.empty(); }),
res.end());
return res;
}
};
vector<vector<int>> ways;
vector<vector<int>> ans;
void connect(const int x, const int y) {
ans[x][y] = 1;
ans[y][x] = 1;
}
bool connect1(const vector<int> &nodes) {
if (nodes.size() < 2) return false;
for (int i = 0; i < nodes.size() - 1; i++) {
connect(nodes[i], nodes[i + 1]);
}
return true;
}
bool connect2(const vector<int> &nodes) {
if (nodes.size() < 3) return false;
connect1(nodes);
connect(nodes.front(), nodes.back());
return true;
}
bool solve(const vector<int> &nodes) {
const int n = (int) nodes.size();
dsu d(n);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
const int way = ways[nodes[i]][nodes[j]];
if (way == 1) {
d.unite(i, j);
} else if (way == 3) {
return false;
}
}
}
const vector<vector<int>> lines = d.components();
for (auto line : lines) {
for (int i = 0; i < line.size(); i++) {
for (int j = i + 1; j < line.size(); j++) {
const int x = nodes[line[i]];
const int y = nodes[line[j]];
if (ways[x][y] != 1) return false;
}
}
for (int &i : line) {
i = nodes[i];
}
connect1(line);
}
if (lines.size() == 1) return true;
vector<int> cycle;
for (const auto &line : lines) {
cycle.push_back(nodes[line[0]]);
}
return connect2(cycle);
}
int construct(vector<std::vector<int>> p) {
const int n = (int) p.size();
ways = p;
dsu d(n);
vector<pair<int, int>> disjoint;
for (int x = 0; x < n; x++) {
for (int y = x + 1; y < n; y++) {
if (p[x][y] == 0) {
disjoint.emplace_back(x, y);
} else {
d.unite(x, y);
}
}
}
for (const auto &[x, y] : disjoint) {
if (d.same(x, y)) return false;
}
ans.assign(n, vector<int>(n));
const auto components = d.components();
for (const auto &component : components) {
if (not solve(component)) return false;
}
build(ans);
return true;
}
Compilation message
supertrees.cpp: In constructor 'dsu::dsu(int)':
supertrees.cpp:29:7: warning: 'dsu::comp' will be initialized after [-Wreorder]
29 | int comp;
| ^~~~
supertrees.cpp:19:20: warning: 'std::vector<int> dsu::e' [-Wreorder]
19 | std::vector<int> e;
| ^
supertrees.cpp:27:12: warning: when initialized here [-Wreorder]
27 | explicit dsu(const int n) : n(n), comp(n), e(n, -1) {}
| ^~~
supertrees.cpp: In function 'bool connect1(const std::vector<int>&)':
supertrees.cpp:76:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 0; i < nodes.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp: In function 'bool solve(const std::vector<int>&)':
supertrees.cpp:106:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int i = 0; i < line.size(); i++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:107:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int j = i + 1; j < line.size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1356 KB |
Output is correct |
7 |
Correct |
189 ms |
25924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1356 KB |
Output is correct |
7 |
Correct |
189 ms |
25924 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1368 KB |
Output is correct |
13 |
Correct |
175 ms |
27896 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
980 KB |
Output is correct |
17 |
Correct |
86 ms |
14332 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
44 ms |
7244 KB |
Output is correct |
21 |
Correct |
176 ms |
27536 KB |
Output is correct |
22 |
Correct |
177 ms |
27900 KB |
Output is correct |
23 |
Correct |
188 ms |
25976 KB |
Output is correct |
24 |
Correct |
168 ms |
27908 KB |
Output is correct |
25 |
Correct |
74 ms |
16392 KB |
Output is correct |
26 |
Correct |
78 ms |
16392 KB |
Output is correct |
27 |
Correct |
190 ms |
25984 KB |
Output is correct |
28 |
Correct |
169 ms |
27968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
1512 KB |
Output is correct |
9 |
Correct |
168 ms |
27908 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1364 KB |
Output is correct |
13 |
Correct |
181 ms |
25988 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
968 KB |
Output is correct |
17 |
Correct |
86 ms |
14220 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
45 ms |
7176 KB |
Output is correct |
22 |
Correct |
176 ms |
27528 KB |
Output is correct |
23 |
Correct |
179 ms |
27900 KB |
Output is correct |
24 |
Correct |
188 ms |
25980 KB |
Output is correct |
25 |
Correct |
76 ms |
20032 KB |
Output is correct |
26 |
Correct |
77 ms |
16396 KB |
Output is correct |
27 |
Correct |
181 ms |
27896 KB |
Output is correct |
28 |
Correct |
191 ms |
26008 KB |
Output is correct |
29 |
Correct |
77 ms |
19964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
47 ms |
7176 KB |
Output is correct |
5 |
Correct |
182 ms |
27512 KB |
Output is correct |
6 |
Correct |
176 ms |
27828 KB |
Output is correct |
7 |
Correct |
186 ms |
26012 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
47 ms |
7176 KB |
Output is correct |
10 |
Correct |
182 ms |
27508 KB |
Output is correct |
11 |
Correct |
190 ms |
27840 KB |
Output is correct |
12 |
Correct |
181 ms |
25976 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
43 ms |
7172 KB |
Output is correct |
17 |
Correct |
175 ms |
27512 KB |
Output is correct |
18 |
Correct |
175 ms |
27520 KB |
Output is correct |
19 |
Correct |
178 ms |
27840 KB |
Output is correct |
20 |
Correct |
171 ms |
27968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1356 KB |
Output is correct |
7 |
Correct |
189 ms |
25924 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1368 KB |
Output is correct |
13 |
Correct |
175 ms |
27896 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
980 KB |
Output is correct |
17 |
Correct |
86 ms |
14332 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
44 ms |
7244 KB |
Output is correct |
21 |
Correct |
176 ms |
27536 KB |
Output is correct |
22 |
Correct |
177 ms |
27900 KB |
Output is correct |
23 |
Correct |
188 ms |
25976 KB |
Output is correct |
24 |
Correct |
168 ms |
27908 KB |
Output is correct |
25 |
Correct |
74 ms |
16392 KB |
Output is correct |
26 |
Correct |
78 ms |
16392 KB |
Output is correct |
27 |
Correct |
190 ms |
25984 KB |
Output is correct |
28 |
Correct |
169 ms |
27968 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
224 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
8 ms |
1512 KB |
Output is correct |
37 |
Correct |
168 ms |
27908 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1364 KB |
Output is correct |
41 |
Correct |
181 ms |
25988 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
968 KB |
Output is correct |
45 |
Correct |
86 ms |
14220 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
45 ms |
7176 KB |
Output is correct |
50 |
Correct |
176 ms |
27528 KB |
Output is correct |
51 |
Correct |
179 ms |
27900 KB |
Output is correct |
52 |
Correct |
188 ms |
25980 KB |
Output is correct |
53 |
Correct |
76 ms |
20032 KB |
Output is correct |
54 |
Correct |
77 ms |
16396 KB |
Output is correct |
55 |
Correct |
181 ms |
27896 KB |
Output is correct |
56 |
Correct |
191 ms |
26008 KB |
Output is correct |
57 |
Correct |
77 ms |
19964 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
852 KB |
Output is correct |
61 |
Correct |
89 ms |
14336 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
44 ms |
7172 KB |
Output is correct |
66 |
Correct |
75 ms |
16264 KB |
Output is correct |
67 |
Correct |
78 ms |
16380 KB |
Output is correct |
68 |
Correct |
72 ms |
16380 KB |
Output is correct |
69 |
Correct |
76 ms |
19960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1356 KB |
Output is correct |
7 |
Correct |
189 ms |
25924 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1368 KB |
Output is correct |
13 |
Correct |
175 ms |
27896 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
980 KB |
Output is correct |
17 |
Correct |
86 ms |
14332 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
44 ms |
7244 KB |
Output is correct |
21 |
Correct |
176 ms |
27536 KB |
Output is correct |
22 |
Correct |
177 ms |
27900 KB |
Output is correct |
23 |
Correct |
188 ms |
25976 KB |
Output is correct |
24 |
Correct |
168 ms |
27908 KB |
Output is correct |
25 |
Correct |
74 ms |
16392 KB |
Output is correct |
26 |
Correct |
78 ms |
16392 KB |
Output is correct |
27 |
Correct |
190 ms |
25984 KB |
Output is correct |
28 |
Correct |
169 ms |
27968 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
224 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
8 ms |
1512 KB |
Output is correct |
37 |
Correct |
168 ms |
27908 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1364 KB |
Output is correct |
41 |
Correct |
181 ms |
25988 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
968 KB |
Output is correct |
45 |
Correct |
86 ms |
14220 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
45 ms |
7176 KB |
Output is correct |
50 |
Correct |
176 ms |
27528 KB |
Output is correct |
51 |
Correct |
179 ms |
27900 KB |
Output is correct |
52 |
Correct |
188 ms |
25980 KB |
Output is correct |
53 |
Correct |
76 ms |
20032 KB |
Output is correct |
54 |
Correct |
77 ms |
16396 KB |
Output is correct |
55 |
Correct |
181 ms |
27896 KB |
Output is correct |
56 |
Correct |
191 ms |
26008 KB |
Output is correct |
57 |
Correct |
77 ms |
19964 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 |
47 ms |
7176 KB |
Output is correct |
62 |
Correct |
182 ms |
27512 KB |
Output is correct |
63 |
Correct |
176 ms |
27828 KB |
Output is correct |
64 |
Correct |
186 ms |
26012 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
47 ms |
7176 KB |
Output is correct |
67 |
Correct |
182 ms |
27508 KB |
Output is correct |
68 |
Correct |
190 ms |
27840 KB |
Output is correct |
69 |
Correct |
181 ms |
25976 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
43 ms |
7172 KB |
Output is correct |
74 |
Correct |
175 ms |
27512 KB |
Output is correct |
75 |
Correct |
175 ms |
27520 KB |
Output is correct |
76 |
Correct |
178 ms |
27840 KB |
Output is correct |
77 |
Correct |
171 ms |
27968 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
852 KB |
Output is correct |
81 |
Correct |
89 ms |
14336 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
44 ms |
7172 KB |
Output is correct |
86 |
Correct |
75 ms |
16264 KB |
Output is correct |
87 |
Correct |
78 ms |
16380 KB |
Output is correct |
88 |
Correct |
72 ms |
16380 KB |
Output is correct |
89 |
Correct |
76 ms |
19960 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
852 KB |
Output is correct |
93 |
Correct |
84 ms |
13188 KB |
Output is correct |
94 |
Correct |
0 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
18 ms |
4360 KB |
Output is correct |
98 |
Correct |
74 ms |
16384 KB |
Output is correct |
99 |
Correct |
76 ms |
16392 KB |
Output is correct |
100 |
Correct |
79 ms |
16324 KB |
Output is correct |
101 |
Correct |
74 ms |
19964 KB |
Output is correct |
102 |
Correct |
81 ms |
16016 KB |
Output is correct |
103 |
Correct |
82 ms |
16004 KB |
Output is correct |
104 |
Correct |
82 ms |
16012 KB |
Output is correct |
105 |
Correct |
87 ms |
14216 KB |
Output is correct |