#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
int leader[1005], ranking[1005];
int Find(int n) {
if (leader[n] != n) leader[n] = Find(leader[n]);
return leader[n];
}
void Union(int x, int y) {
x = Find(x), y = Find(y);
if (ranking[x] > ranking[y]) swap(x, y);
ranking[y] += ranking[x];
leader[x] = y;
}
vector<int> g[1005];
bool visited[1005];
vector<int> components;
void dfs(int n) {
visited[n] = 1;
components.push_back(n);
for (auto x : g[n]) {
if (visited[x]) continue;
dfs(x);
}
}
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i <= n; i++) {
leader[i] = i;
ranking[i] = 1;
}
vector<vector<int>> answer(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 3) return 0;
if (p[i][j] == 1) Union(i, j);
}
}
for (int i = 0; i < n; i++) if (Find(i) != i) answer[Find(i)][i] = answer[i][Find(i)] = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 0 && Find(i) == Find(j)) return 0;
if (p[i][j] == 2 && Find(i) == i && Find(j) == j) {
g[i].push_back(j);
g[j].push_back(i);
}
}
}
for (int i = 0; i < n; i++) {
if (Find(i) != i || visited[i]) continue;
dfs(i);
if (components.size() == 2) return 0;
for (auto x : components) {
for (auto y : components) if (x != y && p[x][y] != 2) return 0;
}
for (int j = 0; j < components.size() - 1; j++) {
answer[components[j]][components[j + 1]] = answer[components[j + 1]][components[j]] = 1;
}
if (components.size() != 1) answer[components[0]][components[components.size() - 1]] = answer[components[components.size() - 1]][components[0]] = 1;
components.clear();
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:63:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int j = 0; j < components.size() - 1; 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 |
1 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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
152 ms |
22048 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 |
1 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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
152 ms |
22048 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
146 ms |
21972 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 |
792 KB |
Output is correct |
17 |
Correct |
73 ms |
12064 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
39 ms |
5796 KB |
Output is correct |
21 |
Correct |
167 ms |
22044 KB |
Output is correct |
22 |
Correct |
153 ms |
21996 KB |
Output is correct |
23 |
Correct |
172 ms |
22012 KB |
Output is correct |
24 |
Correct |
157 ms |
22048 KB |
Output is correct |
25 |
Correct |
72 ms |
12108 KB |
Output is correct |
26 |
Correct |
68 ms |
12068 KB |
Output is correct |
27 |
Correct |
168 ms |
22020 KB |
Output is correct |
28 |
Correct |
168 ms |
22064 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
9 ms |
1212 KB |
Output is correct |
9 |
Correct |
151 ms |
21988 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 |
1376 KB |
Output is correct |
13 |
Correct |
179 ms |
26144 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
96 ms |
15008 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
39 ms |
5780 KB |
Output is correct |
22 |
Correct |
157 ms |
24464 KB |
Output is correct |
23 |
Correct |
160 ms |
23964 KB |
Output is correct |
24 |
Correct |
162 ms |
27160 KB |
Output is correct |
25 |
Correct |
70 ms |
14112 KB |
Output is correct |
26 |
Correct |
74 ms |
14604 KB |
Output is correct |
27 |
Correct |
158 ms |
23988 KB |
Output is correct |
28 |
Correct |
163 ms |
26108 KB |
Output is correct |
29 |
Correct |
70 ms |
14108 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 |
39 ms |
5784 KB |
Output is correct |
5 |
Correct |
154 ms |
22048 KB |
Output is correct |
6 |
Correct |
155 ms |
22008 KB |
Output is correct |
7 |
Correct |
161 ms |
22048 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
47 ms |
5804 KB |
Output is correct |
10 |
Correct |
164 ms |
24472 KB |
Output is correct |
11 |
Correct |
167 ms |
24076 KB |
Output is correct |
12 |
Correct |
163 ms |
27164 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 |
39 ms |
6296 KB |
Output is correct |
17 |
Correct |
159 ms |
24476 KB |
Output is correct |
18 |
Correct |
157 ms |
24208 KB |
Output is correct |
19 |
Correct |
159 ms |
24092 KB |
Output is correct |
20 |
Correct |
154 ms |
24016 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 |
1 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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
152 ms |
22048 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
146 ms |
21972 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 |
792 KB |
Output is correct |
17 |
Correct |
73 ms |
12064 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
39 ms |
5796 KB |
Output is correct |
21 |
Correct |
167 ms |
22044 KB |
Output is correct |
22 |
Correct |
153 ms |
21996 KB |
Output is correct |
23 |
Correct |
172 ms |
22012 KB |
Output is correct |
24 |
Correct |
157 ms |
22048 KB |
Output is correct |
25 |
Correct |
72 ms |
12108 KB |
Output is correct |
26 |
Correct |
68 ms |
12068 KB |
Output is correct |
27 |
Correct |
168 ms |
22020 KB |
Output is correct |
28 |
Correct |
168 ms |
22064 KB |
Output is correct |
29 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
9 ms |
1212 KB |
Output is correct |
37 |
Correct |
151 ms |
21988 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1376 KB |
Output is correct |
41 |
Correct |
179 ms |
26144 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
96 ms |
15008 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
5780 KB |
Output is correct |
50 |
Correct |
157 ms |
24464 KB |
Output is correct |
51 |
Correct |
160 ms |
23964 KB |
Output is correct |
52 |
Correct |
162 ms |
27160 KB |
Output is correct |
53 |
Correct |
70 ms |
14112 KB |
Output is correct |
54 |
Correct |
74 ms |
14604 KB |
Output is correct |
55 |
Correct |
158 ms |
23988 KB |
Output is correct |
56 |
Correct |
163 ms |
26108 KB |
Output is correct |
57 |
Correct |
70 ms |
14108 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
852 KB |
Output is correct |
61 |
Correct |
71 ms |
14100 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
328 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
39 ms |
6292 KB |
Output is correct |
66 |
Correct |
70 ms |
14604 KB |
Output is correct |
67 |
Correct |
68 ms |
14148 KB |
Output is correct |
68 |
Correct |
71 ms |
14236 KB |
Output is correct |
69 |
Correct |
69 ms |
14108 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 |
1 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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
152 ms |
22048 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
146 ms |
21972 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 |
792 KB |
Output is correct |
17 |
Correct |
73 ms |
12064 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
39 ms |
5796 KB |
Output is correct |
21 |
Correct |
167 ms |
22044 KB |
Output is correct |
22 |
Correct |
153 ms |
21996 KB |
Output is correct |
23 |
Correct |
172 ms |
22012 KB |
Output is correct |
24 |
Correct |
157 ms |
22048 KB |
Output is correct |
25 |
Correct |
72 ms |
12108 KB |
Output is correct |
26 |
Correct |
68 ms |
12068 KB |
Output is correct |
27 |
Correct |
168 ms |
22020 KB |
Output is correct |
28 |
Correct |
168 ms |
22064 KB |
Output is correct |
29 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
9 ms |
1212 KB |
Output is correct |
37 |
Correct |
151 ms |
21988 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1376 KB |
Output is correct |
41 |
Correct |
179 ms |
26144 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
96 ms |
15008 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
5780 KB |
Output is correct |
50 |
Correct |
157 ms |
24464 KB |
Output is correct |
51 |
Correct |
160 ms |
23964 KB |
Output is correct |
52 |
Correct |
162 ms |
27160 KB |
Output is correct |
53 |
Correct |
70 ms |
14112 KB |
Output is correct |
54 |
Correct |
74 ms |
14604 KB |
Output is correct |
55 |
Correct |
158 ms |
23988 KB |
Output is correct |
56 |
Correct |
163 ms |
26108 KB |
Output is correct |
57 |
Correct |
70 ms |
14108 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 |
39 ms |
5784 KB |
Output is correct |
62 |
Correct |
154 ms |
22048 KB |
Output is correct |
63 |
Correct |
155 ms |
22008 KB |
Output is correct |
64 |
Correct |
161 ms |
22048 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
47 ms |
5804 KB |
Output is correct |
67 |
Correct |
164 ms |
24472 KB |
Output is correct |
68 |
Correct |
167 ms |
24076 KB |
Output is correct |
69 |
Correct |
163 ms |
27164 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 |
39 ms |
6296 KB |
Output is correct |
74 |
Correct |
159 ms |
24476 KB |
Output is correct |
75 |
Correct |
157 ms |
24208 KB |
Output is correct |
76 |
Correct |
159 ms |
24092 KB |
Output is correct |
77 |
Correct |
154 ms |
24016 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
3 ms |
852 KB |
Output is correct |
81 |
Correct |
71 ms |
14100 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
328 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
39 ms |
6292 KB |
Output is correct |
86 |
Correct |
70 ms |
14604 KB |
Output is correct |
87 |
Correct |
68 ms |
14148 KB |
Output is correct |
88 |
Correct |
71 ms |
14236 KB |
Output is correct |
89 |
Correct |
69 ms |
14108 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
328 KB |
Output is correct |
92 |
Correct |
5 ms |
852 KB |
Output is correct |
93 |
Correct |
84 ms |
14020 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
23 ms |
3692 KB |
Output is correct |
98 |
Correct |
70 ms |
14104 KB |
Output is correct |
99 |
Correct |
67 ms |
14108 KB |
Output is correct |
100 |
Correct |
66 ms |
14096 KB |
Output is correct |
101 |
Correct |
67 ms |
14108 KB |
Output is correct |
102 |
Correct |
66 ms |
14108 KB |
Output is correct |
103 |
Correct |
70 ms |
14104 KB |
Output is correct |
104 |
Correct |
67 ms |
14024 KB |
Output is correct |
105 |
Correct |
70 ms |
14104 KB |
Output is correct |