#include "supertrees.h"
#include "bits/stdc++.h"
using namespace std;
#define vvi vector<vector<int> >
struct union_find{
vector<int> p;
void init(int n){
p.resize(n);
iota(p.begin(),p.end(),0);
}
int find_set(int n){
return (n==p[n]) ? n : p[n]=find_set(p[n]);
}
bool same_set(int a, int b){
return find_set(a)==find_set(b);
}
void union_set(int a, int b){
if(!same_set(a,b)) p[find_set(a)]=find_set(b);
}
};
/*
void init(int n, vector<int> &p){
for(int i=0;i<n;i++) p[i]=i;
}
int find_set(int n, vector<int> &p){
return (n==p[n]) ? n : p[n]=find_set(p[n],p);
}
bool same_set(int a, int b, vector<int> &p){
return find_set(a,p)==find_set(b,p);
}
void union_set(int a, int b, vector<int> &p){
if(!same_set(a,b,p)) p[find_set(a,p)]=find_set(b,p);
}
*/
void join_1(vector<int> &g, vvi &res){
for(int i=1;i<g.size();i++)
res[g[i-1]][g[i]]=res[g[i]][g[i-1]]=1;
}
bool join_2(vector<int> &g, vvi &res){
if(g.size()==1) return true;
if(g.size()<3) return false;
for(int i=0;i<g.size();i++){
int j=(i+1)%g.size();
res[g[i]][g[j]]=res[g[j]][g[i]]=1;
}
return true;
}
bool make_group(int n, vector<int> &g, vvi &v, vvi &res){
//vector<int> p(n);
//init(n,p);
union_find p; p.init(n);
unordered_set<int> mid;
for(int i=0;i<g.size();i++){
int aux=0;
for(int j=0;j<g.size();j++)
if(v[g[i]][g[j]]==1) p.union_set(g[i],g[j]);
else aux++;
if(aux==g.size()-1) mid.insert(g[i]);
}
vvi one(n);
for(int i=0;i<g.size();i++){
if(mid.find(g[i])==mid.end())
one[p.find_set(g[i])].push_back(g[i]);
if(g[i]==p.find_set(g[i])) mid.insert(g[i]);
}
vector<int> v_mid(mid.begin(),mid.end());
for(int i=0;i<n;i++)
if(one[i].size()) join_1(one[i],res);
return join_2(v_mid,res);
}
int construct(vvi v){
int n=v.size();
//vector<int> p(n);
//init(n,p);
union_find p; p.init(n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(v[i][j]) p.union_set(i,j);
vvi g(n);
for(int i=0;i<n;i++)
g[p.find_set(i)].push_back(i);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if((p.same_set(i,j) && v[i][j]==0) || v[i][j]==3) return 0;
vvi res(n,vector<int> (n,0));
for(int i=0;i<n;i++)
if(g[i].size()>0)
if(!make_group(n,g[i],v,res)) return 0;
build(res);
return 1;
}
Compilation message
supertrees.cpp: In function 'void join_1(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:40:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=1;i<g.size();i++)
| ~^~~~~~~~~
supertrees.cpp: In function 'bool join_2(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
supertrees.cpp: In function 'bool make_group(int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:59:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
supertrees.cpp:61:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int j=0;j<g.size();j++)
| ~^~~~~~~~~
supertrees.cpp:64:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | if(aux==g.size()-1) mid.insert(g[i]);
| ~~~^~~~~~~~~~~~
supertrees.cpp:67:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=0;i<g.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
207 ms |
22148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
207 ms |
22148 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 |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
188 ms |
22332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
692 KB |
Output is correct |
17 |
Correct |
85 ms |
8592 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
64 ms |
5836 KB |
Output is correct |
21 |
Correct |
171 ms |
22332 KB |
Output is correct |
22 |
Correct |
174 ms |
22448 KB |
Output is correct |
23 |
Correct |
182 ms |
22268 KB |
Output is correct |
24 |
Correct |
202 ms |
22328 KB |
Output is correct |
25 |
Correct |
80 ms |
8556 KB |
Output is correct |
26 |
Correct |
67 ms |
8592 KB |
Output is correct |
27 |
Correct |
227 ms |
22524 KB |
Output is correct |
28 |
Correct |
181 ms |
22296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
9 ms |
1180 KB |
Output is correct |
9 |
Correct |
206 ms |
22256 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
201 ms |
22276 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
78 ms |
8496 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
45 ms |
5836 KB |
Output is correct |
22 |
Correct |
191 ms |
22264 KB |
Output is correct |
23 |
Correct |
182 ms |
22228 KB |
Output is correct |
24 |
Correct |
180 ms |
22432 KB |
Output is correct |
25 |
Correct |
85 ms |
12592 KB |
Output is correct |
26 |
Correct |
74 ms |
8604 KB |
Output is correct |
27 |
Correct |
165 ms |
22316 KB |
Output is correct |
28 |
Correct |
227 ms |
22240 KB |
Output is correct |
29 |
Correct |
71 ms |
12572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
56 ms |
5896 KB |
Output is correct |
5 |
Correct |
181 ms |
22344 KB |
Output is correct |
6 |
Correct |
219 ms |
22260 KB |
Output is correct |
7 |
Correct |
185 ms |
22348 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
49 ms |
5956 KB |
Output is correct |
10 |
Correct |
183 ms |
22312 KB |
Output is correct |
11 |
Correct |
223 ms |
22320 KB |
Output is correct |
12 |
Correct |
203 ms |
22376 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
44 ms |
5940 KB |
Output is correct |
17 |
Correct |
183 ms |
22336 KB |
Output is correct |
18 |
Correct |
169 ms |
22340 KB |
Output is correct |
19 |
Correct |
184 ms |
22316 KB |
Output is correct |
20 |
Correct |
170 ms |
22272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
207 ms |
22148 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 |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
188 ms |
22332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
692 KB |
Output is correct |
17 |
Correct |
85 ms |
8592 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
64 ms |
5836 KB |
Output is correct |
21 |
Correct |
171 ms |
22332 KB |
Output is correct |
22 |
Correct |
174 ms |
22448 KB |
Output is correct |
23 |
Correct |
182 ms |
22268 KB |
Output is correct |
24 |
Correct |
202 ms |
22328 KB |
Output is correct |
25 |
Correct |
80 ms |
8556 KB |
Output is correct |
26 |
Correct |
67 ms |
8592 KB |
Output is correct |
27 |
Correct |
227 ms |
22524 KB |
Output is correct |
28 |
Correct |
181 ms |
22296 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
296 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
296 KB |
Output is correct |
35 |
Correct |
1 ms |
304 KB |
Output is correct |
36 |
Correct |
9 ms |
1180 KB |
Output is correct |
37 |
Correct |
206 ms |
22256 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
296 KB |
Output is correct |
40 |
Correct |
11 ms |
1260 KB |
Output is correct |
41 |
Correct |
201 ms |
22276 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
78 ms |
8496 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
45 ms |
5836 KB |
Output is correct |
50 |
Correct |
191 ms |
22264 KB |
Output is correct |
51 |
Correct |
182 ms |
22228 KB |
Output is correct |
52 |
Correct |
180 ms |
22432 KB |
Output is correct |
53 |
Correct |
85 ms |
12592 KB |
Output is correct |
54 |
Correct |
74 ms |
8604 KB |
Output is correct |
55 |
Correct |
165 ms |
22316 KB |
Output is correct |
56 |
Correct |
227 ms |
22240 KB |
Output is correct |
57 |
Correct |
71 ms |
12572 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
596 KB |
Output is correct |
61 |
Correct |
76 ms |
8616 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
48 ms |
5900 KB |
Output is correct |
66 |
Correct |
78 ms |
8524 KB |
Output is correct |
67 |
Correct |
67 ms |
8600 KB |
Output is correct |
68 |
Correct |
66 ms |
8608 KB |
Output is correct |
69 |
Correct |
72 ms |
12512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
207 ms |
22148 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 |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
188 ms |
22332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
692 KB |
Output is correct |
17 |
Correct |
85 ms |
8592 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
64 ms |
5836 KB |
Output is correct |
21 |
Correct |
171 ms |
22332 KB |
Output is correct |
22 |
Correct |
174 ms |
22448 KB |
Output is correct |
23 |
Correct |
182 ms |
22268 KB |
Output is correct |
24 |
Correct |
202 ms |
22328 KB |
Output is correct |
25 |
Correct |
80 ms |
8556 KB |
Output is correct |
26 |
Correct |
67 ms |
8592 KB |
Output is correct |
27 |
Correct |
227 ms |
22524 KB |
Output is correct |
28 |
Correct |
181 ms |
22296 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
296 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
296 KB |
Output is correct |
35 |
Correct |
1 ms |
304 KB |
Output is correct |
36 |
Correct |
9 ms |
1180 KB |
Output is correct |
37 |
Correct |
206 ms |
22256 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
296 KB |
Output is correct |
40 |
Correct |
11 ms |
1260 KB |
Output is correct |
41 |
Correct |
201 ms |
22276 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
78 ms |
8496 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
45 ms |
5836 KB |
Output is correct |
50 |
Correct |
191 ms |
22264 KB |
Output is correct |
51 |
Correct |
182 ms |
22228 KB |
Output is correct |
52 |
Correct |
180 ms |
22432 KB |
Output is correct |
53 |
Correct |
85 ms |
12592 KB |
Output is correct |
54 |
Correct |
74 ms |
8604 KB |
Output is correct |
55 |
Correct |
165 ms |
22316 KB |
Output is correct |
56 |
Correct |
227 ms |
22240 KB |
Output is correct |
57 |
Correct |
71 ms |
12572 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
308 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
56 ms |
5896 KB |
Output is correct |
62 |
Correct |
181 ms |
22344 KB |
Output is correct |
63 |
Correct |
219 ms |
22260 KB |
Output is correct |
64 |
Correct |
185 ms |
22348 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
49 ms |
5956 KB |
Output is correct |
67 |
Correct |
183 ms |
22312 KB |
Output is correct |
68 |
Correct |
223 ms |
22320 KB |
Output is correct |
69 |
Correct |
203 ms |
22376 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
300 KB |
Output is correct |
73 |
Correct |
44 ms |
5940 KB |
Output is correct |
74 |
Correct |
183 ms |
22336 KB |
Output is correct |
75 |
Correct |
169 ms |
22340 KB |
Output is correct |
76 |
Correct |
184 ms |
22316 KB |
Output is correct |
77 |
Correct |
170 ms |
22272 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
596 KB |
Output is correct |
81 |
Correct |
76 ms |
8616 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
48 ms |
5900 KB |
Output is correct |
86 |
Correct |
78 ms |
8524 KB |
Output is correct |
87 |
Correct |
67 ms |
8600 KB |
Output is correct |
88 |
Correct |
66 ms |
8608 KB |
Output is correct |
89 |
Correct |
72 ms |
12512 KB |
Output is correct |
90 |
Correct |
1 ms |
296 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
7 ms |
596 KB |
Output is correct |
93 |
Correct |
82 ms |
8660 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
18 ms |
2348 KB |
Output is correct |
98 |
Correct |
73 ms |
8500 KB |
Output is correct |
99 |
Correct |
73 ms |
8480 KB |
Output is correct |
100 |
Correct |
72 ms |
8484 KB |
Output is correct |
101 |
Correct |
81 ms |
8544 KB |
Output is correct |
102 |
Correct |
72 ms |
8548 KB |
Output is correct |
103 |
Correct |
72 ms |
8596 KB |
Output is correct |
104 |
Correct |
69 ms |
8580 KB |
Output is correct |
105 |
Correct |
88 ms |
8596 KB |
Output is correct |