#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
const int mxN = 1007;
int dsu[mxN][2];
bool mark[mxN], done[mxN];
vector<int> cmp[mxN][2];
int find(int x, int id) {
return dsu[x][id] < 0 ? x : dsu[x][id] = find(dsu[x][id], id);
}
bool unite(int x, int y, int id) {
x = find(x, id), y = find(y, id);
if (x == y) {
return false;
}
if (dsu[x][id] > dsu[y][id]) {
swap(x, y);
}
dsu[x][id] += dsu[y][id];
dsu[y][id] = x;
for (auto c : cmp[y][id]) {
cmp[x][id].push_back(c);
}
return true;
}
int construct(vector<vector<int>> p) {
int n = p.size();
memset(dsu, -1, sizeof(dsu));
vector<vector<int>> ans(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
cmp[i][0].push_back(i);
cmp[i][1].push_back(i);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (p[i][j] == 1) {
if (unite(i, j, 0)) {
ans[i][j] = ans[j][i] = 1;
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (!p[i][j] && find(i, 0) == find(j, 0)) {
return 0;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] == 2) {
int x = find(i, 0), y = find(j, 0);
if (x == y) {
return 0;
}
if (find(x, 1) == find(y, 1)) {
continue;
}
for (auto u : cmp[x][0]) {
for (auto v : cmp[y][0]) {
if (p[u][v] != 2) {
return 0;
}
}
}
unite(x, y, 1);
}
}
}
for (int i = 0; i < n; ++i) {
int x = find(i, 0);
int z = find(x, 1);
if (done[z] || cmp[z][1].size() == 1) {
continue;
}
if (cmp[z][1].size() == 2) {
return 0;
}
for (int j = 0; j < cmp[z][1].size() - 1; ++j) {
int a = cmp[z][1][j], b = cmp[z][1][j+1];
ans[a][b] = ans[b][a] = 1;
}
int a = cmp[z][1][0], b = cmp[z][1].back();
ans[a][b] = ans[b][a] = 1;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int j = 0; j < cmp[z][1].size() - 1; ++j) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1368 KB |
Output is correct |
7 |
Correct |
158 ms |
22956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1368 KB |
Output is correct |
7 |
Correct |
158 ms |
22956 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1456 KB |
Output is correct |
13 |
Correct |
155 ms |
23636 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
74 ms |
13636 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
6316 KB |
Output is correct |
21 |
Correct |
166 ms |
23876 KB |
Output is correct |
22 |
Correct |
161 ms |
23620 KB |
Output is correct |
23 |
Correct |
184 ms |
23608 KB |
Output is correct |
24 |
Correct |
155 ms |
23848 KB |
Output is correct |
25 |
Correct |
68 ms |
13820 KB |
Output is correct |
26 |
Correct |
68 ms |
14124 KB |
Output is correct |
27 |
Correct |
166 ms |
23636 KB |
Output is correct |
28 |
Correct |
156 ms |
23632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
153 ms |
22600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
22820 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
43 ms |
6304 KB |
Output is correct |
5 |
Correct |
155 ms |
23536 KB |
Output is correct |
6 |
Correct |
157 ms |
23736 KB |
Output is correct |
7 |
Correct |
178 ms |
23636 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
39 ms |
6484 KB |
Output is correct |
10 |
Correct |
185 ms |
23580 KB |
Output is correct |
11 |
Correct |
154 ms |
23376 KB |
Output is correct |
12 |
Correct |
175 ms |
23540 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
42 ms |
6356 KB |
Output is correct |
17 |
Correct |
163 ms |
23804 KB |
Output is correct |
18 |
Correct |
162 ms |
23724 KB |
Output is correct |
19 |
Correct |
158 ms |
23568 KB |
Output is correct |
20 |
Correct |
179 ms |
23744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1368 KB |
Output is correct |
7 |
Correct |
158 ms |
22956 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1456 KB |
Output is correct |
13 |
Correct |
155 ms |
23636 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
74 ms |
13636 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
6316 KB |
Output is correct |
21 |
Correct |
166 ms |
23876 KB |
Output is correct |
22 |
Correct |
161 ms |
23620 KB |
Output is correct |
23 |
Correct |
184 ms |
23608 KB |
Output is correct |
24 |
Correct |
155 ms |
23848 KB |
Output is correct |
25 |
Correct |
68 ms |
13820 KB |
Output is correct |
26 |
Correct |
68 ms |
14124 KB |
Output is correct |
27 |
Correct |
166 ms |
23636 KB |
Output is correct |
28 |
Correct |
156 ms |
23632 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1372 KB |
Output is correct |
37 |
Correct |
153 ms |
22600 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
158 ms |
22820 KB |
Output is correct |
42 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1368 KB |
Output is correct |
7 |
Correct |
158 ms |
22956 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1456 KB |
Output is correct |
13 |
Correct |
155 ms |
23636 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
74 ms |
13636 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
6316 KB |
Output is correct |
21 |
Correct |
166 ms |
23876 KB |
Output is correct |
22 |
Correct |
161 ms |
23620 KB |
Output is correct |
23 |
Correct |
184 ms |
23608 KB |
Output is correct |
24 |
Correct |
155 ms |
23848 KB |
Output is correct |
25 |
Correct |
68 ms |
13820 KB |
Output is correct |
26 |
Correct |
68 ms |
14124 KB |
Output is correct |
27 |
Correct |
166 ms |
23636 KB |
Output is correct |
28 |
Correct |
156 ms |
23632 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1372 KB |
Output is correct |
37 |
Correct |
153 ms |
22600 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
158 ms |
22820 KB |
Output is correct |
42 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
43 |
Halted |
0 ms |
0 KB |
- |