#include "supertrees.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define vi vector<int>
struct Dsu{
vector<int> p, s;
Dsu(int n){
p.resize(n+1);
s.resize(n+1,1);
for(int i = 1; i <= n; ++i ) p[i]=i;
}
int find(int v){
if(p[v] == v)return v;
return p[v]=find(p[v]);
}
void merge(int a, int b){
a = find(a);
b = find(b);
if(a != b){
if(s[a]>s[b]) swap(a,b);
p[a] = b;
s[b] += s[a];
}
}
};
vector<vector<int>> P;
bool solve(vector<int> v, vector<vector<int>> &ans){
if(v.size() <= 1) return 1;
for(int u: v){
for(int x: v){
if(P[u][x] == 0 || P[u][x] == 3){
return 0;
}
}
}
vector<int> cycle;
vector<vector<int>> onepiece;
vector<bool> used(v.size());
for(int i = 0; i < v.size(); ++i){
bool fulltwo = 1;
if(used[i]) continue;
for(int j = i + 1; j < v.size(); ++j){
if(P[v[i]][v[j]] == 1){
if(used[j]){
return 0;
}
fulltwo = 0;
onepiece.pb(vector<int>{v[i], v[j]});
used[i] = used[j] = 1;
for(int k = 0; k < v.size(); ++k){
if(!used[k]){
int co = 0;
for(int c: onepiece.back()){
if(P[c][v[k]] == 1){
++co;
}
}
if(co != 0 && co != onepiece.back().size()){
return 0;
}
if(co == onepiece.back().size()){
onepiece.back().pb(v[k]);
used[k] = 1;
}
}
}
break;
}
}
if(fulltwo) cycle.pb(v[i]);
// else assert(false);
}
for(auto x: onepiece){
for(int j = 0; j + 1 < x.size(); ++j){
ans[x[j]][x[j + 1]] = ans[x[j + 1]][x[j]] = 1;
}
cycle.pb(x[0]);
}
if(cycle.size() == 1) return 1;
if(cycle.size() == 2) return 0;
for(int i = 0; i < cycle.size(); ++i){
ans[cycle[i]][cycle[(i + 1) % cycle.size()]] = ans[cycle[(i + 1) % cycle.size()]][cycle[i]] = 1;
}
return 1;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
P = p;
vector<vector<int>> answer(n, vector<int>(n));
Dsu d(n);
for(int i = 0; i < n; ++i){
for(int j = 1; j < n; ++j){
if(p[i][j] > 0) d.merge(i, j);
}
}
vector<vector<int>> C(n);
for(int i = 0; i < n; ++i){
C[d.find(i)].pb(i);
}
for(auto v: C){
bool ok = solve(v, answer);
if(!ok) return 0;
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'bool solve(std::vector<int>, std::vector<std::vector<int> >&)':
supertrees.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0; i < v.size(); ++i){
| ~~^~~~~~~~~~
supertrees.cpp:46:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int j = i + 1; j < v.size(); ++j){
| ~~^~~~~~~~~~
supertrees.cpp:54:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int k = 0; k < v.size(); ++k){
| ~~^~~~~~~~~~
supertrees.cpp:62:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if(co != 0 && co != onepiece.back().size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:65:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | if(co == onepiece.back().size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:80:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int j = 0; j + 1 < x.size(); ++j){
| ~~~~~~^~~~~~~~~~
supertrees.cpp:87:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int i = 0; i < cycle.size(); ++i){
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
97 ms |
26044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
97 ms |
26044 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1432 KB |
Output is correct |
13 |
Correct |
93 ms |
26124 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 |
54 ms |
16136 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6864 KB |
Output is correct |
21 |
Correct |
96 ms |
25948 KB |
Output is correct |
22 |
Correct |
100 ms |
25940 KB |
Output is correct |
23 |
Correct |
101 ms |
25940 KB |
Output is correct |
24 |
Correct |
100 ms |
26196 KB |
Output is correct |
25 |
Correct |
42 ms |
16044 KB |
Output is correct |
26 |
Correct |
41 ms |
16148 KB |
Output is correct |
27 |
Correct |
100 ms |
26124 KB |
Output is correct |
28 |
Correct |
112 ms |
26128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
1452 KB |
Output is correct |
9 |
Correct |
111 ms |
26144 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1448 KB |
Output is correct |
13 |
Correct |
97 ms |
26132 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
59 ms |
16208 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
24 ms |
6748 KB |
Output is correct |
22 |
Correct |
97 ms |
25940 KB |
Output is correct |
23 |
Correct |
94 ms |
25936 KB |
Output is correct |
24 |
Correct |
107 ms |
25948 KB |
Output is correct |
25 |
Correct |
41 ms |
16152 KB |
Output is correct |
26 |
Correct |
43 ms |
16208 KB |
Output is correct |
27 |
Correct |
97 ms |
26140 KB |
Output is correct |
28 |
Correct |
101 ms |
26136 KB |
Output is correct |
29 |
Correct |
41 ms |
16136 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 |
25 ms |
6748 KB |
Output is correct |
5 |
Correct |
95 ms |
25940 KB |
Output is correct |
6 |
Correct |
95 ms |
25936 KB |
Output is correct |
7 |
Correct |
100 ms |
26116 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
24 ms |
6744 KB |
Output is correct |
10 |
Correct |
97 ms |
25940 KB |
Output is correct |
11 |
Correct |
106 ms |
25940 KB |
Output is correct |
12 |
Correct |
108 ms |
26196 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
25 ms |
6748 KB |
Output is correct |
17 |
Correct |
97 ms |
25936 KB |
Output is correct |
18 |
Correct |
101 ms |
25940 KB |
Output is correct |
19 |
Correct |
96 ms |
25936 KB |
Output is correct |
20 |
Correct |
96 ms |
26192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
97 ms |
26044 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1432 KB |
Output is correct |
13 |
Correct |
93 ms |
26124 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 |
54 ms |
16136 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6864 KB |
Output is correct |
21 |
Correct |
96 ms |
25948 KB |
Output is correct |
22 |
Correct |
100 ms |
25940 KB |
Output is correct |
23 |
Correct |
101 ms |
25940 KB |
Output is correct |
24 |
Correct |
100 ms |
26196 KB |
Output is correct |
25 |
Correct |
42 ms |
16044 KB |
Output is correct |
26 |
Correct |
41 ms |
16148 KB |
Output is correct |
27 |
Correct |
100 ms |
26124 KB |
Output is correct |
28 |
Correct |
112 ms |
26128 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
600 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 |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
1452 KB |
Output is correct |
37 |
Correct |
111 ms |
26144 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
4 ms |
1448 KB |
Output is correct |
41 |
Correct |
97 ms |
26132 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
59 ms |
16208 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
24 ms |
6748 KB |
Output is correct |
50 |
Correct |
97 ms |
25940 KB |
Output is correct |
51 |
Correct |
94 ms |
25936 KB |
Output is correct |
52 |
Correct |
107 ms |
25948 KB |
Output is correct |
53 |
Correct |
41 ms |
16152 KB |
Output is correct |
54 |
Correct |
43 ms |
16208 KB |
Output is correct |
55 |
Correct |
97 ms |
26140 KB |
Output is correct |
56 |
Correct |
101 ms |
26136 KB |
Output is correct |
57 |
Correct |
41 ms |
16136 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
860 KB |
Output is correct |
61 |
Correct |
49 ms |
16212 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 |
24 ms |
6792 KB |
Output is correct |
66 |
Correct |
46 ms |
16208 KB |
Output is correct |
67 |
Correct |
46 ms |
16152 KB |
Output is correct |
68 |
Correct |
44 ms |
16212 KB |
Output is correct |
69 |
Correct |
42 ms |
16152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
97 ms |
26044 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1432 KB |
Output is correct |
13 |
Correct |
93 ms |
26124 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 |
54 ms |
16136 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6864 KB |
Output is correct |
21 |
Correct |
96 ms |
25948 KB |
Output is correct |
22 |
Correct |
100 ms |
25940 KB |
Output is correct |
23 |
Correct |
101 ms |
25940 KB |
Output is correct |
24 |
Correct |
100 ms |
26196 KB |
Output is correct |
25 |
Correct |
42 ms |
16044 KB |
Output is correct |
26 |
Correct |
41 ms |
16148 KB |
Output is correct |
27 |
Correct |
100 ms |
26124 KB |
Output is correct |
28 |
Correct |
112 ms |
26128 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
600 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 |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
1452 KB |
Output is correct |
37 |
Correct |
111 ms |
26144 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
4 ms |
1448 KB |
Output is correct |
41 |
Correct |
97 ms |
26132 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
59 ms |
16208 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
24 ms |
6748 KB |
Output is correct |
50 |
Correct |
97 ms |
25940 KB |
Output is correct |
51 |
Correct |
94 ms |
25936 KB |
Output is correct |
52 |
Correct |
107 ms |
25948 KB |
Output is correct |
53 |
Correct |
41 ms |
16152 KB |
Output is correct |
54 |
Correct |
43 ms |
16208 KB |
Output is correct |
55 |
Correct |
97 ms |
26140 KB |
Output is correct |
56 |
Correct |
101 ms |
26136 KB |
Output is correct |
57 |
Correct |
41 ms |
16136 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 |
25 ms |
6748 KB |
Output is correct |
62 |
Correct |
95 ms |
25940 KB |
Output is correct |
63 |
Correct |
95 ms |
25936 KB |
Output is correct |
64 |
Correct |
100 ms |
26116 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
24 ms |
6744 KB |
Output is correct |
67 |
Correct |
97 ms |
25940 KB |
Output is correct |
68 |
Correct |
106 ms |
25940 KB |
Output is correct |
69 |
Correct |
108 ms |
26196 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
25 ms |
6748 KB |
Output is correct |
74 |
Correct |
97 ms |
25936 KB |
Output is correct |
75 |
Correct |
101 ms |
25940 KB |
Output is correct |
76 |
Correct |
96 ms |
25936 KB |
Output is correct |
77 |
Correct |
96 ms |
26192 KB |
Output is correct |
78 |
Correct |
1 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
2 ms |
860 KB |
Output is correct |
81 |
Correct |
49 ms |
16212 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 |
24 ms |
6792 KB |
Output is correct |
86 |
Correct |
46 ms |
16208 KB |
Output is correct |
87 |
Correct |
46 ms |
16152 KB |
Output is correct |
88 |
Correct |
44 ms |
16212 KB |
Output is correct |
89 |
Correct |
42 ms |
16152 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
860 KB |
Output is correct |
93 |
Correct |
50 ms |
16464 KB |
Output is correct |
94 |
Correct |
0 ms |
344 KB |
Output is correct |
95 |
Correct |
0 ms |
344 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
11 ms |
4420 KB |
Output is correct |
98 |
Correct |
43 ms |
16136 KB |
Output is correct |
99 |
Correct |
50 ms |
16152 KB |
Output is correct |
100 |
Correct |
42 ms |
16208 KB |
Output is correct |
101 |
Correct |
41 ms |
16208 KB |
Output is correct |
102 |
Correct |
43 ms |
16208 KB |
Output is correct |
103 |
Correct |
43 ms |
16212 KB |
Output is correct |
104 |
Correct |
45 ms |
16132 KB |
Output is correct |
105 |
Correct |
54 ms |
16208 KB |
Output is correct |