#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> answer;
void connect (int u, int v) { answer[u][v] = answer[v][u] = 1; }
int p[2000];
int find (int u) { return u==p[u]? u:p[u]=find(p[u]); }
bool merge (int i, int j) {
if (find(i) == find(j)) return false;
p[find(i)] = find(j);
connect(i, j);
return true;
}
vector<int> c;
vector<vector<int>> r;
int n;
void relax(int i) {
for (int j = i+1; j < n; j++) {
if (r[i][j] != 0) c.push_back(j);
}
c.push_back(i);
for (auto x : c) {
for (auto y : c) {
r[x][y] = r[y][x] = 0;
}
}
}
int construct(vector<vector<int>> req) {
r = req; n = req.size();
for (int i = 0; i < n; i++) {
vector<int> row = vector<int>(n);
answer.push_back(row);
}
iota(p, p+n+1, 0);
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (req[i][j] == 1) merge(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (r[i][j] == 2) {
c.clear();
relax(i);
int first = -1, last = 0;
for (int x = 1; x < c.size(); x++) {
if (merge(c[last], c[x])) {
if (first == -1) first = last;
last = x;
}
}
if (first == -1 or c.size() <= 2) return 0;
connect(c[last], c[first]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (req[i][j] == 0 and find(i) == find(j)) return 0;
if (req[i][j] == 3) return 0;
}
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:52:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int x = 1; x < c.size(); x++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1352 KB |
Output is correct |
7 |
Correct |
250 ms |
26000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1352 KB |
Output is correct |
7 |
Correct |
250 ms |
26000 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1356 KB |
Output is correct |
13 |
Correct |
254 ms |
26004 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 |
121 ms |
16068 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
62 ms |
6660 KB |
Output is correct |
21 |
Correct |
282 ms |
25964 KB |
Output is correct |
22 |
Correct |
250 ms |
25916 KB |
Output is correct |
23 |
Correct |
263 ms |
25924 KB |
Output is correct |
24 |
Correct |
249 ms |
26060 KB |
Output is correct |
25 |
Correct |
120 ms |
16016 KB |
Output is correct |
26 |
Correct |
106 ms |
16012 KB |
Output is correct |
27 |
Correct |
265 ms |
25968 KB |
Output is correct |
28 |
Correct |
242 ms |
25924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
11 ms |
1352 KB |
Output is correct |
9 |
Correct |
242 ms |
26012 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1336 KB |
Output is correct |
13 |
Correct |
250 ms |
25924 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
7 ms |
844 KB |
Output is correct |
17 |
Correct |
122 ms |
16020 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
63 ms |
6664 KB |
Output is correct |
22 |
Correct |
252 ms |
26000 KB |
Output is correct |
23 |
Correct |
253 ms |
25940 KB |
Output is correct |
24 |
Correct |
268 ms |
25996 KB |
Output is correct |
25 |
Correct |
105 ms |
16068 KB |
Output is correct |
26 |
Correct |
106 ms |
16004 KB |
Output is correct |
27 |
Correct |
249 ms |
25996 KB |
Output is correct |
28 |
Correct |
261 ms |
26020 KB |
Output is correct |
29 |
Correct |
107 ms |
16068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
63 ms |
6760 KB |
Output is correct |
5 |
Correct |
260 ms |
25988 KB |
Output is correct |
6 |
Correct |
250 ms |
26000 KB |
Output is correct |
7 |
Correct |
263 ms |
26000 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
66 ms |
6656 KB |
Output is correct |
10 |
Correct |
255 ms |
25984 KB |
Output is correct |
11 |
Correct |
254 ms |
26000 KB |
Output is correct |
12 |
Correct |
277 ms |
25984 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 |
65 ms |
6804 KB |
Output is correct |
17 |
Correct |
248 ms |
26000 KB |
Output is correct |
18 |
Correct |
252 ms |
26052 KB |
Output is correct |
19 |
Correct |
249 ms |
25964 KB |
Output is correct |
20 |
Correct |
243 ms |
26004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1352 KB |
Output is correct |
7 |
Correct |
250 ms |
26000 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1356 KB |
Output is correct |
13 |
Correct |
254 ms |
26004 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 |
121 ms |
16068 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
62 ms |
6660 KB |
Output is correct |
21 |
Correct |
282 ms |
25964 KB |
Output is correct |
22 |
Correct |
250 ms |
25916 KB |
Output is correct |
23 |
Correct |
263 ms |
25924 KB |
Output is correct |
24 |
Correct |
249 ms |
26060 KB |
Output is correct |
25 |
Correct |
120 ms |
16016 KB |
Output is correct |
26 |
Correct |
106 ms |
16012 KB |
Output is correct |
27 |
Correct |
265 ms |
25968 KB |
Output is correct |
28 |
Correct |
242 ms |
25924 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 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 |
11 ms |
1352 KB |
Output is correct |
37 |
Correct |
242 ms |
26012 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1336 KB |
Output is correct |
41 |
Correct |
250 ms |
25924 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
7 ms |
844 KB |
Output is correct |
45 |
Correct |
122 ms |
16020 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
63 ms |
6664 KB |
Output is correct |
50 |
Correct |
252 ms |
26000 KB |
Output is correct |
51 |
Correct |
253 ms |
25940 KB |
Output is correct |
52 |
Correct |
268 ms |
25996 KB |
Output is correct |
53 |
Correct |
105 ms |
16068 KB |
Output is correct |
54 |
Correct |
106 ms |
16004 KB |
Output is correct |
55 |
Correct |
249 ms |
25996 KB |
Output is correct |
56 |
Correct |
261 ms |
26020 KB |
Output is correct |
57 |
Correct |
107 ms |
16068 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
6 ms |
936 KB |
Output is correct |
61 |
Correct |
125 ms |
16008 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 |
76 ms |
6660 KB |
Output is correct |
66 |
Correct |
110 ms |
16012 KB |
Output is correct |
67 |
Correct |
110 ms |
16072 KB |
Output is correct |
68 |
Correct |
105 ms |
16020 KB |
Output is correct |
69 |
Correct |
118 ms |
16020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1352 KB |
Output is correct |
7 |
Correct |
250 ms |
26000 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1356 KB |
Output is correct |
13 |
Correct |
254 ms |
26004 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 |
121 ms |
16068 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
62 ms |
6660 KB |
Output is correct |
21 |
Correct |
282 ms |
25964 KB |
Output is correct |
22 |
Correct |
250 ms |
25916 KB |
Output is correct |
23 |
Correct |
263 ms |
25924 KB |
Output is correct |
24 |
Correct |
249 ms |
26060 KB |
Output is correct |
25 |
Correct |
120 ms |
16016 KB |
Output is correct |
26 |
Correct |
106 ms |
16012 KB |
Output is correct |
27 |
Correct |
265 ms |
25968 KB |
Output is correct |
28 |
Correct |
242 ms |
25924 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 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 |
11 ms |
1352 KB |
Output is correct |
37 |
Correct |
242 ms |
26012 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1336 KB |
Output is correct |
41 |
Correct |
250 ms |
25924 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
7 ms |
844 KB |
Output is correct |
45 |
Correct |
122 ms |
16020 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
63 ms |
6664 KB |
Output is correct |
50 |
Correct |
252 ms |
26000 KB |
Output is correct |
51 |
Correct |
253 ms |
25940 KB |
Output is correct |
52 |
Correct |
268 ms |
25996 KB |
Output is correct |
53 |
Correct |
105 ms |
16068 KB |
Output is correct |
54 |
Correct |
106 ms |
16004 KB |
Output is correct |
55 |
Correct |
249 ms |
25996 KB |
Output is correct |
56 |
Correct |
261 ms |
26020 KB |
Output is correct |
57 |
Correct |
107 ms |
16068 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 |
63 ms |
6760 KB |
Output is correct |
62 |
Correct |
260 ms |
25988 KB |
Output is correct |
63 |
Correct |
250 ms |
26000 KB |
Output is correct |
64 |
Correct |
263 ms |
26000 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
66 ms |
6656 KB |
Output is correct |
67 |
Correct |
255 ms |
25984 KB |
Output is correct |
68 |
Correct |
254 ms |
26000 KB |
Output is correct |
69 |
Correct |
277 ms |
25984 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 |
65 ms |
6804 KB |
Output is correct |
74 |
Correct |
248 ms |
26000 KB |
Output is correct |
75 |
Correct |
252 ms |
26052 KB |
Output is correct |
76 |
Correct |
249 ms |
25964 KB |
Output is correct |
77 |
Correct |
243 ms |
26004 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
6 ms |
936 KB |
Output is correct |
81 |
Correct |
125 ms |
16008 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 |
76 ms |
6660 KB |
Output is correct |
86 |
Correct |
110 ms |
16012 KB |
Output is correct |
87 |
Correct |
110 ms |
16072 KB |
Output is correct |
88 |
Correct |
105 ms |
16020 KB |
Output is correct |
89 |
Correct |
118 ms |
16020 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 |
844 KB |
Output is correct |
93 |
Correct |
116 ms |
16132 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 |
204 KB |
Output is correct |
97 |
Correct |
27 ms |
4172 KB |
Output is correct |
98 |
Correct |
108 ms |
16016 KB |
Output is correct |
99 |
Correct |
108 ms |
16004 KB |
Output is correct |
100 |
Correct |
109 ms |
16000 KB |
Output is correct |
101 |
Correct |
111 ms |
16064 KB |
Output is correct |
102 |
Correct |
110 ms |
16016 KB |
Output is correct |
103 |
Correct |
113 ms |
16012 KB |
Output is correct |
104 |
Correct |
128 ms |
16000 KB |
Output is correct |
105 |
Correct |
126 ms |
16068 KB |
Output is correct |