#include <bits/stdc++.h>
#include "supertrees.h"
#include <vector>
using namespace std;
const int maxn = 1e3+10;
struct DSU
{
int pai[maxn], peso[maxn];
void init(int n)
{
for (int i = 0; i < n; i++)
pai[i] = i, peso[i] = 1;
}
int Find(int x)
{
if (pai[x] == x) return x;
return pai[x] = Find(pai[x]);
}
void Join(int x, int y)
{
x = Find(x), y = Find(y);
if (x == y) return;
if (peso[x] < peso[y]) swap(x, y);
pai[y] = x, peso[x] += peso[y];
}
} dsu;
int pai[maxn];
bool liga[maxn][maxn];
bool in[maxn];
void edge(int u, int v)
{
liga[u][v] = liga[v][u] = 1;
}
int construct(vector<vector<int>> p)
{
int n = p.size();
vector<vector<int>> ans;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (p[i][j] == 3)
return 0;
dsu.init(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (p[i][j] != 0)
dsu.Join(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (dsu.Find(i) == dsu.Find(j) && p[i][j] == 0)
return 0;
for (int f = 0; f < n; f++)
{
vector<int> comp;
for (int j = 0; j < n; j++)
if (dsu.Find(j) == f)
comp.push_back(j);
if (comp.size() == 0) continue;
int u = -1, v = -1;
for (auto i: comp)
for (auto j: comp)
if (p[i][j] == 2)
u = i, v = j;
if (u == -1)
{
for (auto i = 0; i < comp.size(); i++)
{
if (i < (int)comp.size()-1) edge(comp[i], comp[i+1]);
if (i > 0) edge(comp[i], comp[i-1]);
}
continue;
}
vector<int> ciclo;
ciclo.push_back(u); ciclo.push_back(v);
in[u] = 1, in[v] = 1;
for (auto i: comp)
{
bool ok = 1;
for (auto c: ciclo)
if (p[i][c] != 2)
ok = 0;
if (ok)
{
ciclo.push_back(i);
in[i] = 1;
}
}
if (ciclo.size() == 2) return 0;
edge(ciclo[0], ciclo.back());
for (int i = 1; i < ciclo.size(); i++)
edge(ciclo[i], ciclo[i-1]);
for (auto i: comp)
{
if (in[i]) continue;
int ind, qtd = 0;
for (auto c: ciclo)
if (p[i][c] == 1)
++qtd, ind = c;
if (qtd > 1) return 0;
pai[i] = ind;
edge(ind, i);
}
for (auto i: comp)
{
for (auto j: comp)
{
if (!in[i] && !in[j])
{
if (pai[i] == pai[j] && p[i][j] == 2) return 0;
if (pai[i] != pai[j] && p[i][j] == 1) return 0;
}
}
}
}
for (int i = 0; i < n; i++)
{
vector<int> a;
a.resize(n);
for (int j = 0; j < n; j++)
if (liga[i][j])
a[j] = 1;
ans.push_back(a);
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:87:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (auto i = 0; i < comp.size(); i++)
| ~~^~~~~~~~~~~~~
supertrees.cpp:118:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for (int i = 1; i < ciclo.size(); i++)
| ~~^~~~~~~~~~~~~~
supertrees.cpp:43:13: warning: 'ind' may be used uninitialized in this function [-Wmaybe-uninitialized]
43 | liga[u][v] = liga[v][u] = 1;
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~
supertrees.cpp:125:8: note: 'ind' was declared here
125 | int ind, qtd = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
279 ms |
23288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
279 ms |
23288 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
268 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
120 ms |
8184 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
68 ms |
6264 KB |
Output is correct |
21 |
Correct |
265 ms |
23032 KB |
Output is correct |
22 |
Correct |
264 ms |
23288 KB |
Output is correct |
23 |
Correct |
278 ms |
23032 KB |
Output is correct |
24 |
Correct |
263 ms |
22396 KB |
Output is correct |
25 |
Correct |
110 ms |
8184 KB |
Output is correct |
26 |
Correct |
106 ms |
8184 KB |
Output is correct |
27 |
Correct |
295 ms |
23132 KB |
Output is correct |
28 |
Correct |
270 ms |
22392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
259 ms |
22136 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
13 ms |
1408 KB |
Output is correct |
13 |
Correct |
277 ms |
23164 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
120 ms |
8184 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
67 ms |
6264 KB |
Output is correct |
22 |
Correct |
273 ms |
23160 KB |
Output is correct |
23 |
Correct |
268 ms |
23160 KB |
Output is correct |
24 |
Correct |
277 ms |
23032 KB |
Output is correct |
25 |
Correct |
110 ms |
8440 KB |
Output is correct |
26 |
Correct |
111 ms |
8184 KB |
Output is correct |
27 |
Correct |
268 ms |
23288 KB |
Output is correct |
28 |
Correct |
277 ms |
23160 KB |
Output is correct |
29 |
Correct |
112 ms |
8440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
66 ms |
6264 KB |
Output is correct |
5 |
Correct |
271 ms |
23032 KB |
Output is correct |
6 |
Correct |
265 ms |
23160 KB |
Output is correct |
7 |
Correct |
276 ms |
23032 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
69 ms |
6264 KB |
Output is correct |
10 |
Correct |
266 ms |
23032 KB |
Output is correct |
11 |
Correct |
270 ms |
23160 KB |
Output is correct |
12 |
Correct |
276 ms |
23164 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
67 ms |
6264 KB |
Output is correct |
17 |
Correct |
274 ms |
23032 KB |
Output is correct |
18 |
Correct |
268 ms |
23160 KB |
Output is correct |
19 |
Correct |
266 ms |
23160 KB |
Output is correct |
20 |
Correct |
263 ms |
22264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
279 ms |
23288 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
268 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
120 ms |
8184 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
68 ms |
6264 KB |
Output is correct |
21 |
Correct |
265 ms |
23032 KB |
Output is correct |
22 |
Correct |
264 ms |
23288 KB |
Output is correct |
23 |
Correct |
278 ms |
23032 KB |
Output is correct |
24 |
Correct |
263 ms |
22396 KB |
Output is correct |
25 |
Correct |
110 ms |
8184 KB |
Output is correct |
26 |
Correct |
106 ms |
8184 KB |
Output is correct |
27 |
Correct |
295 ms |
23132 KB |
Output is correct |
28 |
Correct |
270 ms |
22392 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
259 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
13 ms |
1408 KB |
Output is correct |
41 |
Correct |
277 ms |
23164 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
120 ms |
8184 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
67 ms |
6264 KB |
Output is correct |
50 |
Correct |
273 ms |
23160 KB |
Output is correct |
51 |
Correct |
268 ms |
23160 KB |
Output is correct |
52 |
Correct |
277 ms |
23032 KB |
Output is correct |
53 |
Correct |
110 ms |
8440 KB |
Output is correct |
54 |
Correct |
111 ms |
8184 KB |
Output is correct |
55 |
Correct |
268 ms |
23288 KB |
Output is correct |
56 |
Correct |
277 ms |
23160 KB |
Output is correct |
57 |
Correct |
112 ms |
8440 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
256 KB |
Output is correct |
60 |
Correct |
5 ms |
640 KB |
Output is correct |
61 |
Correct |
119 ms |
8156 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
67 ms |
6264 KB |
Output is correct |
66 |
Correct |
113 ms |
8184 KB |
Output is correct |
67 |
Correct |
110 ms |
8152 KB |
Output is correct |
68 |
Correct |
109 ms |
8184 KB |
Output is correct |
69 |
Correct |
109 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1408 KB |
Output is correct |
7 |
Correct |
279 ms |
23288 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
268 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
120 ms |
8184 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
68 ms |
6264 KB |
Output is correct |
21 |
Correct |
265 ms |
23032 KB |
Output is correct |
22 |
Correct |
264 ms |
23288 KB |
Output is correct |
23 |
Correct |
278 ms |
23032 KB |
Output is correct |
24 |
Correct |
263 ms |
22396 KB |
Output is correct |
25 |
Correct |
110 ms |
8184 KB |
Output is correct |
26 |
Correct |
106 ms |
8184 KB |
Output is correct |
27 |
Correct |
295 ms |
23132 KB |
Output is correct |
28 |
Correct |
270 ms |
22392 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
259 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
13 ms |
1408 KB |
Output is correct |
41 |
Correct |
277 ms |
23164 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
120 ms |
8184 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
67 ms |
6264 KB |
Output is correct |
50 |
Correct |
273 ms |
23160 KB |
Output is correct |
51 |
Correct |
268 ms |
23160 KB |
Output is correct |
52 |
Correct |
277 ms |
23032 KB |
Output is correct |
53 |
Correct |
110 ms |
8440 KB |
Output is correct |
54 |
Correct |
111 ms |
8184 KB |
Output is correct |
55 |
Correct |
268 ms |
23288 KB |
Output is correct |
56 |
Correct |
277 ms |
23160 KB |
Output is correct |
57 |
Correct |
112 ms |
8440 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
0 ms |
384 KB |
Output is correct |
61 |
Correct |
66 ms |
6264 KB |
Output is correct |
62 |
Correct |
271 ms |
23032 KB |
Output is correct |
63 |
Correct |
265 ms |
23160 KB |
Output is correct |
64 |
Correct |
276 ms |
23032 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
69 ms |
6264 KB |
Output is correct |
67 |
Correct |
266 ms |
23032 KB |
Output is correct |
68 |
Correct |
270 ms |
23160 KB |
Output is correct |
69 |
Correct |
276 ms |
23164 KB |
Output is correct |
70 |
Correct |
0 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
384 KB |
Output is correct |
73 |
Correct |
67 ms |
6264 KB |
Output is correct |
74 |
Correct |
274 ms |
23032 KB |
Output is correct |
75 |
Correct |
268 ms |
23160 KB |
Output is correct |
76 |
Correct |
266 ms |
23160 KB |
Output is correct |
77 |
Correct |
263 ms |
22264 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
1 ms |
256 KB |
Output is correct |
80 |
Correct |
5 ms |
640 KB |
Output is correct |
81 |
Correct |
119 ms |
8156 KB |
Output is correct |
82 |
Correct |
1 ms |
384 KB |
Output is correct |
83 |
Correct |
1 ms |
384 KB |
Output is correct |
84 |
Correct |
1 ms |
384 KB |
Output is correct |
85 |
Correct |
67 ms |
6264 KB |
Output is correct |
86 |
Correct |
113 ms |
8184 KB |
Output is correct |
87 |
Correct |
110 ms |
8152 KB |
Output is correct |
88 |
Correct |
109 ms |
8184 KB |
Output is correct |
89 |
Correct |
109 ms |
8284 KB |
Output is correct |
90 |
Correct |
1 ms |
256 KB |
Output is correct |
91 |
Correct |
0 ms |
256 KB |
Output is correct |
92 |
Correct |
5 ms |
640 KB |
Output is correct |
93 |
Correct |
118 ms |
8184 KB |
Output is correct |
94 |
Correct |
1 ms |
256 KB |
Output is correct |
95 |
Correct |
0 ms |
256 KB |
Output is correct |
96 |
Correct |
1 ms |
256 KB |
Output is correct |
97 |
Correct |
28 ms |
2304 KB |
Output is correct |
98 |
Correct |
109 ms |
8184 KB |
Output is correct |
99 |
Correct |
109 ms |
8184 KB |
Output is correct |
100 |
Correct |
108 ms |
8184 KB |
Output is correct |
101 |
Correct |
107 ms |
8184 KB |
Output is correct |
102 |
Correct |
113 ms |
8148 KB |
Output is correct |
103 |
Correct |
111 ms |
8184 KB |
Output is correct |
104 |
Correct |
111 ms |
8184 KB |
Output is correct |
105 |
Correct |
112 ms |
8156 KB |
Output is correct |