#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> dsu, sz;
DSU (int n) {
dsu.resize(n);
iota(dsu.begin(), dsu.end(), 0);
sz.resize(n, 1);
}
int find (int u) {
return u == dsu[u] ? u : dsu[u] = find(dsu[u]);
}
bool unite (int u, int v) {
if ((u = find(u)) != (v = find(v))) {
if (sz[u] < sz[v]) {
swap(u, v);
}
dsu[v] = u;
sz[u] += sz[v];
return 1;
}
return 0;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; ++i) {
if (count(p[i].begin(), p[i].end(), 3)) {
return 0;
}
}
vector<vector<int>> ans(n, vector<int>(n));
DSU nodes(n);
bool two = 0;
auto sit = [&] (int i, int j, int t) {
ans[i][j] = ans[j][i] = t;
};
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] == 1) {
sit(i, j, nodes.unite(i, j));
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (p[i][j] == 0 && nodes.find(i) == nodes.find(j)) {
return 0;
}
}
}
vector<vector<int>> c(n);
for (int i = 0; i < n; ++i) {
c[nodes.find(i)].push_back(i);
}
DSU cmp(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (p[i][j] == 2) {
cmp.unite(nodes.find(i), nodes.find(j));
}
}
}
vector<vector<int>> C(n);
for (int i = 0; i < n; ++i) {
C[cmp.find(i)].push_back(i);
}
for (int i = 0; i < n; ++i) {
if (i != cmp.find(i)) {
continue;
}
for (int j = 0; j < n; ++j) {
if (nodes.find(i) != nodes.find(j) && cmp.find(i) == cmp.find(j) && p[i][j] != 2) {
return 0;
}
}
for (int j : c[i]) {
for (int k = 0; k < n; ++k) {
if ((p[k][i] == 2) ^ (p[j][k] == 2)) {
return 0;
}
}
}
for (int j = 0; j < C[i].size(); ++j) {
for (int k = j + 1; k < C[i].size(); ++k) {
if (p[C[i][j]][C[i][k]] ^ 2) {
return 0;
}
}
}
if (C[i].size() == 2) {
return 0;
}
if (C[i].size() > 2) {
sit(C[i][0], C[i][1], 1);
sit(C[i][1], C[i][2], 1);
for (int j = 3; j < C[i].size(); ++j) {
sit(C[i][j], C[i][j - 1], 1);
}
sit(C[i].back(), C[i][0], 1);
}
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int j = 0; j < C[i].size(); ++j) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:92:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int k = j + 1; k < C[i].size(); ++k) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:104:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int j = 3; j < C[i].size(); ++j) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:41:8: warning: unused variable 'two' [-Wunused-variable]
41 | bool two = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1228 KB |
Output is correct |
7 |
Correct |
234 ms |
24080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1228 KB |
Output is correct |
7 |
Correct |
234 ms |
24080 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1172 KB |
Output is correct |
13 |
Correct |
228 ms |
24084 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
784 KB |
Output is correct |
17 |
Correct |
102 ms |
14072 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
6256 KB |
Output is correct |
21 |
Correct |
240 ms |
24032 KB |
Output is correct |
22 |
Correct |
242 ms |
24004 KB |
Output is correct |
23 |
Correct |
254 ms |
24056 KB |
Output is correct |
24 |
Correct |
238 ms |
24132 KB |
Output is correct |
25 |
Correct |
93 ms |
14060 KB |
Output is correct |
26 |
Correct |
91 ms |
14068 KB |
Output is correct |
27 |
Correct |
246 ms |
24180 KB |
Output is correct |
28 |
Correct |
229 ms |
24052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
10 ms |
1228 KB |
Output is correct |
9 |
Correct |
227 ms |
24040 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
10 ms |
1228 KB |
Output is correct |
13 |
Correct |
227 ms |
24052 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
122 ms |
14072 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
292 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
57 ms |
6224 KB |
Output is correct |
22 |
Correct |
225 ms |
24004 KB |
Output is correct |
23 |
Correct |
226 ms |
24004 KB |
Output is correct |
24 |
Correct |
237 ms |
24008 KB |
Output is correct |
25 |
Correct |
98 ms |
14184 KB |
Output is correct |
26 |
Correct |
101 ms |
14076 KB |
Output is correct |
27 |
Correct |
226 ms |
24052 KB |
Output is correct |
28 |
Correct |
237 ms |
24004 KB |
Output is correct |
29 |
Correct |
106 ms |
14148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
60 ms |
6208 KB |
Output is correct |
5 |
Correct |
234 ms |
24004 KB |
Output is correct |
6 |
Correct |
233 ms |
24004 KB |
Output is correct |
7 |
Correct |
247 ms |
24072 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
57 ms |
6244 KB |
Output is correct |
10 |
Correct |
226 ms |
24056 KB |
Output is correct |
11 |
Correct |
224 ms |
24004 KB |
Output is correct |
12 |
Correct |
241 ms |
24060 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
58 ms |
6248 KB |
Output is correct |
17 |
Correct |
232 ms |
24140 KB |
Output is correct |
18 |
Correct |
234 ms |
24004 KB |
Output is correct |
19 |
Correct |
236 ms |
24036 KB |
Output is correct |
20 |
Correct |
233 ms |
24028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1228 KB |
Output is correct |
7 |
Correct |
234 ms |
24080 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1172 KB |
Output is correct |
13 |
Correct |
228 ms |
24084 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
784 KB |
Output is correct |
17 |
Correct |
102 ms |
14072 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
6256 KB |
Output is correct |
21 |
Correct |
240 ms |
24032 KB |
Output is correct |
22 |
Correct |
242 ms |
24004 KB |
Output is correct |
23 |
Correct |
254 ms |
24056 KB |
Output is correct |
24 |
Correct |
238 ms |
24132 KB |
Output is correct |
25 |
Correct |
93 ms |
14060 KB |
Output is correct |
26 |
Correct |
91 ms |
14068 KB |
Output is correct |
27 |
Correct |
246 ms |
24180 KB |
Output is correct |
28 |
Correct |
229 ms |
24052 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
2 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1228 KB |
Output is correct |
37 |
Correct |
227 ms |
24040 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
292 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
227 ms |
24052 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
122 ms |
14072 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
292 KB |
Output is correct |
48 |
Correct |
1 ms |
292 KB |
Output is correct |
49 |
Correct |
57 ms |
6224 KB |
Output is correct |
50 |
Correct |
225 ms |
24004 KB |
Output is correct |
51 |
Correct |
226 ms |
24004 KB |
Output is correct |
52 |
Correct |
237 ms |
24008 KB |
Output is correct |
53 |
Correct |
98 ms |
14184 KB |
Output is correct |
54 |
Correct |
101 ms |
14076 KB |
Output is correct |
55 |
Correct |
226 ms |
24052 KB |
Output is correct |
56 |
Correct |
237 ms |
24004 KB |
Output is correct |
57 |
Correct |
106 ms |
14148 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
5 ms |
844 KB |
Output is correct |
61 |
Correct |
104 ms |
14020 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
58 ms |
6256 KB |
Output is correct |
66 |
Correct |
100 ms |
14072 KB |
Output is correct |
67 |
Correct |
93 ms |
14060 KB |
Output is correct |
68 |
Correct |
100 ms |
14188 KB |
Output is correct |
69 |
Correct |
95 ms |
14192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1228 KB |
Output is correct |
7 |
Correct |
234 ms |
24080 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1172 KB |
Output is correct |
13 |
Correct |
228 ms |
24084 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
784 KB |
Output is correct |
17 |
Correct |
102 ms |
14072 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
6256 KB |
Output is correct |
21 |
Correct |
240 ms |
24032 KB |
Output is correct |
22 |
Correct |
242 ms |
24004 KB |
Output is correct |
23 |
Correct |
254 ms |
24056 KB |
Output is correct |
24 |
Correct |
238 ms |
24132 KB |
Output is correct |
25 |
Correct |
93 ms |
14060 KB |
Output is correct |
26 |
Correct |
91 ms |
14068 KB |
Output is correct |
27 |
Correct |
246 ms |
24180 KB |
Output is correct |
28 |
Correct |
229 ms |
24052 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
2 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1228 KB |
Output is correct |
37 |
Correct |
227 ms |
24040 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
292 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
227 ms |
24052 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
122 ms |
14072 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
292 KB |
Output is correct |
48 |
Correct |
1 ms |
292 KB |
Output is correct |
49 |
Correct |
57 ms |
6224 KB |
Output is correct |
50 |
Correct |
225 ms |
24004 KB |
Output is correct |
51 |
Correct |
226 ms |
24004 KB |
Output is correct |
52 |
Correct |
237 ms |
24008 KB |
Output is correct |
53 |
Correct |
98 ms |
14184 KB |
Output is correct |
54 |
Correct |
101 ms |
14076 KB |
Output is correct |
55 |
Correct |
226 ms |
24052 KB |
Output is correct |
56 |
Correct |
237 ms |
24004 KB |
Output is correct |
57 |
Correct |
106 ms |
14148 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
60 ms |
6208 KB |
Output is correct |
62 |
Correct |
234 ms |
24004 KB |
Output is correct |
63 |
Correct |
233 ms |
24004 KB |
Output is correct |
64 |
Correct |
247 ms |
24072 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
57 ms |
6244 KB |
Output is correct |
67 |
Correct |
226 ms |
24056 KB |
Output is correct |
68 |
Correct |
224 ms |
24004 KB |
Output is correct |
69 |
Correct |
241 ms |
24060 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
58 ms |
6248 KB |
Output is correct |
74 |
Correct |
232 ms |
24140 KB |
Output is correct |
75 |
Correct |
234 ms |
24004 KB |
Output is correct |
76 |
Correct |
236 ms |
24036 KB |
Output is correct |
77 |
Correct |
233 ms |
24028 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
5 ms |
844 KB |
Output is correct |
81 |
Correct |
104 ms |
14020 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
58 ms |
6256 KB |
Output is correct |
86 |
Correct |
100 ms |
14072 KB |
Output is correct |
87 |
Correct |
93 ms |
14060 KB |
Output is correct |
88 |
Correct |
100 ms |
14188 KB |
Output is correct |
89 |
Correct |
95 ms |
14192 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
5 ms |
656 KB |
Output is correct |
93 |
Correct |
94 ms |
10100 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
1 ms |
292 KB |
Output is correct |
97 |
Correct |
23 ms |
2664 KB |
Output is correct |
98 |
Correct |
88 ms |
10052 KB |
Output is correct |
99 |
Correct |
90 ms |
10092 KB |
Output is correct |
100 |
Correct |
88 ms |
10104 KB |
Output is correct |
101 |
Correct |
87 ms |
10052 KB |
Output is correct |
102 |
Correct |
90 ms |
10108 KB |
Output is correct |
103 |
Correct |
88 ms |
10092 KB |
Output is correct |
104 |
Correct |
86 ms |
10052 KB |
Output is correct |
105 |
Correct |
95 ms |
10104 KB |
Output is correct |