#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 &x : line) {
x = nodes[x];
}
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++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
1244 KB |
Output is correct |
7 |
Correct |
185 ms |
26004 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1244 KB |
Output is correct |
7 |
Correct |
185 ms |
26004 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
1368 KB |
Output is correct |
13 |
Correct |
184 ms |
27912 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 |
87 ms |
14280 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
45 ms |
7236 KB |
Output is correct |
21 |
Correct |
180 ms |
27512 KB |
Output is correct |
22 |
Correct |
189 ms |
27892 KB |
Output is correct |
23 |
Correct |
186 ms |
25988 KB |
Output is correct |
24 |
Correct |
172 ms |
27908 KB |
Output is correct |
25 |
Correct |
75 ms |
16376 KB |
Output is correct |
26 |
Correct |
73 ms |
16452 KB |
Output is correct |
27 |
Correct |
185 ms |
25992 KB |
Output is correct |
28 |
Correct |
176 ms |
27900 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 |
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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
1368 KB |
Output is correct |
9 |
Correct |
170 ms |
27900 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 |
1236 KB |
Output is correct |
13 |
Correct |
181 ms |
25932 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 |
90 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 |
43 ms |
7244 KB |
Output is correct |
22 |
Correct |
178 ms |
27516 KB |
Output is correct |
23 |
Correct |
195 ms |
27936 KB |
Output is correct |
24 |
Correct |
185 ms |
25976 KB |
Output is correct |
25 |
Correct |
83 ms |
19960 KB |
Output is correct |
26 |
Correct |
77 ms |
16296 KB |
Output is correct |
27 |
Correct |
175 ms |
27904 KB |
Output is correct |
28 |
Correct |
189 ms |
26056 KB |
Output is correct |
29 |
Correct |
77 ms |
20032 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 |
44 ms |
7176 KB |
Output is correct |
5 |
Correct |
186 ms |
27508 KB |
Output is correct |
6 |
Correct |
170 ms |
27896 KB |
Output is correct |
7 |
Correct |
192 ms |
26136 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
47 ms |
7236 KB |
Output is correct |
10 |
Correct |
178 ms |
27512 KB |
Output is correct |
11 |
Correct |
173 ms |
27840 KB |
Output is correct |
12 |
Correct |
179 ms |
25992 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
46 ms |
7168 KB |
Output is correct |
17 |
Correct |
172 ms |
27504 KB |
Output is correct |
18 |
Correct |
184 ms |
27524 KB |
Output is correct |
19 |
Correct |
174 ms |
27764 KB |
Output is correct |
20 |
Correct |
175 ms |
27968 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1244 KB |
Output is correct |
7 |
Correct |
185 ms |
26004 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
1368 KB |
Output is correct |
13 |
Correct |
184 ms |
27912 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 |
87 ms |
14280 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
45 ms |
7236 KB |
Output is correct |
21 |
Correct |
180 ms |
27512 KB |
Output is correct |
22 |
Correct |
189 ms |
27892 KB |
Output is correct |
23 |
Correct |
186 ms |
25988 KB |
Output is correct |
24 |
Correct |
172 ms |
27908 KB |
Output is correct |
25 |
Correct |
75 ms |
16376 KB |
Output is correct |
26 |
Correct |
73 ms |
16452 KB |
Output is correct |
27 |
Correct |
185 ms |
25992 KB |
Output is correct |
28 |
Correct |
176 ms |
27900 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 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 |
1368 KB |
Output is correct |
37 |
Correct |
170 ms |
27900 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 |
1236 KB |
Output is correct |
41 |
Correct |
181 ms |
25932 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 |
980 KB |
Output is correct |
45 |
Correct |
90 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 |
43 ms |
7244 KB |
Output is correct |
50 |
Correct |
178 ms |
27516 KB |
Output is correct |
51 |
Correct |
195 ms |
27936 KB |
Output is correct |
52 |
Correct |
185 ms |
25976 KB |
Output is correct |
53 |
Correct |
83 ms |
19960 KB |
Output is correct |
54 |
Correct |
77 ms |
16296 KB |
Output is correct |
55 |
Correct |
175 ms |
27904 KB |
Output is correct |
56 |
Correct |
189 ms |
26056 KB |
Output is correct |
57 |
Correct |
77 ms |
20032 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 |
86 ms |
14220 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 |
51 ms |
7172 KB |
Output is correct |
66 |
Correct |
75 ms |
16380 KB |
Output is correct |
67 |
Correct |
77 ms |
16452 KB |
Output is correct |
68 |
Correct |
72 ms |
16392 KB |
Output is correct |
69 |
Correct |
74 ms |
19960 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1244 KB |
Output is correct |
7 |
Correct |
185 ms |
26004 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
1368 KB |
Output is correct |
13 |
Correct |
184 ms |
27912 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 |
87 ms |
14280 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
45 ms |
7236 KB |
Output is correct |
21 |
Correct |
180 ms |
27512 KB |
Output is correct |
22 |
Correct |
189 ms |
27892 KB |
Output is correct |
23 |
Correct |
186 ms |
25988 KB |
Output is correct |
24 |
Correct |
172 ms |
27908 KB |
Output is correct |
25 |
Correct |
75 ms |
16376 KB |
Output is correct |
26 |
Correct |
73 ms |
16452 KB |
Output is correct |
27 |
Correct |
185 ms |
25992 KB |
Output is correct |
28 |
Correct |
176 ms |
27900 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 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 |
1368 KB |
Output is correct |
37 |
Correct |
170 ms |
27900 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 |
1236 KB |
Output is correct |
41 |
Correct |
181 ms |
25932 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 |
980 KB |
Output is correct |
45 |
Correct |
90 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 |
43 ms |
7244 KB |
Output is correct |
50 |
Correct |
178 ms |
27516 KB |
Output is correct |
51 |
Correct |
195 ms |
27936 KB |
Output is correct |
52 |
Correct |
185 ms |
25976 KB |
Output is correct |
53 |
Correct |
83 ms |
19960 KB |
Output is correct |
54 |
Correct |
77 ms |
16296 KB |
Output is correct |
55 |
Correct |
175 ms |
27904 KB |
Output is correct |
56 |
Correct |
189 ms |
26056 KB |
Output is correct |
57 |
Correct |
77 ms |
20032 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 |
44 ms |
7176 KB |
Output is correct |
62 |
Correct |
186 ms |
27508 KB |
Output is correct |
63 |
Correct |
170 ms |
27896 KB |
Output is correct |
64 |
Correct |
192 ms |
26136 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
47 ms |
7236 KB |
Output is correct |
67 |
Correct |
178 ms |
27512 KB |
Output is correct |
68 |
Correct |
173 ms |
27840 KB |
Output is correct |
69 |
Correct |
179 ms |
25992 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
46 ms |
7168 KB |
Output is correct |
74 |
Correct |
172 ms |
27504 KB |
Output is correct |
75 |
Correct |
184 ms |
27524 KB |
Output is correct |
76 |
Correct |
174 ms |
27764 KB |
Output is correct |
77 |
Correct |
175 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 |
86 ms |
14220 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 |
51 ms |
7172 KB |
Output is correct |
86 |
Correct |
75 ms |
16380 KB |
Output is correct |
87 |
Correct |
77 ms |
16452 KB |
Output is correct |
88 |
Correct |
72 ms |
16392 KB |
Output is correct |
89 |
Correct |
74 ms |
19960 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
85 ms |
13188 KB |
Output is correct |
94 |
Correct |
1 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 |
4360 KB |
Output is correct |
98 |
Correct |
76 ms |
16392 KB |
Output is correct |
99 |
Correct |
78 ms |
16400 KB |
Output is correct |
100 |
Correct |
83 ms |
16384 KB |
Output is correct |
101 |
Correct |
74 ms |
19956 KB |
Output is correct |
102 |
Correct |
81 ms |
16008 KB |
Output is correct |
103 |
Correct |
81 ms |
16072 KB |
Output is correct |
104 |
Correct |
81 ms |
16000 KB |
Output is correct |
105 |
Correct |
89 ms |
14336 KB |
Output is correct |