#include<bits/stdc++.h>
using namespace std;
#include "supertrees.h"
#define N 1005
int p[N],pp[N];
vector<int> v;
vector<vector<int>> a,b;
bitset<N> vis,ch;
int fp(int v){return (v==p[v])?v:p[v]=fp(p[v]);}
int fpp(int v){return (v==pp[v])?v:pp[v]=fpp(pp[v]);}
bool sol(int n){
int i,j,cy=0,head,num;
vector<int> gr[N],u;
ch=0;
iota(pp,pp+n,0);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
num=a[v[i]][v[j]];
if(num==1){
if(fpp(i)!=fpp(j))pp[fpp(i)]=fpp(j);
}
else if(!cy){
cy=num;
}
else if(cy!=num)return false;
}
}
for(i=0;i<n;i++)gr[fpp(i)].push_back(v[i]);
if(!cy){
for(i=1;i<n;i++)b[v[i]][v[i-1]]=b[v[i-1]][v[i]]=1;
return true;
}
else{
for(i=0;i<n;i++){
if(ch[fpp(i)])continue;
head=fpp(i);
u.push_back(v[head]);
ch[head]=true;
for(auto x:gr[head]){
for(j=0;j<n;j++){
if(fpp(j)==head){
if(a[x][v[j]]!=1)return false;
}
else{
if(a[x][v[j]]!=cy)return false;
}
}
}
}
for(i=0;i<n;i++)
for(j=1;j<gr[i].size();j++)b[gr[i][j]][gr[i][j-1]]=b[gr[i][j-1]][gr[i][j]]=1;
if(cy==2){
if(u.size()<3)return false;
for(i=1;i<u.size();i++)b[u[i]][u[i-1]]=b[u[i-1]][u[i]]=1;
b[u[0]][u.back()]=b[u.back()][u[0]]=1;
return true;
}
else{
if(u.size()<4)return false;
for(i=1;i<u.size();i++)b[u[i]][u[i-1]]=b[u[i-1]][u[i]]=1;
b[u[0]][u.back()]=b[u.back()][u[0]]=1;
b[u[0]][u[2]]=b[u[2]][u[0]]=1;
return true;
}
}
}
int construct(vector<vector<int>> input) {
a=input;
int n=a.size(),i,j,m;
bool flag;
iota(p,p+n,0);
for(i=0;i<n;i++){
b.push_back({});
b[i].resize(n);
for(j=0;j<n;j++){
if(a[i][j]==3)return 0;
if(a[i][j]){
if(fp(i)==fp(j))continue;
p[fp(i)]=fp(j);
}
else if(fp(i)==fp(j))return 0;
}
}
for(i=0;i<n;i++){
if(!vis[fp(i)]){
vis[fp(i)]=true;
for(j=0;j<n;j++)if(fp(i)==fp(j))v.push_back(j);
m=v.size();
flag=sol(m);
v.clear();
if(!flag)return 0;
}
}
build(b);
return 1;
}
Compilation message
supertrees.cpp: In function 'bool sol(int)':
supertrees.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(j=1;j<gr[i].size();j++)b[gr[i][j]][gr[i][j-1]]=b[gr[i][j-1]][gr[i][j]]=1;
| ~^~~~~~~~~~~~~
supertrees.cpp:58:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(i=1;i<u.size();i++)b[u[i]][u[i-1]]=b[u[i-1]][u[i]]=1;
| ~^~~~~~~~~
supertrees.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(i=1;i<u.size();i++)b[u[i]][u[i-1]]=b[u[i-1]][u[i]]=1;
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1352 KB |
Output is correct |
7 |
Correct |
163 ms |
26004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1352 KB |
Output is correct |
7 |
Correct |
163 ms |
26004 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1356 KB |
Output is correct |
13 |
Correct |
167 ms |
25924 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
72 ms |
12036 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
43 ms |
6664 KB |
Output is correct |
21 |
Correct |
173 ms |
25980 KB |
Output is correct |
22 |
Correct |
173 ms |
25980 KB |
Output is correct |
23 |
Correct |
182 ms |
25924 KB |
Output is correct |
24 |
Correct |
173 ms |
25984 KB |
Output is correct |
25 |
Correct |
82 ms |
15304 KB |
Output is correct |
26 |
Correct |
67 ms |
13188 KB |
Output is correct |
27 |
Correct |
175 ms |
25988 KB |
Output is correct |
28 |
Correct |
174 ms |
25980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
7 ms |
1356 KB |
Output is correct |
9 |
Correct |
170 ms |
25996 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1356 KB |
Output is correct |
13 |
Correct |
165 ms |
25996 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
80 ms |
12096 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
43 ms |
6708 KB |
Output is correct |
22 |
Correct |
204 ms |
26140 KB |
Output is correct |
23 |
Correct |
172 ms |
26148 KB |
Output is correct |
24 |
Correct |
176 ms |
26164 KB |
Output is correct |
25 |
Correct |
74 ms |
16164 KB |
Output is correct |
26 |
Correct |
68 ms |
12440 KB |
Output is correct |
27 |
Correct |
170 ms |
26056 KB |
Output is correct |
28 |
Correct |
189 ms |
26136 KB |
Output is correct |
29 |
Correct |
77 ms |
16128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
44 ms |
6724 KB |
Output is correct |
5 |
Correct |
190 ms |
25992 KB |
Output is correct |
6 |
Correct |
176 ms |
25996 KB |
Output is correct |
7 |
Correct |
201 ms |
25924 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
45 ms |
6760 KB |
Output is correct |
10 |
Correct |
183 ms |
26272 KB |
Output is correct |
11 |
Correct |
185 ms |
26200 KB |
Output is correct |
12 |
Correct |
180 ms |
26376 KB |
Output is correct |
13 |
Correct |
1 ms |
248 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
44 ms |
6864 KB |
Output is correct |
17 |
Correct |
169 ms |
26168 KB |
Output is correct |
18 |
Correct |
171 ms |
26168 KB |
Output is correct |
19 |
Correct |
179 ms |
26440 KB |
Output is correct |
20 |
Correct |
171 ms |
26192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1352 KB |
Output is correct |
7 |
Correct |
163 ms |
26004 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1356 KB |
Output is correct |
13 |
Correct |
167 ms |
25924 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
72 ms |
12036 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
43 ms |
6664 KB |
Output is correct |
21 |
Correct |
173 ms |
25980 KB |
Output is correct |
22 |
Correct |
173 ms |
25980 KB |
Output is correct |
23 |
Correct |
182 ms |
25924 KB |
Output is correct |
24 |
Correct |
173 ms |
25984 KB |
Output is correct |
25 |
Correct |
82 ms |
15304 KB |
Output is correct |
26 |
Correct |
67 ms |
13188 KB |
Output is correct |
27 |
Correct |
175 ms |
25988 KB |
Output is correct |
28 |
Correct |
174 ms |
25980 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
7 ms |
1356 KB |
Output is correct |
37 |
Correct |
170 ms |
25996 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
7 ms |
1356 KB |
Output is correct |
41 |
Correct |
165 ms |
25996 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
3 ms |
716 KB |
Output is correct |
45 |
Correct |
80 ms |
12096 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
43 ms |
6708 KB |
Output is correct |
50 |
Correct |
204 ms |
26140 KB |
Output is correct |
51 |
Correct |
172 ms |
26148 KB |
Output is correct |
52 |
Correct |
176 ms |
26164 KB |
Output is correct |
53 |
Correct |
74 ms |
16164 KB |
Output is correct |
54 |
Correct |
68 ms |
12440 KB |
Output is correct |
55 |
Correct |
170 ms |
26056 KB |
Output is correct |
56 |
Correct |
189 ms |
26136 KB |
Output is correct |
57 |
Correct |
77 ms |
16128 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
4 ms |
792 KB |
Output is correct |
61 |
Correct |
71 ms |
12284 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
46 ms |
6852 KB |
Output is correct |
66 |
Correct |
73 ms |
12448 KB |
Output is correct |
67 |
Correct |
69 ms |
12444 KB |
Output is correct |
68 |
Correct |
78 ms |
14084 KB |
Output is correct |
69 |
Correct |
75 ms |
16284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1352 KB |
Output is correct |
7 |
Correct |
163 ms |
26004 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
1356 KB |
Output is correct |
13 |
Correct |
167 ms |
25924 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
72 ms |
12036 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
43 ms |
6664 KB |
Output is correct |
21 |
Correct |
173 ms |
25980 KB |
Output is correct |
22 |
Correct |
173 ms |
25980 KB |
Output is correct |
23 |
Correct |
182 ms |
25924 KB |
Output is correct |
24 |
Correct |
173 ms |
25984 KB |
Output is correct |
25 |
Correct |
82 ms |
15304 KB |
Output is correct |
26 |
Correct |
67 ms |
13188 KB |
Output is correct |
27 |
Correct |
175 ms |
25988 KB |
Output is correct |
28 |
Correct |
174 ms |
25980 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
7 ms |
1356 KB |
Output is correct |
37 |
Correct |
170 ms |
25996 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
7 ms |
1356 KB |
Output is correct |
41 |
Correct |
165 ms |
25996 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
3 ms |
716 KB |
Output is correct |
45 |
Correct |
80 ms |
12096 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
43 ms |
6708 KB |
Output is correct |
50 |
Correct |
204 ms |
26140 KB |
Output is correct |
51 |
Correct |
172 ms |
26148 KB |
Output is correct |
52 |
Correct |
176 ms |
26164 KB |
Output is correct |
53 |
Correct |
74 ms |
16164 KB |
Output is correct |
54 |
Correct |
68 ms |
12440 KB |
Output is correct |
55 |
Correct |
170 ms |
26056 KB |
Output is correct |
56 |
Correct |
189 ms |
26136 KB |
Output is correct |
57 |
Correct |
77 ms |
16128 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
44 ms |
6724 KB |
Output is correct |
62 |
Correct |
190 ms |
25992 KB |
Output is correct |
63 |
Correct |
176 ms |
25996 KB |
Output is correct |
64 |
Correct |
201 ms |
25924 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
45 ms |
6760 KB |
Output is correct |
67 |
Correct |
183 ms |
26272 KB |
Output is correct |
68 |
Correct |
185 ms |
26200 KB |
Output is correct |
69 |
Correct |
180 ms |
26376 KB |
Output is correct |
70 |
Correct |
1 ms |
248 KB |
Output is correct |
71 |
Correct |
1 ms |
296 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
44 ms |
6864 KB |
Output is correct |
74 |
Correct |
169 ms |
26168 KB |
Output is correct |
75 |
Correct |
171 ms |
26168 KB |
Output is correct |
76 |
Correct |
179 ms |
26440 KB |
Output is correct |
77 |
Correct |
171 ms |
26192 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
4 ms |
792 KB |
Output is correct |
81 |
Correct |
71 ms |
12284 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
0 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
46 ms |
6852 KB |
Output is correct |
86 |
Correct |
73 ms |
12448 KB |
Output is correct |
87 |
Correct |
69 ms |
12444 KB |
Output is correct |
88 |
Correct |
78 ms |
14084 KB |
Output is correct |
89 |
Correct |
75 ms |
16284 KB |
Output is correct |
90 |
Correct |
0 ms |
300 KB |
Output is correct |
91 |
Correct |
0 ms |
204 KB |
Output is correct |
92 |
Correct |
4 ms |
792 KB |
Output is correct |
93 |
Correct |
71 ms |
12304 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
0 ms |
292 KB |
Output is correct |
96 |
Correct |
0 ms |
204 KB |
Output is correct |
97 |
Correct |
17 ms |
3496 KB |
Output is correct |
98 |
Correct |
75 ms |
12556 KB |
Output is correct |
99 |
Correct |
72 ms |
12928 KB |
Output is correct |
100 |
Correct |
72 ms |
12684 KB |
Output is correct |
101 |
Correct |
70 ms |
13708 KB |
Output is correct |
102 |
Correct |
64 ms |
12332 KB |
Output is correct |
103 |
Correct |
66 ms |
14064 KB |
Output is correct |
104 |
Correct |
62 ms |
14064 KB |
Output is correct |
105 |
Correct |
73 ms |
13992 KB |
Output is correct |