#include "supertrees.h"
#include <vector>
using namespace std;
const int maxn = 1e3+10;
int pai[maxn], sz[maxn];
int find(int x){ return pai[x] = (pai[x] == x ? x : find(pai[x])); }
void join(int a, int b){
a = find(a), b = find(b);
if(a == b) return;
if(sz[a] > sz[b]) swap(a, b);
sz[b] += sz[a];
pai[a] = b;
}
int construct(vector<vector<int> > p) {
int n = p.size();
vector<vector<int> > ans;
for(int i=0; i<n; i++){
vector<int> row;
for(int j=0; j<n; j++){
row.push_back(0);
}
ans.push_back(row);
}
for(int i=0; i<n; i++){
pai[i] = i;
sz[i] = 1;
}
bool ok = 1;
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(p[i][j]) join(i, j);
if(p[i][j] == 3) ok = 0;
}
}
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(p[i][j] == 0){
if(find(i) == find(j)) ok = 0;
}
}
}
if(!ok) return 0;
vector<int> cc[n];
for(int i=0; i<n; i++) cc[find(i)].push_back(i);
for(int i=0; i<n; i++){
if(cc[i].empty() or !ok) continue;
for(int j=0; j<n; j++){
pai[j] = j;
sz[j] = 1;
}
for(auto x : cc[i]){
for(auto y : cc[i]){
if(p[x][y] == 1){
join(x, y);
}
}
}
for(auto x : cc[i]){
for(auto y : cc[i]){
if(p[x][y] == 2){
if(find(x) == find(y)) ok = 0;
}
}
}
if(!ok) continue;
vector<int> v;
for(auto j : cc[i]){
int rep = find(j);
if(rep == j) v.push_back(j);
else ans[j][rep] = ans[rep][j] = 1;
for(auto wtf : cc[i]){
int rep2 = find(wtf);
if(rep2 == rep and p[j][wtf] == 2) ok = 0;
}
}
if(v.size() == 1) continue;
if(v.size() == 2) ok = 0;
for(int j=0; j+1<v.size(); j++){
if(p[v[j]][v[j+1]] != 2) continue;
ans[v[j]][v[j+1]] = ans[v[j+1]][v[j]] = 1;
}
if(p[v[0]][v.back()] == 2) ans[v.back()][v[0]] = ans[v[0]][v.back()] = 1;
}
if(!ok) return 0;
build(ans);
return 1;
/*
6
1 2 2 1 2 2
2 1 2 2 1 2
2 2 1 2 2 1
1 2 2 1 2 2
2 1 2 2 1 2
2 2 1 2 2 1
5
1 1 0 0 0
1 1 0 0 0
0 0 1 2 2
0 0 2 1 2
0 0 2 2 1
5
1 2 2 2 2
2 1 2 2 2
2 2 1 2 2
2 2 2 1 2
2 2 2 2 1
*/
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:78:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j=0; j+1<v.size(); j++){
| ~~~^~~~~~~~~
# |
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 |
128 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 |
274 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 |
128 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 |
274 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
250 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
123 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
68 ms |
5752 KB |
Output is correct |
21 |
Correct |
250 ms |
22136 KB |
Output is correct |
22 |
Correct |
244 ms |
22136 KB |
Output is correct |
23 |
Correct |
269 ms |
22136 KB |
Output is correct |
24 |
Correct |
244 ms |
22136 KB |
Output is correct |
25 |
Correct |
110 ms |
12152 KB |
Output is correct |
26 |
Correct |
107 ms |
12152 KB |
Output is correct |
27 |
Correct |
268 ms |
22136 KB |
Output is correct |
28 |
Correct |
244 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 |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
12 ms |
1168 KB |
Output is correct |
9 |
Correct |
240 ms |
22136 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
264 ms |
22264 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
124 ms |
12108 KB |
Output is correct |
18 |
Correct |
0 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 |
62 ms |
5880 KB |
Output is correct |
22 |
Correct |
249 ms |
22136 KB |
Output is correct |
23 |
Correct |
244 ms |
22136 KB |
Output is correct |
24 |
Correct |
266 ms |
22136 KB |
Output is correct |
25 |
Correct |
105 ms |
12152 KB |
Output is correct |
26 |
Correct |
110 ms |
12152 KB |
Output is correct |
27 |
Correct |
245 ms |
22136 KB |
Output is correct |
28 |
Correct |
264 ms |
22136 KB |
Output is correct |
29 |
Correct |
104 ms |
12156 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 |
62 ms |
5752 KB |
Output is correct |
5 |
Correct |
257 ms |
22264 KB |
Output is correct |
6 |
Correct |
243 ms |
22264 KB |
Output is correct |
7 |
Correct |
266 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
63 ms |
5752 KB |
Output is correct |
10 |
Correct |
244 ms |
22136 KB |
Output is correct |
11 |
Correct |
245 ms |
22136 KB |
Output is correct |
12 |
Correct |
260 ms |
22136 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
62 ms |
5880 KB |
Output is correct |
17 |
Correct |
269 ms |
22224 KB |
Output is correct |
18 |
Correct |
248 ms |
22136 KB |
Output is correct |
19 |
Correct |
251 ms |
22136 KB |
Output is correct |
20 |
Correct |
243 ms |
22264 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 |
128 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 |
274 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
250 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
123 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
68 ms |
5752 KB |
Output is correct |
21 |
Correct |
250 ms |
22136 KB |
Output is correct |
22 |
Correct |
244 ms |
22136 KB |
Output is correct |
23 |
Correct |
269 ms |
22136 KB |
Output is correct |
24 |
Correct |
244 ms |
22136 KB |
Output is correct |
25 |
Correct |
110 ms |
12152 KB |
Output is correct |
26 |
Correct |
107 ms |
12152 KB |
Output is correct |
27 |
Correct |
268 ms |
22136 KB |
Output is correct |
28 |
Correct |
244 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 |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
12 ms |
1168 KB |
Output is correct |
37 |
Correct |
240 ms |
22136 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
11 ms |
1152 KB |
Output is correct |
41 |
Correct |
264 ms |
22264 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
768 KB |
Output is correct |
45 |
Correct |
124 ms |
12108 KB |
Output is correct |
46 |
Correct |
0 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 |
62 ms |
5880 KB |
Output is correct |
50 |
Correct |
249 ms |
22136 KB |
Output is correct |
51 |
Correct |
244 ms |
22136 KB |
Output is correct |
52 |
Correct |
266 ms |
22136 KB |
Output is correct |
53 |
Correct |
105 ms |
12152 KB |
Output is correct |
54 |
Correct |
110 ms |
12152 KB |
Output is correct |
55 |
Correct |
245 ms |
22136 KB |
Output is correct |
56 |
Correct |
264 ms |
22136 KB |
Output is correct |
57 |
Correct |
104 ms |
12156 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 |
123 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 |
0 ms |
256 KB |
Output is correct |
65 |
Correct |
61 ms |
5752 KB |
Output is correct |
66 |
Correct |
109 ms |
12024 KB |
Output is correct |
67 |
Correct |
109 ms |
12152 KB |
Output is correct |
68 |
Correct |
106 ms |
12116 KB |
Output is correct |
69 |
Correct |
105 ms |
12152 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 |
128 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 |
274 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
250 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
123 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
68 ms |
5752 KB |
Output is correct |
21 |
Correct |
250 ms |
22136 KB |
Output is correct |
22 |
Correct |
244 ms |
22136 KB |
Output is correct |
23 |
Correct |
269 ms |
22136 KB |
Output is correct |
24 |
Correct |
244 ms |
22136 KB |
Output is correct |
25 |
Correct |
110 ms |
12152 KB |
Output is correct |
26 |
Correct |
107 ms |
12152 KB |
Output is correct |
27 |
Correct |
268 ms |
22136 KB |
Output is correct |
28 |
Correct |
244 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 |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
12 ms |
1168 KB |
Output is correct |
37 |
Correct |
240 ms |
22136 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
11 ms |
1152 KB |
Output is correct |
41 |
Correct |
264 ms |
22264 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
768 KB |
Output is correct |
45 |
Correct |
124 ms |
12108 KB |
Output is correct |
46 |
Correct |
0 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 |
62 ms |
5880 KB |
Output is correct |
50 |
Correct |
249 ms |
22136 KB |
Output is correct |
51 |
Correct |
244 ms |
22136 KB |
Output is correct |
52 |
Correct |
266 ms |
22136 KB |
Output is correct |
53 |
Correct |
105 ms |
12152 KB |
Output is correct |
54 |
Correct |
110 ms |
12152 KB |
Output is correct |
55 |
Correct |
245 ms |
22136 KB |
Output is correct |
56 |
Correct |
264 ms |
22136 KB |
Output is correct |
57 |
Correct |
104 ms |
12156 KB |
Output is correct |
58 |
Correct |
0 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 |
62 ms |
5752 KB |
Output is correct |
62 |
Correct |
257 ms |
22264 KB |
Output is correct |
63 |
Correct |
243 ms |
22264 KB |
Output is correct |
64 |
Correct |
266 ms |
22136 KB |
Output is correct |
65 |
Correct |
0 ms |
256 KB |
Output is correct |
66 |
Correct |
63 ms |
5752 KB |
Output is correct |
67 |
Correct |
244 ms |
22136 KB |
Output is correct |
68 |
Correct |
245 ms |
22136 KB |
Output is correct |
69 |
Correct |
260 ms |
22136 KB |
Output is correct |
70 |
Correct |
0 ms |
256 KB |
Output is correct |
71 |
Correct |
0 ms |
256 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
62 ms |
5880 KB |
Output is correct |
74 |
Correct |
269 ms |
22224 KB |
Output is correct |
75 |
Correct |
248 ms |
22136 KB |
Output is correct |
76 |
Correct |
251 ms |
22136 KB |
Output is correct |
77 |
Correct |
243 ms |
22264 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 |
123 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 |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
61 ms |
5752 KB |
Output is correct |
86 |
Correct |
109 ms |
12024 KB |
Output is correct |
87 |
Correct |
109 ms |
12152 KB |
Output is correct |
88 |
Correct |
106 ms |
12116 KB |
Output is correct |
89 |
Correct |
105 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 |
124 ms |
12280 KB |
Output is correct |
94 |
Correct |
1 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 |
27 ms |
3328 KB |
Output is correct |
98 |
Correct |
109 ms |
12152 KB |
Output is correct |
99 |
Correct |
111 ms |
12152 KB |
Output is correct |
100 |
Correct |
107 ms |
12152 KB |
Output is correct |
101 |
Correct |
106 ms |
12152 KB |
Output is correct |
102 |
Correct |
120 ms |
12152 KB |
Output is correct |
103 |
Correct |
118 ms |
12116 KB |
Output is correct |
104 |
Correct |
115 ms |
12156 KB |
Output is correct |
105 |
Correct |
131 ms |
12152 KB |
Output is correct |