#include "supertrees.h"
#include <bits/stdc++.h>
#define FOR(i, j, k, l) for(int i = (j); i < (k); i += (l))
#define FORD(i, j, k, l) for(int i = (j);i >= (k); i -= (l))
#define REP(i, n) FOR(i, 0, n, 1)
#define REPD(i, n) FORD(i, n, 0, 1)
#define pb push_back
typedef long long ll;
using namespace std;
struct dsu{
vector<int> p;
int n;
dsu(int _n){
p.resize(n, -1);
}
int find(int x){
return (p[x] < 0 ? x : p[x] = find(p[x]));
}
bool sameSet(int u, int v){
return (find(u) == find(v));
}
bool join(int a, int b){
a = find(a); b = find(b);
if(a == b)
return false;
if(p[a] > p[b])
swap(a, b);
p[a] += p[b];
p[b] = a;
return true;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
REP(i, n){
REP(j, n){
if(p[i][j] == 3)
return 0;
}
}
vector<vector<int>> answer(n, vector<int>(n, 0));
dsu d(n);
dsu d2(n); // with 2s. if multiple 1 repres are in same comp, then create cycle from them;
REP(i, n){
REP(j, i){
if(p[i][j] == 1){
d.join(i, j);
d2.join(i, j);
}
}
}
REP(i, n){
if(d.find(i) != i){
int rp = d.find(i);
answer[rp][i] = answer[i][rp] = 1;
}
}
REP(i, n){
REP(j, i){
if(p[i][j] == 2)
d2.join(i, j);
}
}
vector<vector<int>> buckets(n);
REP(i, n){
if(d.find(i) == i){
buckets[d2.find(i)].pb(i);
}
}
// can't be 2 in p[i][j] = 2;
REP(i, n){
if(buckets[i].size() == 2)
return 0;
if(buckets[i].size() > 2){
REP(j, buckets[i].size()){
int u = buckets[i][j];
int v = buckets[i][(j + 1) % buckets[i].size()];
answer[u][v] = answer[v][u] = 1;
}
}
}
// now some connectivity checks;
// d2 is final connectivity; d are 1 components
// if p[i][j] == 0; they cant be connected in any way
// if p[i][j] == 2, they can't be connected in d;
REP(i, n){
REP(j, i){
if(p[i][j] == 0 && d2.sameSet(i, j))
return 0;
if(p[i][j] == 2 && d.sameSet(i, j))
return 0;
}
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:3:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, j, k, l) for(int i = (j); i < (k); i += (l))
| ^
supertrees.cpp:5:19: note: in expansion of macro 'FOR'
5 | #define REP(i, n) FOR(i, 0, n, 1)
| ^~~
supertrees.cpp:77:13: note: in expansion of macro 'REP'
77 | REP(j, buckets[i].size()){
| ^~~
supertrees.cpp:15:18: warning: 'd.dsu::n' may be used uninitialized in this function [-Wmaybe-uninitialized]
15 | p.resize(n, -1);
| ^
supertrees.cpp:15:18: warning: 'd2.dsu::n' may be used uninitialized in this function [-Wmaybe-uninitialized]
15 | p.resize(n, -1);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
134504 KB |
Output is correct |
2 |
Correct |
95 ms |
175248 KB |
Output is correct |
3 |
Correct |
83 ms |
138884 KB |
Output is correct |
4 |
Correct |
78 ms |
140164 KB |
Output is correct |
5 |
Correct |
96 ms |
173128 KB |
Output is correct |
6 |
Correct |
41 ms |
58572 KB |
Output is correct |
7 |
Correct |
340 ms |
161176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
134504 KB |
Output is correct |
2 |
Correct |
95 ms |
175248 KB |
Output is correct |
3 |
Correct |
83 ms |
138884 KB |
Output is correct |
4 |
Correct |
78 ms |
140164 KB |
Output is correct |
5 |
Correct |
96 ms |
173128 KB |
Output is correct |
6 |
Correct |
41 ms |
58572 KB |
Output is correct |
7 |
Correct |
340 ms |
161176 KB |
Output is correct |
8 |
Correct |
83 ms |
154020 KB |
Output is correct |
9 |
Correct |
33 ms |
51908 KB |
Output is correct |
10 |
Correct |
84 ms |
146280 KB |
Output is correct |
11 |
Correct |
58 ms |
101152 KB |
Output is correct |
12 |
Correct |
81 ms |
124380 KB |
Output is correct |
13 |
Correct |
322 ms |
171360 KB |
Output is correct |
14 |
Correct |
88 ms |
161860 KB |
Output is correct |
15 |
Correct |
92 ms |
161624 KB |
Output is correct |
16 |
Correct |
97 ms |
146332 KB |
Output is correct |
17 |
Correct |
172 ms |
124576 KB |
Output is correct |
18 |
Correct |
90 ms |
160112 KB |
Output is correct |
19 |
Correct |
96 ms |
164896 KB |
Output is correct |
20 |
Correct |
129 ms |
127980 KB |
Output is correct |
21 |
Correct |
271 ms |
79084 KB |
Output is correct |
22 |
Correct |
281 ms |
105196 KB |
Output is correct |
23 |
Correct |
355 ms |
161004 KB |
Output is correct |
24 |
Correct |
330 ms |
173840 KB |
Output is correct |
25 |
Correct |
214 ms |
184944 KB |
Output is correct |
26 |
Correct |
143 ms |
101228 KB |
Output is correct |
27 |
Correct |
347 ms |
107752 KB |
Output is correct |
28 |
Correct |
315 ms |
161656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
90180 KB |
Output is correct |
2 |
Correct |
49 ms |
88992 KB |
Output is correct |
3 |
Correct |
34 ms |
56008 KB |
Output is correct |
4 |
Correct |
56 ms |
106528 KB |
Output is correct |
5 |
Correct |
82 ms |
156640 KB |
Output is correct |
6 |
Correct |
71 ms |
118780 KB |
Output is correct |
7 |
Correct |
94 ms |
160436 KB |
Output is correct |
8 |
Correct |
77 ms |
116036 KB |
Output is correct |
9 |
Correct |
282 ms |
71700 KB |
Output is correct |
10 |
Correct |
111 ms |
167188 KB |
Output is correct |
11 |
Correct |
45 ms |
81564 KB |
Output is correct |
12 |
Correct |
77 ms |
117904 KB |
Output is correct |
13 |
Correct |
301 ms |
123844 KB |
Output is correct |
14 |
Correct |
64 ms |
100640 KB |
Output is correct |
15 |
Correct |
51 ms |
81768 KB |
Output is correct |
16 |
Correct |
38 ms |
54960 KB |
Output is correct |
17 |
Correct |
201 ms |
134240 KB |
Output is correct |
18 |
Correct |
31 ms |
53196 KB |
Output is correct |
19 |
Correct |
57 ms |
92048 KB |
Output is correct |
20 |
Correct |
63 ms |
103056 KB |
Output is correct |
21 |
Correct |
137 ms |
148284 KB |
Output is correct |
22 |
Correct |
340 ms |
129216 KB |
Output is correct |
23 |
Correct |
317 ms |
133196 KB |
Output is correct |
24 |
Correct |
313 ms |
91844 KB |
Output is correct |
25 |
Correct |
180 ms |
138400 KB |
Output is correct |
26 |
Correct |
197 ms |
148676 KB |
Output is correct |
27 |
Correct |
342 ms |
194724 KB |
Output is correct |
28 |
Correct |
304 ms |
103176 KB |
Output is correct |
29 |
Correct |
146 ms |
92960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
101496 KB |
Output is correct |
2 |
Correct |
64 ms |
105744 KB |
Output is correct |
3 |
Correct |
34 ms |
50764 KB |
Output is correct |
4 |
Correct |
155 ms |
138372 KB |
Output is correct |
5 |
Correct |
348 ms |
182464 KB |
Output is correct |
6 |
Correct |
262 ms |
71356 KB |
Output is correct |
7 |
Correct |
336 ms |
194740 KB |
Output is correct |
8 |
Correct |
79 ms |
143556 KB |
Output is correct |
9 |
Correct |
107 ms |
85532 KB |
Output is correct |
10 |
Correct |
267 ms |
75384 KB |
Output is correct |
11 |
Correct |
296 ms |
110052 KB |
Output is correct |
12 |
Correct |
330 ms |
162420 KB |
Output is correct |
13 |
Correct |
54 ms |
96580 KB |
Output is correct |
14 |
Correct |
30 ms |
51140 KB |
Output is correct |
15 |
Correct |
85 ms |
156996 KB |
Output is correct |
16 |
Correct |
106 ms |
80692 KB |
Output is correct |
17 |
Correct |
270 ms |
85532 KB |
Output is correct |
18 |
Correct |
324 ms |
150724 KB |
Output is correct |
19 |
Correct |
279 ms |
108360 KB |
Output is correct |
20 |
Correct |
296 ms |
132932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
134504 KB |
Output is correct |
2 |
Correct |
95 ms |
175248 KB |
Output is correct |
3 |
Correct |
83 ms |
138884 KB |
Output is correct |
4 |
Correct |
78 ms |
140164 KB |
Output is correct |
5 |
Correct |
96 ms |
173128 KB |
Output is correct |
6 |
Correct |
41 ms |
58572 KB |
Output is correct |
7 |
Correct |
340 ms |
161176 KB |
Output is correct |
8 |
Correct |
83 ms |
154020 KB |
Output is correct |
9 |
Correct |
33 ms |
51908 KB |
Output is correct |
10 |
Correct |
84 ms |
146280 KB |
Output is correct |
11 |
Correct |
58 ms |
101152 KB |
Output is correct |
12 |
Correct |
81 ms |
124380 KB |
Output is correct |
13 |
Correct |
322 ms |
171360 KB |
Output is correct |
14 |
Correct |
88 ms |
161860 KB |
Output is correct |
15 |
Correct |
92 ms |
161624 KB |
Output is correct |
16 |
Correct |
97 ms |
146332 KB |
Output is correct |
17 |
Correct |
172 ms |
124576 KB |
Output is correct |
18 |
Correct |
90 ms |
160112 KB |
Output is correct |
19 |
Correct |
96 ms |
164896 KB |
Output is correct |
20 |
Correct |
129 ms |
127980 KB |
Output is correct |
21 |
Correct |
271 ms |
79084 KB |
Output is correct |
22 |
Correct |
281 ms |
105196 KB |
Output is correct |
23 |
Correct |
355 ms |
161004 KB |
Output is correct |
24 |
Correct |
330 ms |
173840 KB |
Output is correct |
25 |
Correct |
214 ms |
184944 KB |
Output is correct |
26 |
Correct |
143 ms |
101228 KB |
Output is correct |
27 |
Correct |
347 ms |
107752 KB |
Output is correct |
28 |
Correct |
315 ms |
161656 KB |
Output is correct |
29 |
Correct |
56 ms |
90180 KB |
Output is correct |
30 |
Correct |
49 ms |
88992 KB |
Output is correct |
31 |
Correct |
34 ms |
56008 KB |
Output is correct |
32 |
Correct |
56 ms |
106528 KB |
Output is correct |
33 |
Correct |
82 ms |
156640 KB |
Output is correct |
34 |
Correct |
71 ms |
118780 KB |
Output is correct |
35 |
Correct |
94 ms |
160436 KB |
Output is correct |
36 |
Correct |
77 ms |
116036 KB |
Output is correct |
37 |
Correct |
282 ms |
71700 KB |
Output is correct |
38 |
Correct |
111 ms |
167188 KB |
Output is correct |
39 |
Correct |
45 ms |
81564 KB |
Output is correct |
40 |
Correct |
77 ms |
117904 KB |
Output is correct |
41 |
Correct |
301 ms |
123844 KB |
Output is correct |
42 |
Correct |
64 ms |
100640 KB |
Output is correct |
43 |
Correct |
51 ms |
81768 KB |
Output is correct |
44 |
Correct |
38 ms |
54960 KB |
Output is correct |
45 |
Correct |
201 ms |
134240 KB |
Output is correct |
46 |
Correct |
31 ms |
53196 KB |
Output is correct |
47 |
Correct |
57 ms |
92048 KB |
Output is correct |
48 |
Correct |
63 ms |
103056 KB |
Output is correct |
49 |
Correct |
137 ms |
148284 KB |
Output is correct |
50 |
Correct |
340 ms |
129216 KB |
Output is correct |
51 |
Correct |
317 ms |
133196 KB |
Output is correct |
52 |
Correct |
313 ms |
91844 KB |
Output is correct |
53 |
Correct |
180 ms |
138400 KB |
Output is correct |
54 |
Correct |
197 ms |
148676 KB |
Output is correct |
55 |
Correct |
342 ms |
194724 KB |
Output is correct |
56 |
Correct |
304 ms |
103176 KB |
Output is correct |
57 |
Correct |
146 ms |
92960 KB |
Output is correct |
58 |
Correct |
51 ms |
95308 KB |
Output is correct |
59 |
Correct |
71 ms |
122664 KB |
Output is correct |
60 |
Correct |
53 ms |
84548 KB |
Output is correct |
61 |
Correct |
151 ms |
69828 KB |
Output is correct |
62 |
Correct |
50 ms |
86924 KB |
Output is correct |
63 |
Correct |
34 ms |
55024 KB |
Output is correct |
64 |
Correct |
28 ms |
45876 KB |
Output is correct |
65 |
Correct |
95 ms |
72556 KB |
Output is correct |
66 |
Correct |
170 ms |
143732 KB |
Output is correct |
67 |
Correct |
180 ms |
169080 KB |
Output is correct |
68 |
Correct |
153 ms |
96756 KB |
Output is correct |
69 |
Correct |
147 ms |
103364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
134504 KB |
Output is correct |
2 |
Correct |
95 ms |
175248 KB |
Output is correct |
3 |
Correct |
83 ms |
138884 KB |
Output is correct |
4 |
Correct |
78 ms |
140164 KB |
Output is correct |
5 |
Correct |
96 ms |
173128 KB |
Output is correct |
6 |
Correct |
41 ms |
58572 KB |
Output is correct |
7 |
Correct |
340 ms |
161176 KB |
Output is correct |
8 |
Correct |
83 ms |
154020 KB |
Output is correct |
9 |
Correct |
33 ms |
51908 KB |
Output is correct |
10 |
Correct |
84 ms |
146280 KB |
Output is correct |
11 |
Correct |
58 ms |
101152 KB |
Output is correct |
12 |
Correct |
81 ms |
124380 KB |
Output is correct |
13 |
Correct |
322 ms |
171360 KB |
Output is correct |
14 |
Correct |
88 ms |
161860 KB |
Output is correct |
15 |
Correct |
92 ms |
161624 KB |
Output is correct |
16 |
Correct |
97 ms |
146332 KB |
Output is correct |
17 |
Correct |
172 ms |
124576 KB |
Output is correct |
18 |
Correct |
90 ms |
160112 KB |
Output is correct |
19 |
Correct |
96 ms |
164896 KB |
Output is correct |
20 |
Correct |
129 ms |
127980 KB |
Output is correct |
21 |
Correct |
271 ms |
79084 KB |
Output is correct |
22 |
Correct |
281 ms |
105196 KB |
Output is correct |
23 |
Correct |
355 ms |
161004 KB |
Output is correct |
24 |
Correct |
330 ms |
173840 KB |
Output is correct |
25 |
Correct |
214 ms |
184944 KB |
Output is correct |
26 |
Correct |
143 ms |
101228 KB |
Output is correct |
27 |
Correct |
347 ms |
107752 KB |
Output is correct |
28 |
Correct |
315 ms |
161656 KB |
Output is correct |
29 |
Correct |
56 ms |
90180 KB |
Output is correct |
30 |
Correct |
49 ms |
88992 KB |
Output is correct |
31 |
Correct |
34 ms |
56008 KB |
Output is correct |
32 |
Correct |
56 ms |
106528 KB |
Output is correct |
33 |
Correct |
82 ms |
156640 KB |
Output is correct |
34 |
Correct |
71 ms |
118780 KB |
Output is correct |
35 |
Correct |
94 ms |
160436 KB |
Output is correct |
36 |
Correct |
77 ms |
116036 KB |
Output is correct |
37 |
Correct |
282 ms |
71700 KB |
Output is correct |
38 |
Correct |
111 ms |
167188 KB |
Output is correct |
39 |
Correct |
45 ms |
81564 KB |
Output is correct |
40 |
Correct |
77 ms |
117904 KB |
Output is correct |
41 |
Correct |
301 ms |
123844 KB |
Output is correct |
42 |
Correct |
64 ms |
100640 KB |
Output is correct |
43 |
Correct |
51 ms |
81768 KB |
Output is correct |
44 |
Correct |
38 ms |
54960 KB |
Output is correct |
45 |
Correct |
201 ms |
134240 KB |
Output is correct |
46 |
Correct |
31 ms |
53196 KB |
Output is correct |
47 |
Correct |
57 ms |
92048 KB |
Output is correct |
48 |
Correct |
63 ms |
103056 KB |
Output is correct |
49 |
Correct |
137 ms |
148284 KB |
Output is correct |
50 |
Correct |
340 ms |
129216 KB |
Output is correct |
51 |
Correct |
317 ms |
133196 KB |
Output is correct |
52 |
Correct |
313 ms |
91844 KB |
Output is correct |
53 |
Correct |
180 ms |
138400 KB |
Output is correct |
54 |
Correct |
197 ms |
148676 KB |
Output is correct |
55 |
Correct |
342 ms |
194724 KB |
Output is correct |
56 |
Correct |
304 ms |
103176 KB |
Output is correct |
57 |
Correct |
146 ms |
92960 KB |
Output is correct |
58 |
Correct |
64 ms |
101496 KB |
Output is correct |
59 |
Correct |
64 ms |
105744 KB |
Output is correct |
60 |
Correct |
34 ms |
50764 KB |
Output is correct |
61 |
Correct |
155 ms |
138372 KB |
Output is correct |
62 |
Correct |
348 ms |
182464 KB |
Output is correct |
63 |
Correct |
262 ms |
71356 KB |
Output is correct |
64 |
Correct |
336 ms |
194740 KB |
Output is correct |
65 |
Correct |
79 ms |
143556 KB |
Output is correct |
66 |
Correct |
107 ms |
85532 KB |
Output is correct |
67 |
Correct |
267 ms |
75384 KB |
Output is correct |
68 |
Correct |
296 ms |
110052 KB |
Output is correct |
69 |
Correct |
330 ms |
162420 KB |
Output is correct |
70 |
Correct |
54 ms |
96580 KB |
Output is correct |
71 |
Correct |
30 ms |
51140 KB |
Output is correct |
72 |
Correct |
85 ms |
156996 KB |
Output is correct |
73 |
Correct |
106 ms |
80692 KB |
Output is correct |
74 |
Correct |
270 ms |
85532 KB |
Output is correct |
75 |
Correct |
324 ms |
150724 KB |
Output is correct |
76 |
Correct |
279 ms |
108360 KB |
Output is correct |
77 |
Correct |
296 ms |
132932 KB |
Output is correct |
78 |
Correct |
51 ms |
95308 KB |
Output is correct |
79 |
Correct |
71 ms |
122664 KB |
Output is correct |
80 |
Correct |
53 ms |
84548 KB |
Output is correct |
81 |
Correct |
151 ms |
69828 KB |
Output is correct |
82 |
Correct |
50 ms |
86924 KB |
Output is correct |
83 |
Correct |
34 ms |
55024 KB |
Output is correct |
84 |
Correct |
28 ms |
45876 KB |
Output is correct |
85 |
Correct |
95 ms |
72556 KB |
Output is correct |
86 |
Correct |
170 ms |
143732 KB |
Output is correct |
87 |
Correct |
180 ms |
169080 KB |
Output is correct |
88 |
Correct |
153 ms |
96756 KB |
Output is correct |
89 |
Correct |
147 ms |
103364 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
4 ms |
660 KB |
Output is correct |
93 |
Correct |
104 ms |
10104 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
1 ms |
204 KB |
Output is correct |
97 |
Correct |
24 ms |
2680 KB |
Output is correct |
98 |
Correct |
94 ms |
10092 KB |
Output is correct |
99 |
Correct |
98 ms |
10052 KB |
Output is correct |
100 |
Correct |
94 ms |
10092 KB |
Output is correct |
101 |
Correct |
98 ms |
10104 KB |
Output is correct |
102 |
Correct |
100 ms |
10052 KB |
Output is correct |
103 |
Correct |
98 ms |
10100 KB |
Output is correct |
104 |
Correct |
96 ms |
10104 KB |
Output is correct |
105 |
Correct |
110 ms |
10104 KB |
Output is correct |