#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
const int N = 1005;
class DSU {
int par[N], n;
public:
DSU(int n) {
this->n = n;
iota(par, par + n, 0);
}
int find(int x) {
if (x == par[x])
return x;
return par[x] = find(par[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
par[u] = v;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
DSU dsu(n);
if (n == 1) {
build({{0}});
return 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 3)
return 0;
else if (p[i][j] != 0)
dsu.merge(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 0 && dsu.find(i) == dsu.find(j))
return 0;
}
}
vector<vector<int>> comp(n);
vector<vector<int>> b(n);
for (int i = 0; i < n; i++) b[i].resize(n, 0);
vector<bool> done(n, false);
for (int i = 0, k = 0; i < n; i++) {
if (done[i])
continue;
comp[k].push_back(i);
for (int j = i + 1; j < n; j++) {
if (dsu.find(i) == dsu.find(j))
comp[k].push_back(j), done[j] = true;
}
k++;
}
for (int i = 0; i < comp.size() && !comp[i].empty(); i++) {
//cout << "Done" << endl;
if (comp[i].size() == 1) continue;
set<int> s;
for (int j = 0; j < comp[i].size(); j++) {
for (int k = 0; k < comp[i].size(); k++) {
if (k != j && p[comp[i][j]][comp[i][k]] == 1)
s.insert(comp[i][j]), s.insert(comp[i][k]);
}
}
for (int j : s) {
for (int k : s) {
if (p[j][k] != 1)
return 0;
}
}
if (s.size() == comp[i].size() - 1)
return 0;
if (s.empty()) {
if (comp[i].size() <= 2)
return 0;
int last = -1;
for (int j : comp[i]) {
if (last != -1)
b[j][last] = b[last][j] = 1;
last = j;
}
b[*comp[i].begin()][*comp[i].rbegin()] = b[*comp[i].rbegin()][*comp[i].begin()] = 1;
}
int last = -1;
for (int j : s) {
if (last != -1)
b[j][last] = b[last][j] = 1;
last = j;
}
int last1 = -1, first1 = -1;
for (int j : comp[i]) {
if (last1 != -1)
b[last1][j] = b[j][last1] = 1;
else
first1 = j;
last1 = j;
}
if (s.size() != comp[i].size()) {
if (last != -1 && first1 != -1)
b[last][first1] = b[first1][last] = 1;
if (last != -1 && last1 != -1)
b[last][last1] = b[last1][last] = 1;
}
}
build(b);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < comp.size() && !comp[i].empty(); i++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:74:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int j = 0; j < comp[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp:75:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int k = 0; k < comp[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
14 ms |
1152 KB |
Output is correct |
7 |
Correct |
328 ms |
22196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
14 ms |
1152 KB |
Output is correct |
7 |
Correct |
328 ms |
22196 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
265 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
121 ms |
8184 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
65 ms |
5880 KB |
Output is correct |
21 |
Correct |
264 ms |
22136 KB |
Output is correct |
22 |
Correct |
252 ms |
22136 KB |
Output is correct |
23 |
Correct |
294 ms |
22136 KB |
Output is correct |
24 |
Correct |
256 ms |
22264 KB |
Output is correct |
25 |
Correct |
110 ms |
8184 KB |
Output is correct |
26 |
Correct |
107 ms |
8184 KB |
Output is correct |
27 |
Correct |
298 ms |
22136 KB |
Output is correct |
28 |
Correct |
255 ms |
22136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
253 ms |
22136 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
259 ms |
22108 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
118 ms |
8312 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
68 ms |
5880 KB |
Output is correct |
22 |
Correct |
251 ms |
22136 KB |
Output is correct |
23 |
Correct |
257 ms |
22136 KB |
Output is correct |
24 |
Correct |
258 ms |
22136 KB |
Output is correct |
25 |
Correct |
118 ms |
12280 KB |
Output is correct |
26 |
Correct |
107 ms |
8184 KB |
Output is correct |
27 |
Correct |
254 ms |
22136 KB |
Output is correct |
28 |
Correct |
276 ms |
22136 KB |
Output is correct |
29 |
Correct |
118 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
74 ms |
5880 KB |
Output is correct |
5 |
Correct |
261 ms |
22264 KB |
Output is correct |
6 |
Correct |
264 ms |
22136 KB |
Output is correct |
7 |
Correct |
296 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
63 ms |
5880 KB |
Output is correct |
10 |
Correct |
253 ms |
22104 KB |
Output is correct |
11 |
Correct |
252 ms |
22264 KB |
Output is correct |
12 |
Correct |
257 ms |
22104 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Incorrect |
0 ms |
256 KB |
Too many ways to get from 0 to 4, should be 1 found no less than 2 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
14 ms |
1152 KB |
Output is correct |
7 |
Correct |
328 ms |
22196 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
265 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
121 ms |
8184 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
65 ms |
5880 KB |
Output is correct |
21 |
Correct |
264 ms |
22136 KB |
Output is correct |
22 |
Correct |
252 ms |
22136 KB |
Output is correct |
23 |
Correct |
294 ms |
22136 KB |
Output is correct |
24 |
Correct |
256 ms |
22264 KB |
Output is correct |
25 |
Correct |
110 ms |
8184 KB |
Output is correct |
26 |
Correct |
107 ms |
8184 KB |
Output is correct |
27 |
Correct |
298 ms |
22136 KB |
Output is correct |
28 |
Correct |
255 ms |
22136 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
253 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
11 ms |
1280 KB |
Output is correct |
41 |
Correct |
259 ms |
22108 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
118 ms |
8312 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
256 KB |
Output is correct |
49 |
Correct |
68 ms |
5880 KB |
Output is correct |
50 |
Correct |
251 ms |
22136 KB |
Output is correct |
51 |
Correct |
257 ms |
22136 KB |
Output is correct |
52 |
Correct |
258 ms |
22136 KB |
Output is correct |
53 |
Correct |
118 ms |
12280 KB |
Output is correct |
54 |
Correct |
107 ms |
8184 KB |
Output is correct |
55 |
Correct |
254 ms |
22136 KB |
Output is correct |
56 |
Correct |
276 ms |
22136 KB |
Output is correct |
57 |
Correct |
118 ms |
12280 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
5 ms |
640 KB |
Output is correct |
61 |
Correct |
118 ms |
8152 KB |
Output is correct |
62 |
Correct |
1 ms |
256 KB |
Output is correct |
63 |
Correct |
0 ms |
256 KB |
Output is correct |
64 |
Correct |
1 ms |
256 KB |
Output is correct |
65 |
Incorrect |
30 ms |
3320 KB |
Answer gives possible 0 while actual possible 1 |
66 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
14 ms |
1152 KB |
Output is correct |
7 |
Correct |
328 ms |
22196 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
265 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
121 ms |
8184 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
65 ms |
5880 KB |
Output is correct |
21 |
Correct |
264 ms |
22136 KB |
Output is correct |
22 |
Correct |
252 ms |
22136 KB |
Output is correct |
23 |
Correct |
294 ms |
22136 KB |
Output is correct |
24 |
Correct |
256 ms |
22264 KB |
Output is correct |
25 |
Correct |
110 ms |
8184 KB |
Output is correct |
26 |
Correct |
107 ms |
8184 KB |
Output is correct |
27 |
Correct |
298 ms |
22136 KB |
Output is correct |
28 |
Correct |
255 ms |
22136 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
253 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
11 ms |
1280 KB |
Output is correct |
41 |
Correct |
259 ms |
22108 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
118 ms |
8312 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
256 KB |
Output is correct |
49 |
Correct |
68 ms |
5880 KB |
Output is correct |
50 |
Correct |
251 ms |
22136 KB |
Output is correct |
51 |
Correct |
257 ms |
22136 KB |
Output is correct |
52 |
Correct |
258 ms |
22136 KB |
Output is correct |
53 |
Correct |
118 ms |
12280 KB |
Output is correct |
54 |
Correct |
107 ms |
8184 KB |
Output is correct |
55 |
Correct |
254 ms |
22136 KB |
Output is correct |
56 |
Correct |
276 ms |
22136 KB |
Output is correct |
57 |
Correct |
118 ms |
12280 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
1 ms |
256 KB |
Output is correct |
61 |
Correct |
74 ms |
5880 KB |
Output is correct |
62 |
Correct |
261 ms |
22264 KB |
Output is correct |
63 |
Correct |
264 ms |
22136 KB |
Output is correct |
64 |
Correct |
296 ms |
22136 KB |
Output is correct |
65 |
Correct |
0 ms |
256 KB |
Output is correct |
66 |
Correct |
63 ms |
5880 KB |
Output is correct |
67 |
Correct |
253 ms |
22104 KB |
Output is correct |
68 |
Correct |
252 ms |
22264 KB |
Output is correct |
69 |
Correct |
257 ms |
22104 KB |
Output is correct |
70 |
Correct |
1 ms |
256 KB |
Output is correct |
71 |
Correct |
1 ms |
256 KB |
Output is correct |
72 |
Incorrect |
0 ms |
256 KB |
Too many ways to get from 0 to 4, should be 1 found no less than 2 |
73 |
Halted |
0 ms |
0 KB |
- |