#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1007;
vector<int> g[N];
bool vis[N];
int cmp[N];
vector<int> comp;
int par[N];
int find_par(int v) {
return par[v] == v ? v : par[v] = find_par(par[v]);
}
bool unite(int v, int u) {
v = find_par(v);
u = find_par(u);
par[v] = u;
return v != u;
}
void dfs(int v, int st) {
vis[v] = true;
cmp[v] = st;
comp.push_back(v);
for (int u : g[v]) {
if (!vis[u]) dfs(u, st);
}
}
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 0; i < n; i++) {
if (p[i][i] != 1) return 0;
for (int j = 0; j < i; j++) {
if (p[i][j] != p[j][i]) return 0;
if (p[i][j] == 3) return 0;
if (p[i][j] > 0) {
g[i].push_back(j);
g[j].push_back(i);
}
}
}
vector<vector<int>> b(n, vector<int>(n, 0));
auto add_edge = [&](int v, int u) {
b[v][u] = b[u][v] = 1;
};
for (int i = 0; i < n; i++) {
vis[i] = false;
cmp[i] = -1;
}
for (int st = 0; st < n; st++) {
if (!vis[st]) {
comp = {};
dfs(st, st);
bool ok = true;
for (int v : comp) {
for (int u : comp) ok &= p[v][u] > 0;
for (int i = 0; i < n; i++) {
if (cmp[i] != st) ok &= p[v][i] == 0;
}
}
if (!ok) return 0;
bool has = false;
for (int v : comp) {
for (int u : comp) has |= p[v][u] == 2;
}
if (has) {
for (int i = 0; i < n; i++) par[i] = i;
for (int v : comp) {
for (int u : comp) {
if (p[v][u] == 1) unite(v, u);
}
}
for (int v : comp) {
for (int u : comp) {
if (find_par(v) == find_par(u) && p[v][u] == 2) return 0;
if (find_par(v) != find_par(u) && p[v][u] == 1) return 0;
}
}
for (int v : comp) find_par(v);
vector<int> arr;
for (int v : comp) arr.push_back(find_par(v));
sort(arr.begin(), arr.end());
arr.erase(unique(arr.begin(), arr.end()), arr.end());
if (arr.size() < 3) return 0;
vector<vector<int>> arrs(arr.size());
for (int v : comp) {
arrs[lower_bound(arr.begin(), arr.end(), find_par(v)) - arr.begin()].push_back(v);
}
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j + 1 < arrs[i].size(); j++) add_edge(arrs[i][j], arrs[i][j + 1]);
}
for (int i = 0; i < arr.size(); i++) add_edge(arr[i], arr[(i + 1) % (int)arr.size()]);
} else {
for (int i = 0; i + 1 < comp.size(); i++) add_edge(comp[i], comp[i + 1]);
}
}
}
build(b);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:95:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
supertrees.cpp:96:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int j = 0; j + 1 < arrs[i].size(); j++) add_edge(arrs[i][j], arrs[i][j + 1]);
| ~~~~~~^~~~~~~~~~~~~~~~
supertrees.cpp:98:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int i = 0; i < arr.size(); i++) add_edge(arr[i], arr[(i + 1) % (int)arr.size()]);
| ~~^~~~~~~~~~~~
supertrees.cpp:100:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int i = 0; i + 1 < comp.size(); i++) add_edge(comp[i], comp[i + 1]);
| ~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1516 KB |
Output is correct |
7 |
Correct |
261 ms |
26348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1516 KB |
Output is correct |
7 |
Correct |
261 ms |
26348 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
261 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
6 ms |
1004 KB |
Output is correct |
17 |
Correct |
120 ms |
17008 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
64 ms |
6380 KB |
Output is correct |
21 |
Correct |
256 ms |
24688 KB |
Output is correct |
22 |
Correct |
257 ms |
24172 KB |
Output is correct |
23 |
Correct |
266 ms |
26252 KB |
Output is correct |
24 |
Correct |
254 ms |
24068 KB |
Output is correct |
25 |
Correct |
121 ms |
14780 KB |
Output is correct |
26 |
Correct |
105 ms |
14192 KB |
Output is correct |
27 |
Correct |
263 ms |
27500 KB |
Output is correct |
28 |
Correct |
249 ms |
24044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
10 ms |
1260 KB |
Output is correct |
9 |
Correct |
250 ms |
22144 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1516 KB |
Output is correct |
13 |
Correct |
271 ms |
26268 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
6 ms |
1004 KB |
Output is correct |
17 |
Correct |
116 ms |
14956 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
0 ms |
364 KB |
Output is correct |
21 |
Correct |
66 ms |
5868 KB |
Output is correct |
22 |
Correct |
255 ms |
24684 KB |
Output is correct |
23 |
Correct |
253 ms |
24172 KB |
Output is correct |
24 |
Correct |
267 ms |
27504 KB |
Output is correct |
25 |
Correct |
109 ms |
14188 KB |
Output is correct |
26 |
Correct |
108 ms |
14700 KB |
Output is correct |
27 |
Correct |
252 ms |
24428 KB |
Output is correct |
28 |
Correct |
261 ms |
26220 KB |
Output is correct |
29 |
Correct |
104 ms |
14188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
64 ms |
6380 KB |
Output is correct |
5 |
Correct |
272 ms |
24684 KB |
Output is correct |
6 |
Correct |
254 ms |
24172 KB |
Output is correct |
7 |
Correct |
268 ms |
26348 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
66 ms |
6380 KB |
Output is correct |
10 |
Correct |
261 ms |
24684 KB |
Output is correct |
11 |
Correct |
254 ms |
24176 KB |
Output is correct |
12 |
Correct |
265 ms |
27500 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
65 ms |
6432 KB |
Output is correct |
17 |
Correct |
275 ms |
24812 KB |
Output is correct |
18 |
Correct |
253 ms |
24612 KB |
Output is correct |
19 |
Correct |
255 ms |
24300 KB |
Output is correct |
20 |
Correct |
259 ms |
24172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1516 KB |
Output is correct |
7 |
Correct |
261 ms |
26348 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
261 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
6 ms |
1004 KB |
Output is correct |
17 |
Correct |
120 ms |
17008 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
64 ms |
6380 KB |
Output is correct |
21 |
Correct |
256 ms |
24688 KB |
Output is correct |
22 |
Correct |
257 ms |
24172 KB |
Output is correct |
23 |
Correct |
266 ms |
26252 KB |
Output is correct |
24 |
Correct |
254 ms |
24068 KB |
Output is correct |
25 |
Correct |
121 ms |
14780 KB |
Output is correct |
26 |
Correct |
105 ms |
14192 KB |
Output is correct |
27 |
Correct |
263 ms |
27500 KB |
Output is correct |
28 |
Correct |
249 ms |
24044 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
0 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
0 ms |
364 KB |
Output is correct |
33 |
Correct |
0 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
10 ms |
1260 KB |
Output is correct |
37 |
Correct |
250 ms |
22144 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
11 ms |
1516 KB |
Output is correct |
41 |
Correct |
271 ms |
26268 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
6 ms |
1004 KB |
Output is correct |
45 |
Correct |
116 ms |
14956 KB |
Output is correct |
46 |
Correct |
0 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
0 ms |
364 KB |
Output is correct |
49 |
Correct |
66 ms |
5868 KB |
Output is correct |
50 |
Correct |
255 ms |
24684 KB |
Output is correct |
51 |
Correct |
253 ms |
24172 KB |
Output is correct |
52 |
Correct |
267 ms |
27504 KB |
Output is correct |
53 |
Correct |
109 ms |
14188 KB |
Output is correct |
54 |
Correct |
108 ms |
14700 KB |
Output is correct |
55 |
Correct |
252 ms |
24428 KB |
Output is correct |
56 |
Correct |
261 ms |
26220 KB |
Output is correct |
57 |
Correct |
104 ms |
14188 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
5 ms |
1132 KB |
Output is correct |
61 |
Correct |
117 ms |
18540 KB |
Output is correct |
62 |
Correct |
1 ms |
492 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Correct |
63 ms |
6380 KB |
Output is correct |
66 |
Correct |
114 ms |
14956 KB |
Output is correct |
67 |
Correct |
111 ms |
14700 KB |
Output is correct |
68 |
Correct |
106 ms |
14316 KB |
Output is correct |
69 |
Correct |
108 ms |
14188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1516 KB |
Output is correct |
7 |
Correct |
261 ms |
26348 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
261 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
6 ms |
1004 KB |
Output is correct |
17 |
Correct |
120 ms |
17008 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
64 ms |
6380 KB |
Output is correct |
21 |
Correct |
256 ms |
24688 KB |
Output is correct |
22 |
Correct |
257 ms |
24172 KB |
Output is correct |
23 |
Correct |
266 ms |
26252 KB |
Output is correct |
24 |
Correct |
254 ms |
24068 KB |
Output is correct |
25 |
Correct |
121 ms |
14780 KB |
Output is correct |
26 |
Correct |
105 ms |
14192 KB |
Output is correct |
27 |
Correct |
263 ms |
27500 KB |
Output is correct |
28 |
Correct |
249 ms |
24044 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
0 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
0 ms |
364 KB |
Output is correct |
33 |
Correct |
0 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
10 ms |
1260 KB |
Output is correct |
37 |
Correct |
250 ms |
22144 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
11 ms |
1516 KB |
Output is correct |
41 |
Correct |
271 ms |
26268 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
6 ms |
1004 KB |
Output is correct |
45 |
Correct |
116 ms |
14956 KB |
Output is correct |
46 |
Correct |
0 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
0 ms |
364 KB |
Output is correct |
49 |
Correct |
66 ms |
5868 KB |
Output is correct |
50 |
Correct |
255 ms |
24684 KB |
Output is correct |
51 |
Correct |
253 ms |
24172 KB |
Output is correct |
52 |
Correct |
267 ms |
27504 KB |
Output is correct |
53 |
Correct |
109 ms |
14188 KB |
Output is correct |
54 |
Correct |
108 ms |
14700 KB |
Output is correct |
55 |
Correct |
252 ms |
24428 KB |
Output is correct |
56 |
Correct |
261 ms |
26220 KB |
Output is correct |
57 |
Correct |
104 ms |
14188 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
0 ms |
364 KB |
Output is correct |
61 |
Correct |
64 ms |
6380 KB |
Output is correct |
62 |
Correct |
272 ms |
24684 KB |
Output is correct |
63 |
Correct |
254 ms |
24172 KB |
Output is correct |
64 |
Correct |
268 ms |
26348 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
66 ms |
6380 KB |
Output is correct |
67 |
Correct |
261 ms |
24684 KB |
Output is correct |
68 |
Correct |
254 ms |
24176 KB |
Output is correct |
69 |
Correct |
265 ms |
27500 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |
73 |
Correct |
65 ms |
6432 KB |
Output is correct |
74 |
Correct |
275 ms |
24812 KB |
Output is correct |
75 |
Correct |
253 ms |
24612 KB |
Output is correct |
76 |
Correct |
255 ms |
24300 KB |
Output is correct |
77 |
Correct |
259 ms |
24172 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
5 ms |
1132 KB |
Output is correct |
81 |
Correct |
117 ms |
18540 KB |
Output is correct |
82 |
Correct |
1 ms |
492 KB |
Output is correct |
83 |
Correct |
1 ms |
364 KB |
Output is correct |
84 |
Correct |
1 ms |
364 KB |
Output is correct |
85 |
Correct |
63 ms |
6380 KB |
Output is correct |
86 |
Correct |
114 ms |
14956 KB |
Output is correct |
87 |
Correct |
111 ms |
14700 KB |
Output is correct |
88 |
Correct |
106 ms |
14316 KB |
Output is correct |
89 |
Correct |
108 ms |
14188 KB |
Output is correct |
90 |
Correct |
1 ms |
364 KB |
Output is correct |
91 |
Correct |
1 ms |
364 KB |
Output is correct |
92 |
Correct |
5 ms |
748 KB |
Output is correct |
93 |
Correct |
101 ms |
10224 KB |
Output is correct |
94 |
Correct |
1 ms |
364 KB |
Output is correct |
95 |
Correct |
1 ms |
364 KB |
Output is correct |
96 |
Correct |
1 ms |
364 KB |
Output is correct |
97 |
Correct |
26 ms |
2796 KB |
Output is correct |
98 |
Correct |
107 ms |
10604 KB |
Output is correct |
99 |
Correct |
104 ms |
10348 KB |
Output is correct |
100 |
Correct |
103 ms |
10220 KB |
Output is correct |
101 |
Correct |
103 ms |
10220 KB |
Output is correct |
102 |
Correct |
92 ms |
10220 KB |
Output is correct |
103 |
Correct |
94 ms |
10220 KB |
Output is correct |
104 |
Correct |
92 ms |
10220 KB |
Output is correct |
105 |
Correct |
102 ms |
10220 KB |
Output is correct |