#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
struct DSU {
vi e;
DSU(int n) : e(n, -1) {}
int repr(int u) {
while(e[u] >= 0) u = e[u];
return u;
}
void join(int u, int v) {
u = repr(u); v = repr(v);
if(u == v) return;
if(e[u] >= e[v]) swap(u, v);
e[u] += e[v];
e[v] = u;
}
bool same(int u, int v) { return repr(u) == repr(v); }
vector<vi> comp() {
///va da componentele
int n = int(e.size()), nr = 0;
map<int, int> Id;
for(int i = 0; i < n; ++i) {
if(e[i] < 0) {
Id[i] = nr++;
}
}
vector<vi> Re(nr);
for(int i = 0; i < n; ++i)
Re[Id[repr(i)]].push_back(i);
return Re;
}
};
bool solve(vi comp, vector<vi> &p, vector<vi> &L) {
int m = (int)comp.size(), n = (int)p.size();
DSU Sol(m);
bool ok = true;
for(int i = 0; i < m; ++i)
for(int j = i + 1; j < m; ++j)
if(p[comp[i]][comp[j]] == 0 || p[comp[i]][comp[j]] == 3) ok = 0;
for(int i = 0; i < m; ++i)
for(int j = i + 1; j < m; ++j)
if(p[comp[i]][comp[j]] == 1) Sol.join(i, j); /// arborescenta
for(int i = 0; i < m; ++i)
for(int j = i + 1; j < m; ++j)
if(p[comp[i]][comp[j]] != 1 && Sol.same(i, j)) ok = false;
auto Arb = Sol.comp();
vi A;
for(auto sir : Arb) {
for(int i = 1; i < (int)sir.size(); ++i) {
L[comp[sir[0]]][comp[sir[i]]] = 1;
L[comp[sir[i]]][comp[sir[0]]] = 1;
}
A.push_back(comp[sir[0]]);
}
if(A.size() == 2) ok = false;
for(int i = 0; i + 1 < (int)A.size(); ++i) {
L[A[i]][A[i + 1]] = 1;
L[A[i + 1]][A[i]] = 1;
}
if(A.size() != 1) {
L[A[0]][A.back()] = 1;
L[A.back()][A[0]] = 1;
}
return ok;
}
int construct(vector<vi> p) {
int n = int(p.size());
DSU Sol(n);
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
if(p[i][j]) Sol.join(i, j);
}
}
bool ok = true;
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
if(p[i][j] == 0 && Sol.same(i, j))
ok = false;
if(p[i][j] == 3)
ok = false;
}
}
if(!ok) return 0;
auto C = Sol.comp();
vector<vi> Muchii(n, vi(n, 0));
for(auto comp : C) {
ok &= solve(comp, p, Muchii);
}
if(ok) {
build(Muchii);
}
return ok;
}
Compilation message
supertrees.cpp: In function 'bool solve(vi, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:44:31: warning: unused variable 'n' [-Wunused-variable]
44 | int m = (int)comp.size(), n = (int)p.size();
| ^
# |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
113 ms |
22012 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
113 ms |
22012 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
108 ms |
22044 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
61 ms |
8276 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
29 ms |
5776 KB |
Output is correct |
21 |
Correct |
113 ms |
22096 KB |
Output is correct |
22 |
Correct |
111 ms |
22028 KB |
Output is correct |
23 |
Correct |
118 ms |
22100 KB |
Output is correct |
24 |
Correct |
108 ms |
22100 KB |
Output is correct |
25 |
Correct |
48 ms |
8276 KB |
Output is correct |
26 |
Correct |
46 ms |
8280 KB |
Output is correct |
27 |
Correct |
116 ms |
22020 KB |
Output is correct |
28 |
Correct |
114 ms |
22044 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 |
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 |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
1116 KB |
Output is correct |
9 |
Correct |
111 ms |
22072 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
112 ms |
22096 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
56 ms |
8216 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
28 ms |
5720 KB |
Output is correct |
22 |
Correct |
110 ms |
22096 KB |
Output is correct |
23 |
Correct |
115 ms |
22100 KB |
Output is correct |
24 |
Correct |
129 ms |
22040 KB |
Output is correct |
25 |
Correct |
48 ms |
12124 KB |
Output is correct |
26 |
Correct |
55 ms |
8196 KB |
Output is correct |
27 |
Correct |
109 ms |
22096 KB |
Output is correct |
28 |
Correct |
138 ms |
22020 KB |
Output is correct |
29 |
Correct |
47 ms |
12308 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
30 ms |
5716 KB |
Output is correct |
5 |
Correct |
113 ms |
22096 KB |
Output is correct |
6 |
Correct |
119 ms |
22040 KB |
Output is correct |
7 |
Correct |
126 ms |
22100 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
28 ms |
5848 KB |
Output is correct |
10 |
Correct |
110 ms |
22100 KB |
Output is correct |
11 |
Correct |
108 ms |
22100 KB |
Output is correct |
12 |
Correct |
117 ms |
22040 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
28 ms |
5796 KB |
Output is correct |
17 |
Correct |
111 ms |
22100 KB |
Output is correct |
18 |
Correct |
109 ms |
22096 KB |
Output is correct |
19 |
Correct |
117 ms |
22020 KB |
Output is correct |
20 |
Correct |
108 ms |
22096 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
113 ms |
22012 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
108 ms |
22044 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
61 ms |
8276 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
29 ms |
5776 KB |
Output is correct |
21 |
Correct |
113 ms |
22096 KB |
Output is correct |
22 |
Correct |
111 ms |
22028 KB |
Output is correct |
23 |
Correct |
118 ms |
22100 KB |
Output is correct |
24 |
Correct |
108 ms |
22100 KB |
Output is correct |
25 |
Correct |
48 ms |
8276 KB |
Output is correct |
26 |
Correct |
46 ms |
8280 KB |
Output is correct |
27 |
Correct |
116 ms |
22020 KB |
Output is correct |
28 |
Correct |
114 ms |
22044 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1116 KB |
Output is correct |
37 |
Correct |
111 ms |
22072 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1116 KB |
Output is correct |
41 |
Correct |
112 ms |
22096 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
56 ms |
8216 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
28 ms |
5720 KB |
Output is correct |
50 |
Correct |
110 ms |
22096 KB |
Output is correct |
51 |
Correct |
115 ms |
22100 KB |
Output is correct |
52 |
Correct |
129 ms |
22040 KB |
Output is correct |
53 |
Correct |
48 ms |
12124 KB |
Output is correct |
54 |
Correct |
55 ms |
8196 KB |
Output is correct |
55 |
Correct |
109 ms |
22096 KB |
Output is correct |
56 |
Correct |
138 ms |
22020 KB |
Output is correct |
57 |
Correct |
47 ms |
12308 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
3 ms |
604 KB |
Output is correct |
61 |
Correct |
60 ms |
8212 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
32 ms |
5780 KB |
Output is correct |
66 |
Correct |
48 ms |
8276 KB |
Output is correct |
67 |
Correct |
47 ms |
8276 KB |
Output is correct |
68 |
Correct |
69 ms |
8276 KB |
Output is correct |
69 |
Correct |
55 ms |
12308 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
113 ms |
22012 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
108 ms |
22044 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
61 ms |
8276 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
29 ms |
5776 KB |
Output is correct |
21 |
Correct |
113 ms |
22096 KB |
Output is correct |
22 |
Correct |
111 ms |
22028 KB |
Output is correct |
23 |
Correct |
118 ms |
22100 KB |
Output is correct |
24 |
Correct |
108 ms |
22100 KB |
Output is correct |
25 |
Correct |
48 ms |
8276 KB |
Output is correct |
26 |
Correct |
46 ms |
8280 KB |
Output is correct |
27 |
Correct |
116 ms |
22020 KB |
Output is correct |
28 |
Correct |
114 ms |
22044 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1116 KB |
Output is correct |
37 |
Correct |
111 ms |
22072 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1116 KB |
Output is correct |
41 |
Correct |
112 ms |
22096 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
56 ms |
8216 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
28 ms |
5720 KB |
Output is correct |
50 |
Correct |
110 ms |
22096 KB |
Output is correct |
51 |
Correct |
115 ms |
22100 KB |
Output is correct |
52 |
Correct |
129 ms |
22040 KB |
Output is correct |
53 |
Correct |
48 ms |
12124 KB |
Output is correct |
54 |
Correct |
55 ms |
8196 KB |
Output is correct |
55 |
Correct |
109 ms |
22096 KB |
Output is correct |
56 |
Correct |
138 ms |
22020 KB |
Output is correct |
57 |
Correct |
47 ms |
12308 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
30 ms |
5716 KB |
Output is correct |
62 |
Correct |
113 ms |
22096 KB |
Output is correct |
63 |
Correct |
119 ms |
22040 KB |
Output is correct |
64 |
Correct |
126 ms |
22100 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
28 ms |
5848 KB |
Output is correct |
67 |
Correct |
110 ms |
22100 KB |
Output is correct |
68 |
Correct |
108 ms |
22100 KB |
Output is correct |
69 |
Correct |
117 ms |
22040 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
28 ms |
5796 KB |
Output is correct |
74 |
Correct |
111 ms |
22100 KB |
Output is correct |
75 |
Correct |
109 ms |
22096 KB |
Output is correct |
76 |
Correct |
117 ms |
22020 KB |
Output is correct |
77 |
Correct |
108 ms |
22096 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
344 KB |
Output is correct |
80 |
Correct |
3 ms |
604 KB |
Output is correct |
81 |
Correct |
60 ms |
8212 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
32 ms |
5780 KB |
Output is correct |
86 |
Correct |
48 ms |
8276 KB |
Output is correct |
87 |
Correct |
47 ms |
8276 KB |
Output is correct |
88 |
Correct |
69 ms |
8276 KB |
Output is correct |
89 |
Correct |
55 ms |
12308 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 |
604 KB |
Output is correct |
93 |
Correct |
53 ms |
8200 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
12 ms |
2408 KB |
Output is correct |
98 |
Correct |
56 ms |
8228 KB |
Output is correct |
99 |
Correct |
48 ms |
8212 KB |
Output is correct |
100 |
Correct |
50 ms |
8276 KB |
Output is correct |
101 |
Correct |
47 ms |
8272 KB |
Output is correct |
102 |
Correct |
48 ms |
8272 KB |
Output is correct |
103 |
Correct |
47 ms |
8528 KB |
Output is correct |
104 |
Correct |
47 ms |
8272 KB |
Output is correct |
105 |
Correct |
71 ms |
8216 KB |
Output is correct |