#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
class DisjointSetUnion {
protected:
vector<int> parent;
vector<int> compSize;
const int n;
int connectedComponents;
public:
int getConnectedComponents() const {
return connectedComponents;
}
public:
DisjointSetUnion(int sz) : n(sz), connectedComponents(sz) {
parent.resize(sz), compSize.resize(sz);
for (int i = 0; i < n; i++) {
parent[i] = i, compSize[i] = 1;
}
}
int find_head(int x) const {
int cur = x;
while (cur != parent[cur]) {
cur = parent[cur];
}
return cur;
}
void join(int x, int y) {
x = find_head(x);
y = find_head(y);
if (x == y) {
return;
}
if (compSize[x] > compSize[y]) {
swap(x, y);
//ensures that compSize[x1] <= compSize[y1]
}
parent[x] = y;
compSize[y] += compSize[x];
connectedComponents--;
}
bool comp(int x, int y) {
return (find_head(x) == find_head(y));
}
};
int construct (vector<vector<int>> grid) {
vector<vector<int>> ans(grid.size());
for (int i = 0; i < grid.size(); i++) {
ans[i].assign(grid[0].size(), 0);
}
set<int> s;
map<int,int> cnt;
DisjointSetUnion disjointSetUnion(grid.size());
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid.size(); j++) {
if (grid[i][j] == 3) {
return false;
}
s.insert(grid[i][j]);
cnt[grid[i][j]]++;
if (grid[i][j] != 0) {
disjointSetUnion.join(i, j);
}
}
}
map<int,vector<int>> myMap;
for (int i = 0; i < grid.size(); i++) {
myMap[disjointSetUnion.find_head(i)].push_back(i);
}
if (cnt[0] >= 0 && cnt[1] >= 0 && cnt[2] >= 0 && cnt[3] == 0) {
for (auto& p: myMap) {
DisjointSetUnion dsu(grid.size());
map<int,vector<int>> m;
for (int x: p.second) {
for (int y: p.second) {
if (grid[x][y] == 1) {
dsu.join(x, y);
}
}
}
for (int i: p.second) {
m[dsu.find_head(i)].push_back(i);
}
vector<int> vec;
for (auto& q: m) {
vec.push_back(q.first);
}
if (vec.size() != 1) {
if (vec.size() == 2) {
return false;
}
for (int i = 0; i < vec.size(); i++) {
ans[vec[i]][vec[(i + 1) % (int) vec.size()]] = ans[vec[(i + 1) % (int) vec.size()]][vec[i]] = 1;
}
}
for (int x: vec) {
for (int y: vec) {
if (grid[x][y] == 0) {
return false;
}
}
}
for (auto& q: m) {
vector<int> v = q.second;
for (int i: v) {
for (int j: v) {
if (grid[i][j] == 0) {
return false;
}
}
}
for (int i = 1; i < (int)v.size(); i++) {
ans[v[i]][v[0]] = 1;
ans[v[0]][v[i]] = 1;
}
for (int x: v) {
for (int y: v) {
if (grid[x][y] != 1) {
return 0;
}
}
}
}
}
build(ans);
return 1;
}
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < grid.size(); i++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < grid.size(); i++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int j = 0; j < grid.size(); j++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < grid.size(); i++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:99:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
supertrees.cpp:54:40: warning: control reaches end of non-void function [-Wreturn-type]
54 | vector<vector<int>> ans(grid.size());
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22096 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
180 ms |
22288 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
5 ms |
852 KB |
Output is correct |
17 |
Correct |
106 ms |
12236 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
42 ms |
5788 KB |
Output is correct |
21 |
Correct |
189 ms |
22148 KB |
Output is correct |
22 |
Correct |
167 ms |
22204 KB |
Output is correct |
23 |
Correct |
200 ms |
22076 KB |
Output is correct |
24 |
Correct |
208 ms |
22276 KB |
Output is correct |
25 |
Correct |
78 ms |
12212 KB |
Output is correct |
26 |
Correct |
78 ms |
12288 KB |
Output is correct |
27 |
Correct |
190 ms |
22144 KB |
Output is correct |
28 |
Correct |
172 ms |
22332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
1236 KB |
Output is correct |
9 |
Correct |
171 ms |
22192 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1208 KB |
Output is correct |
13 |
Correct |
176 ms |
22224 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
852 KB |
Output is correct |
17 |
Correct |
90 ms |
12288 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 |
300 KB |
Output is correct |
21 |
Correct |
43 ms |
5896 KB |
Output is correct |
22 |
Correct |
180 ms |
22148 KB |
Output is correct |
23 |
Correct |
168 ms |
22160 KB |
Output is correct |
24 |
Correct |
181 ms |
22076 KB |
Output is correct |
25 |
Correct |
95 ms |
12300 KB |
Output is correct |
26 |
Correct |
77 ms |
12236 KB |
Output is correct |
27 |
Correct |
171 ms |
22108 KB |
Output is correct |
28 |
Correct |
183 ms |
22220 KB |
Output is correct |
29 |
Correct |
75 ms |
12304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
44 ms |
5844 KB |
Output is correct |
5 |
Correct |
176 ms |
22100 KB |
Output is correct |
6 |
Correct |
201 ms |
22212 KB |
Output is correct |
7 |
Correct |
182 ms |
22216 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
44 ms |
5836 KB |
Output is correct |
10 |
Correct |
177 ms |
22108 KB |
Output is correct |
11 |
Correct |
175 ms |
22168 KB |
Output is correct |
12 |
Correct |
183 ms |
22128 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
42 ms |
5880 KB |
Output is correct |
17 |
Correct |
170 ms |
22148 KB |
Output is correct |
18 |
Correct |
172 ms |
22148 KB |
Output is correct |
19 |
Correct |
182 ms |
22156 KB |
Output is correct |
20 |
Correct |
169 ms |
22288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22096 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
180 ms |
22288 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
5 ms |
852 KB |
Output is correct |
17 |
Correct |
106 ms |
12236 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
42 ms |
5788 KB |
Output is correct |
21 |
Correct |
189 ms |
22148 KB |
Output is correct |
22 |
Correct |
167 ms |
22204 KB |
Output is correct |
23 |
Correct |
200 ms |
22076 KB |
Output is correct |
24 |
Correct |
208 ms |
22276 KB |
Output is correct |
25 |
Correct |
78 ms |
12212 KB |
Output is correct |
26 |
Correct |
78 ms |
12288 KB |
Output is correct |
27 |
Correct |
190 ms |
22144 KB |
Output is correct |
28 |
Correct |
172 ms |
22332 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
300 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
171 ms |
22192 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1208 KB |
Output is correct |
41 |
Correct |
176 ms |
22224 KB |
Output is correct |
42 |
Correct |
1 ms |
304 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
6 ms |
852 KB |
Output is correct |
45 |
Correct |
90 ms |
12288 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 |
300 KB |
Output is correct |
49 |
Correct |
43 ms |
5896 KB |
Output is correct |
50 |
Correct |
180 ms |
22148 KB |
Output is correct |
51 |
Correct |
168 ms |
22160 KB |
Output is correct |
52 |
Correct |
181 ms |
22076 KB |
Output is correct |
53 |
Correct |
95 ms |
12300 KB |
Output is correct |
54 |
Correct |
77 ms |
12236 KB |
Output is correct |
55 |
Correct |
171 ms |
22108 KB |
Output is correct |
56 |
Correct |
183 ms |
22220 KB |
Output is correct |
57 |
Correct |
75 ms |
12304 KB |
Output is correct |
58 |
Correct |
1 ms |
296 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
5 ms |
820 KB |
Output is correct |
61 |
Correct |
101 ms |
12392 KB |
Output is correct |
62 |
Correct |
0 ms |
296 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
46 ms |
5904 KB |
Output is correct |
66 |
Correct |
75 ms |
12296 KB |
Output is correct |
67 |
Correct |
75 ms |
12296 KB |
Output is correct |
68 |
Correct |
74 ms |
12292 KB |
Output is correct |
69 |
Correct |
80 ms |
12308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22096 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
180 ms |
22288 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
5 ms |
852 KB |
Output is correct |
17 |
Correct |
106 ms |
12236 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
42 ms |
5788 KB |
Output is correct |
21 |
Correct |
189 ms |
22148 KB |
Output is correct |
22 |
Correct |
167 ms |
22204 KB |
Output is correct |
23 |
Correct |
200 ms |
22076 KB |
Output is correct |
24 |
Correct |
208 ms |
22276 KB |
Output is correct |
25 |
Correct |
78 ms |
12212 KB |
Output is correct |
26 |
Correct |
78 ms |
12288 KB |
Output is correct |
27 |
Correct |
190 ms |
22144 KB |
Output is correct |
28 |
Correct |
172 ms |
22332 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
300 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
171 ms |
22192 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1208 KB |
Output is correct |
41 |
Correct |
176 ms |
22224 KB |
Output is correct |
42 |
Correct |
1 ms |
304 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
6 ms |
852 KB |
Output is correct |
45 |
Correct |
90 ms |
12288 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 |
300 KB |
Output is correct |
49 |
Correct |
43 ms |
5896 KB |
Output is correct |
50 |
Correct |
180 ms |
22148 KB |
Output is correct |
51 |
Correct |
168 ms |
22160 KB |
Output is correct |
52 |
Correct |
181 ms |
22076 KB |
Output is correct |
53 |
Correct |
95 ms |
12300 KB |
Output is correct |
54 |
Correct |
77 ms |
12236 KB |
Output is correct |
55 |
Correct |
171 ms |
22108 KB |
Output is correct |
56 |
Correct |
183 ms |
22220 KB |
Output is correct |
57 |
Correct |
75 ms |
12304 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 |
44 ms |
5844 KB |
Output is correct |
62 |
Correct |
176 ms |
22100 KB |
Output is correct |
63 |
Correct |
201 ms |
22212 KB |
Output is correct |
64 |
Correct |
182 ms |
22216 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
44 ms |
5836 KB |
Output is correct |
67 |
Correct |
177 ms |
22108 KB |
Output is correct |
68 |
Correct |
175 ms |
22168 KB |
Output is correct |
69 |
Correct |
183 ms |
22128 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
42 ms |
5880 KB |
Output is correct |
74 |
Correct |
170 ms |
22148 KB |
Output is correct |
75 |
Correct |
172 ms |
22148 KB |
Output is correct |
76 |
Correct |
182 ms |
22156 KB |
Output is correct |
77 |
Correct |
169 ms |
22288 KB |
Output is correct |
78 |
Correct |
1 ms |
296 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
5 ms |
820 KB |
Output is correct |
81 |
Correct |
101 ms |
12392 KB |
Output is correct |
82 |
Correct |
0 ms |
296 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
46 ms |
5904 KB |
Output is correct |
86 |
Correct |
75 ms |
12296 KB |
Output is correct |
87 |
Correct |
75 ms |
12296 KB |
Output is correct |
88 |
Correct |
74 ms |
12292 KB |
Output is correct |
89 |
Correct |
80 ms |
12308 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
71 ms |
12168 KB |
Output is correct |
94 |
Correct |
0 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
17 ms |
3412 KB |
Output is correct |
98 |
Correct |
70 ms |
12180 KB |
Output is correct |
99 |
Correct |
90 ms |
12168 KB |
Output is correct |
100 |
Correct |
90 ms |
12164 KB |
Output is correct |
101 |
Correct |
71 ms |
12168 KB |
Output is correct |
102 |
Correct |
68 ms |
12236 KB |
Output is correct |
103 |
Correct |
73 ms |
12180 KB |
Output is correct |
104 |
Correct |
68 ms |
12176 KB |
Output is correct |
105 |
Correct |
76 ms |
12160 KB |
Output is correct |