#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 = 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) {
for (int j = 0; j < n; ++j) {
int x = find(i, 0), y = find(j, 0);
if (!p[i][j] && (x == y || find(x, 1) == find(y, 1))) {
return 0;
}
if (p[i][j] == 1 && x != y && find(x, 1) == find(y,1)) {
return 0;
}
}
}
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:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int j = 0; j < cmp[z][1].size() - 1; ++j) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
161 ms |
22168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
161 ms |
22168 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
8 ms |
1372 KB |
Output is correct |
13 |
Correct |
162 ms |
22096 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 |
73 ms |
12392 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
39 ms |
5972 KB |
Output is correct |
21 |
Correct |
158 ms |
22204 KB |
Output is correct |
22 |
Correct |
178 ms |
22088 KB |
Output is correct |
23 |
Correct |
186 ms |
22096 KB |
Output is correct |
24 |
Correct |
158 ms |
22300 KB |
Output is correct |
25 |
Correct |
64 ms |
12348 KB |
Output is correct |
26 |
Correct |
63 ms |
12372 KB |
Output is correct |
27 |
Correct |
165 ms |
22100 KB |
Output is correct |
28 |
Correct |
155 ms |
22100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1160 KB |
Output is correct |
9 |
Correct |
161 ms |
22092 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 |
1372 KB |
Output is correct |
13 |
Correct |
155 ms |
22096 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
69 ms |
12372 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
39 ms |
5780 KB |
Output is correct |
22 |
Correct |
163 ms |
23724 KB |
Output is correct |
23 |
Correct |
186 ms |
23584 KB |
Output is correct |
24 |
Correct |
162 ms |
23660 KB |
Output is correct |
25 |
Correct |
66 ms |
13664 KB |
Output is correct |
26 |
Correct |
65 ms |
13788 KB |
Output is correct |
27 |
Correct |
167 ms |
23612 KB |
Output is correct |
28 |
Correct |
187 ms |
23732 KB |
Output is correct |
29 |
Correct |
66 ms |
13720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
39 ms |
5756 KB |
Output is correct |
5 |
Correct |
166 ms |
22608 KB |
Output is correct |
6 |
Correct |
156 ms |
22100 KB |
Output is correct |
7 |
Correct |
164 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
40 ms |
5876 KB |
Output is correct |
10 |
Correct |
160 ms |
22100 KB |
Output is correct |
11 |
Correct |
159 ms |
22084 KB |
Output is correct |
12 |
Correct |
161 ms |
22080 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
41 ms |
5776 KB |
Output is correct |
17 |
Correct |
180 ms |
22076 KB |
Output is correct |
18 |
Correct |
164 ms |
22092 KB |
Output is correct |
19 |
Correct |
161 ms |
22208 KB |
Output is correct |
20 |
Correct |
156 ms |
22072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
161 ms |
22168 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
8 ms |
1372 KB |
Output is correct |
13 |
Correct |
162 ms |
22096 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 |
73 ms |
12392 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
39 ms |
5972 KB |
Output is correct |
21 |
Correct |
158 ms |
22204 KB |
Output is correct |
22 |
Correct |
178 ms |
22088 KB |
Output is correct |
23 |
Correct |
186 ms |
22096 KB |
Output is correct |
24 |
Correct |
158 ms |
22300 KB |
Output is correct |
25 |
Correct |
64 ms |
12348 KB |
Output is correct |
26 |
Correct |
63 ms |
12372 KB |
Output is correct |
27 |
Correct |
165 ms |
22100 KB |
Output is correct |
28 |
Correct |
155 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1160 KB |
Output is correct |
37 |
Correct |
161 ms |
22092 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
155 ms |
22096 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
69 ms |
12372 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
39 ms |
5780 KB |
Output is correct |
50 |
Correct |
163 ms |
23724 KB |
Output is correct |
51 |
Correct |
186 ms |
23584 KB |
Output is correct |
52 |
Correct |
162 ms |
23660 KB |
Output is correct |
53 |
Correct |
66 ms |
13664 KB |
Output is correct |
54 |
Correct |
65 ms |
13788 KB |
Output is correct |
55 |
Correct |
167 ms |
23612 KB |
Output is correct |
56 |
Correct |
187 ms |
23732 KB |
Output is correct |
57 |
Correct |
66 ms |
13720 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
4 ms |
860 KB |
Output is correct |
61 |
Correct |
73 ms |
13612 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
40 ms |
6344 KB |
Output is correct |
66 |
Correct |
65 ms |
13652 KB |
Output is correct |
67 |
Correct |
79 ms |
13648 KB |
Output is correct |
68 |
Correct |
64 ms |
13648 KB |
Output is correct |
69 |
Correct |
67 ms |
13652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1364 KB |
Output is correct |
7 |
Correct |
161 ms |
22168 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
8 ms |
1372 KB |
Output is correct |
13 |
Correct |
162 ms |
22096 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 |
73 ms |
12392 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
39 ms |
5972 KB |
Output is correct |
21 |
Correct |
158 ms |
22204 KB |
Output is correct |
22 |
Correct |
178 ms |
22088 KB |
Output is correct |
23 |
Correct |
186 ms |
22096 KB |
Output is correct |
24 |
Correct |
158 ms |
22300 KB |
Output is correct |
25 |
Correct |
64 ms |
12348 KB |
Output is correct |
26 |
Correct |
63 ms |
12372 KB |
Output is correct |
27 |
Correct |
165 ms |
22100 KB |
Output is correct |
28 |
Correct |
155 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1160 KB |
Output is correct |
37 |
Correct |
161 ms |
22092 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
155 ms |
22096 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
69 ms |
12372 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
39 ms |
5780 KB |
Output is correct |
50 |
Correct |
163 ms |
23724 KB |
Output is correct |
51 |
Correct |
186 ms |
23584 KB |
Output is correct |
52 |
Correct |
162 ms |
23660 KB |
Output is correct |
53 |
Correct |
66 ms |
13664 KB |
Output is correct |
54 |
Correct |
65 ms |
13788 KB |
Output is correct |
55 |
Correct |
167 ms |
23612 KB |
Output is correct |
56 |
Correct |
187 ms |
23732 KB |
Output is correct |
57 |
Correct |
66 ms |
13720 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
39 ms |
5756 KB |
Output is correct |
62 |
Correct |
166 ms |
22608 KB |
Output is correct |
63 |
Correct |
156 ms |
22100 KB |
Output is correct |
64 |
Correct |
164 ms |
22096 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
40 ms |
5876 KB |
Output is correct |
67 |
Correct |
160 ms |
22100 KB |
Output is correct |
68 |
Correct |
159 ms |
22084 KB |
Output is correct |
69 |
Correct |
161 ms |
22080 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
344 KB |
Output is correct |
73 |
Correct |
41 ms |
5776 KB |
Output is correct |
74 |
Correct |
180 ms |
22076 KB |
Output is correct |
75 |
Correct |
164 ms |
22092 KB |
Output is correct |
76 |
Correct |
161 ms |
22208 KB |
Output is correct |
77 |
Correct |
156 ms |
22072 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
4 ms |
860 KB |
Output is correct |
81 |
Correct |
73 ms |
13612 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
40 ms |
6344 KB |
Output is correct |
86 |
Correct |
65 ms |
13652 KB |
Output is correct |
87 |
Correct |
79 ms |
13648 KB |
Output is correct |
88 |
Correct |
64 ms |
13648 KB |
Output is correct |
89 |
Correct |
67 ms |
13652 KB |
Output is correct |
90 |
Correct |
1 ms |
344 KB |
Output is correct |
91 |
Correct |
1 ms |
348 KB |
Output is correct |
92 |
Correct |
5 ms |
860 KB |
Output is correct |
93 |
Correct |
71 ms |
13644 KB |
Output is correct |
94 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
95 |
Halted |
0 ms |
0 KB |
- |