#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
int p[1000];
void init() {
memset(p,-1,sizeof(p));
}
int find(int a) {
if (p[a]<0) {
return a;
}
return p[a]=find(p[a]);
}
void merge(int a,int b) {
a=find(a);
b=find(b);
if (a==b) {
return;
}
p[b]=a;
}
};
bool used[1000];
int construct(vector<vector<int>> v) {
int n = v.size();
vector<vector<int>> ret(n,vector<int>(n));
UnionFind uf;
uf.init();
bool flag3=false;
for (int i = 0; i < n; i++) {
for(int j=0;j<n;j++) {
if (v[i][j]!=0) {
uf.merge(i,j);
if (v[i][j]==3) {
flag3=true;
}
}
}
}
if (flag3) {
if (n!=4) {
return 0;
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if (i!=j&&v[i][j]!=3) {
return 0;
}
if (i==j&&v[i][j]!=1) {
return 0;
}
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if (i==j) {
ret[i][j]=0;
}
ret[i][j]=1;
}
}
ret[0][3]=0;
ret[3][0]=0;
build(ret);
return 1;
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if (v[i][j]!=0) {
if (uf.find(i)!=uf.find(j)) {
return 0;
}
}
else {
if (uf.find(i)==uf.find(j)) {
return 0;
}
}
}
}
for(int i=0;i<n;i++) {
if (!used[i]) {
vector<int> save;
save.push_back(i);
used[i]=true;
for(int j=i+1;j<n;j++) {
if (uf.find(j)==uf.find(i)) {
save.push_back(j);
used[j]=true;
}
}
UnionFind uf2;
uf2.init();
for(int j=0;j<save.size();j++) {
for(int k=0;k<save.size();k++) {
if (v[save[j]][save[k]]==1) {
uf2.merge(save[j],save[k]);
}
}
}
for(int j=0;j<save.size();j++) {
for(int k=0;k<save.size();k++) {
if (v[save[j]][save[k]]!=1) {
if (uf2.find(save[j])==uf2.find(save[k])) {
return 0;
}
}
}
}
bool used2[1000];
memset(used2,0,sizeof(used2));
vector<int> cycle;
for(int j=0;j<save.size();j++) {
if (!used2[save[j]]) {
used2[save[j]]=true;
cycle.push_back(save[j]);
for(int k=j+1;k<save.size();k++) {
if (!used2[save[k]]&&uf2.find(save[j])==uf2.find(save[k])) {
used2[save[k]]=true;
ret[save[j]][save[k]]=1;
ret[save[k]][save[j]]=1;
}
}
}
}
if (cycle.size()==2) {
return 0;
}
if (cycle.size()==1) {
continue;
}
for(int j=0;j+1<cycle.size();j++) {
ret[cycle[j]][cycle[j+1]]=1;
ret[cycle[j+1]][cycle[j]]=1;
}
ret[cycle.back()][cycle[0]]=1;
ret[cycle[0]][cycle.back()]=1;
}
}
build(ret);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:99:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int j=0;j<save.size();j++) {
| ~^~~~~~~~~~~~
supertrees.cpp:100:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int k=0;k<save.size();k++) {
| ~^~~~~~~~~~~~
supertrees.cpp:106:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int j=0;j<save.size();j++) {
| ~^~~~~~~~~~~~
supertrees.cpp:107:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int k=0;k<save.size();k++) {
| ~^~~~~~~~~~~~
supertrees.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(int j=0;j<save.size();j++) {
| ~^~~~~~~~~~~~
supertrees.cpp:122:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(int k=j+1;k<save.size();k++) {
| ~^~~~~~~~~~~~
supertrees.cpp:137:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int j=0;j+1<cycle.size();j++) {
| ~~~^~~~~~~~~~~~~
# |
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 |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
253 ms |
22136 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 |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
253 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 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 |
10 ms |
1152 KB |
Output is correct |
13 |
Correct |
259 ms |
22208 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 |
768 KB |
Output is correct |
17 |
Correct |
136 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
60 ms |
5752 KB |
Output is correct |
21 |
Correct |
240 ms |
22264 KB |
Output is correct |
22 |
Correct |
241 ms |
22136 KB |
Output is correct |
23 |
Correct |
285 ms |
22136 KB |
Output is correct |
24 |
Correct |
246 ms |
22136 KB |
Output is correct |
25 |
Correct |
113 ms |
12152 KB |
Output is correct |
26 |
Correct |
98 ms |
12120 KB |
Output is correct |
27 |
Correct |
260 ms |
22136 KB |
Output is correct |
28 |
Correct |
238 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 |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
1152 KB |
Output is correct |
9 |
Correct |
240 ms |
22076 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
17 ms |
1152 KB |
Output is correct |
13 |
Correct |
283 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 |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
115 ms |
12112 KB |
Output is correct |
18 |
Correct |
1 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 |
60 ms |
5752 KB |
Output is correct |
22 |
Correct |
283 ms |
22136 KB |
Output is correct |
23 |
Correct |
243 ms |
22116 KB |
Output is correct |
24 |
Correct |
286 ms |
22136 KB |
Output is correct |
25 |
Correct |
104 ms |
12152 KB |
Output is correct |
26 |
Correct |
102 ms |
12124 KB |
Output is correct |
27 |
Correct |
248 ms |
22136 KB |
Output is correct |
28 |
Correct |
259 ms |
22136 KB |
Output is correct |
29 |
Correct |
105 ms |
12152 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 |
61 ms |
5752 KB |
Output is correct |
5 |
Correct |
248 ms |
22136 KB |
Output is correct |
6 |
Correct |
241 ms |
22136 KB |
Output is correct |
7 |
Correct |
268 ms |
22136 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
59 ms |
5752 KB |
Output is correct |
10 |
Correct |
248 ms |
22136 KB |
Output is correct |
11 |
Correct |
259 ms |
22136 KB |
Output is correct |
12 |
Correct |
274 ms |
22164 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
62 ms |
5752 KB |
Output is correct |
17 |
Correct |
251 ms |
22100 KB |
Output is correct |
18 |
Correct |
246 ms |
22152 KB |
Output is correct |
19 |
Correct |
259 ms |
22136 KB |
Output is correct |
20 |
Correct |
240 ms |
22076 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 |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
253 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 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 |
10 ms |
1152 KB |
Output is correct |
13 |
Correct |
259 ms |
22208 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 |
768 KB |
Output is correct |
17 |
Correct |
136 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
60 ms |
5752 KB |
Output is correct |
21 |
Correct |
240 ms |
22264 KB |
Output is correct |
22 |
Correct |
241 ms |
22136 KB |
Output is correct |
23 |
Correct |
285 ms |
22136 KB |
Output is correct |
24 |
Correct |
246 ms |
22136 KB |
Output is correct |
25 |
Correct |
113 ms |
12152 KB |
Output is correct |
26 |
Correct |
98 ms |
12120 KB |
Output is correct |
27 |
Correct |
260 ms |
22136 KB |
Output is correct |
28 |
Correct |
238 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 |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
240 ms |
22076 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
17 ms |
1152 KB |
Output is correct |
41 |
Correct |
283 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
115 ms |
12112 KB |
Output is correct |
46 |
Correct |
1 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 |
60 ms |
5752 KB |
Output is correct |
50 |
Correct |
283 ms |
22136 KB |
Output is correct |
51 |
Correct |
243 ms |
22116 KB |
Output is correct |
52 |
Correct |
286 ms |
22136 KB |
Output is correct |
53 |
Correct |
104 ms |
12152 KB |
Output is correct |
54 |
Correct |
102 ms |
12124 KB |
Output is correct |
55 |
Correct |
248 ms |
22136 KB |
Output is correct |
56 |
Correct |
259 ms |
22136 KB |
Output is correct |
57 |
Correct |
105 ms |
12152 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
8 ms |
768 KB |
Output is correct |
61 |
Correct |
113 ms |
12152 KB |
Output is correct |
62 |
Correct |
0 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 |
Correct |
60 ms |
5752 KB |
Output is correct |
66 |
Correct |
100 ms |
12152 KB |
Output is correct |
67 |
Correct |
100 ms |
12152 KB |
Output is correct |
68 |
Correct |
100 ms |
12120 KB |
Output is correct |
69 |
Correct |
102 ms |
12152 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 |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
253 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 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 |
10 ms |
1152 KB |
Output is correct |
13 |
Correct |
259 ms |
22208 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 |
768 KB |
Output is correct |
17 |
Correct |
136 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
60 ms |
5752 KB |
Output is correct |
21 |
Correct |
240 ms |
22264 KB |
Output is correct |
22 |
Correct |
241 ms |
22136 KB |
Output is correct |
23 |
Correct |
285 ms |
22136 KB |
Output is correct |
24 |
Correct |
246 ms |
22136 KB |
Output is correct |
25 |
Correct |
113 ms |
12152 KB |
Output is correct |
26 |
Correct |
98 ms |
12120 KB |
Output is correct |
27 |
Correct |
260 ms |
22136 KB |
Output is correct |
28 |
Correct |
238 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 |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
240 ms |
22076 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
17 ms |
1152 KB |
Output is correct |
41 |
Correct |
283 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
115 ms |
12112 KB |
Output is correct |
46 |
Correct |
1 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 |
60 ms |
5752 KB |
Output is correct |
50 |
Correct |
283 ms |
22136 KB |
Output is correct |
51 |
Correct |
243 ms |
22116 KB |
Output is correct |
52 |
Correct |
286 ms |
22136 KB |
Output is correct |
53 |
Correct |
104 ms |
12152 KB |
Output is correct |
54 |
Correct |
102 ms |
12124 KB |
Output is correct |
55 |
Correct |
248 ms |
22136 KB |
Output is correct |
56 |
Correct |
259 ms |
22136 KB |
Output is correct |
57 |
Correct |
105 ms |
12152 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
0 ms |
256 KB |
Output is correct |
61 |
Correct |
61 ms |
5752 KB |
Output is correct |
62 |
Correct |
248 ms |
22136 KB |
Output is correct |
63 |
Correct |
241 ms |
22136 KB |
Output is correct |
64 |
Correct |
268 ms |
22136 KB |
Output is correct |
65 |
Correct |
1 ms |
256 KB |
Output is correct |
66 |
Correct |
59 ms |
5752 KB |
Output is correct |
67 |
Correct |
248 ms |
22136 KB |
Output is correct |
68 |
Correct |
259 ms |
22136 KB |
Output is correct |
69 |
Correct |
274 ms |
22164 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
0 ms |
384 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
62 ms |
5752 KB |
Output is correct |
74 |
Correct |
251 ms |
22100 KB |
Output is correct |
75 |
Correct |
246 ms |
22152 KB |
Output is correct |
76 |
Correct |
259 ms |
22136 KB |
Output is correct |
77 |
Correct |
240 ms |
22076 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
0 ms |
256 KB |
Output is correct |
80 |
Correct |
8 ms |
768 KB |
Output is correct |
81 |
Correct |
113 ms |
12152 KB |
Output is correct |
82 |
Correct |
0 ms |
256 KB |
Output is correct |
83 |
Correct |
0 ms |
256 KB |
Output is correct |
84 |
Correct |
1 ms |
256 KB |
Output is correct |
85 |
Correct |
60 ms |
5752 KB |
Output is correct |
86 |
Correct |
100 ms |
12152 KB |
Output is correct |
87 |
Correct |
100 ms |
12152 KB |
Output is correct |
88 |
Correct |
100 ms |
12120 KB |
Output is correct |
89 |
Correct |
102 ms |
12152 KB |
Output is correct |
90 |
Correct |
0 ms |
256 KB |
Output is correct |
91 |
Correct |
0 ms |
256 KB |
Output is correct |
92 |
Correct |
5 ms |
768 KB |
Output is correct |
93 |
Correct |
113 ms |
12120 KB |
Output is correct |
94 |
Correct |
0 ms |
256 KB |
Output is correct |
95 |
Correct |
0 ms |
256 KB |
Output is correct |
96 |
Correct |
0 ms |
256 KB |
Output is correct |
97 |
Correct |
25 ms |
3328 KB |
Output is correct |
98 |
Correct |
103 ms |
12156 KB |
Output is correct |
99 |
Correct |
101 ms |
12152 KB |
Output is correct |
100 |
Correct |
99 ms |
12152 KB |
Output is correct |
101 |
Correct |
99 ms |
12152 KB |
Output is correct |
102 |
Correct |
110 ms |
12280 KB |
Output is correct |
103 |
Correct |
110 ms |
12152 KB |
Output is correct |
104 |
Correct |
109 ms |
12120 KB |
Output is correct |
105 |
Correct |
115 ms |
12152 KB |
Output is correct |