#include "supertrees.h"
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <iostream>
#include <optional>
using std::cout;
using std::endl;
using Matriz = std::vector<std::vector<int>>;
using Comp = std::unordered_set<int>;
using Rama = std::unordered_set<int>;
void ConstruyeLista(const Comp& comp, Matriz& matriz) {
int prev = -1;
for (int actual : comp) {
if (prev != -1) {
matriz[prev][actual] = 1;
matriz[actual][prev] = 1;
}
prev = actual;
}
}
bool ConstruyeCiclo(const std::vector<int>& vertices, Matriz& matriz) {
int n = vertices.size();
if (n < 3) {
return false;
}
for (int i = 0; i < n; ++i) {
int a = vertices[i];
int b = vertices[(i+1)%n];
matriz[a][b] = 1;
matriz[b][a] = 1;
}
return true;
}
bool Contiene(const Comp& c, int v) {
return c.count(v) > 0;
}
struct Componentes {
Componentes(int n) : n(n), comp_id(n, -1) {}
std::vector<Comp> comps;
int n;
std::vector<int> comp_id;
void AgregaComponente(Comp c) {
for (int v : c) {
comp_id[v] = comps.size();
}
comps.push_back(std::move(c));
}
};
std::optional<Componentes> EncuentraComponentesConexas(const Matriz& caminos) {
int n = caminos.size();
Componentes ret(n);
for (int origen = 0; origen < n; ++origen) {
int comp_id = ret.comp_id[origen];
if (comp_id == -1) {
Comp comp;
for (int dest = 0; dest < n; ++dest) {
if (caminos[origen][dest] > 0) {
comp.insert(dest);
}
}
ret.AgregaComponente(std::move(comp));
} else {
auto& comp = ret.comps[comp_id];
for (int dest = 0; dest < n; ++dest) {
if ((caminos[origen][dest] > 0) != Contiene(comp, dest)){
return std::nullopt;
}
}
}
}
return ret;
}
std::optional<Componentes> EncuentraRamas(const Matriz& caminos, Comp& comp) {
int n = caminos.size();
Componentes ret(n);
for (int origen : comp) {
int comp_id = ret.comp_id[origen];
if (comp_id == -1) {
Comp rama;
for (int dest : comp) {
if (caminos[origen][dest] == 1) {
rama.insert(dest);
}
}
ret.AgregaComponente(std::move(rama));
} else {
auto& rama = ret.comps[comp_id];
for (int dest : comp) {
if ((caminos[origen][dest] == 1) != Contiene(rama, dest)){
return std::nullopt;
}
}
}
}
return ret;
}
bool Busca(const Matriz& matriz, int a) {
for (const auto& vec : matriz) {
for (int val : vec) {
if (val == a) {
return true;
}
}
}
return false;
}
int construct(std::vector<std::vector<int>> caminos) {
int n = caminos.size();
if (Busca(caminos, 3)) {
return 0;
}
auto conectividad = EncuentraComponentesConexas(caminos);
if (!conectividad) {
return 0;
}
Matriz answer(n, std::vector<int>(n, 0));
for (auto& c: conectividad->comps) {
auto ramas = EncuentraRamas(caminos, c);
if (!ramas) {
return 0;
}
std::vector<int> vertices_ciclo;
for (auto& rama : ramas->comps) {
assert(rama.size() > 0);
ConstruyeLista(rama, answer);
vertices_ciclo.push_back(*rama.begin());
}
if (vertices_ciclo.size() > 1 && !ConstruyeCiclo(vertices_ciclo, answer)) {
return 0;
}
}
build(answer);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 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 |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
250 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 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 |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
250 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 |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
235 ms |
22392 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
101 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 |
62 ms |
5880 KB |
Output is correct |
21 |
Correct |
251 ms |
22228 KB |
Output is correct |
22 |
Correct |
237 ms |
22136 KB |
Output is correct |
23 |
Correct |
256 ms |
22136 KB |
Output is correct |
24 |
Correct |
234 ms |
22136 KB |
Output is correct |
25 |
Correct |
116 ms |
8188 KB |
Output is correct |
26 |
Correct |
117 ms |
8312 KB |
Output is correct |
27 |
Correct |
262 ms |
22156 KB |
Output is correct |
28 |
Correct |
234 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
10 ms |
1280 KB |
Output is correct |
9 |
Correct |
235 ms |
22392 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
242 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
644 KB |
Output is correct |
17 |
Correct |
104 ms |
8280 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 |
61 ms |
5892 KB |
Output is correct |
22 |
Correct |
252 ms |
22264 KB |
Output is correct |
23 |
Correct |
242 ms |
22180 KB |
Output is correct |
24 |
Correct |
258 ms |
22136 KB |
Output is correct |
25 |
Correct |
111 ms |
12280 KB |
Output is correct |
26 |
Correct |
100 ms |
8184 KB |
Output is correct |
27 |
Correct |
240 ms |
22220 KB |
Output is correct |
28 |
Correct |
256 ms |
22232 KB |
Output is correct |
29 |
Correct |
102 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
5884 KB |
Output is correct |
5 |
Correct |
266 ms |
22104 KB |
Output is correct |
6 |
Correct |
245 ms |
22140 KB |
Output is correct |
7 |
Correct |
259 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
61 ms |
5812 KB |
Output is correct |
10 |
Correct |
249 ms |
22136 KB |
Output is correct |
11 |
Correct |
242 ms |
22136 KB |
Output is correct |
12 |
Correct |
258 ms |
22136 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
276 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
61 ms |
5880 KB |
Output is correct |
17 |
Correct |
266 ms |
22152 KB |
Output is correct |
18 |
Correct |
250 ms |
22136 KB |
Output is correct |
19 |
Correct |
244 ms |
22136 KB |
Output is correct |
20 |
Correct |
233 ms |
22236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 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 |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
250 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 |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
235 ms |
22392 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
101 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 |
62 ms |
5880 KB |
Output is correct |
21 |
Correct |
251 ms |
22228 KB |
Output is correct |
22 |
Correct |
237 ms |
22136 KB |
Output is correct |
23 |
Correct |
256 ms |
22136 KB |
Output is correct |
24 |
Correct |
234 ms |
22136 KB |
Output is correct |
25 |
Correct |
116 ms |
8188 KB |
Output is correct |
26 |
Correct |
117 ms |
8312 KB |
Output is correct |
27 |
Correct |
262 ms |
22156 KB |
Output is correct |
28 |
Correct |
234 ms |
22264 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 |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
235 ms |
22392 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
10 ms |
1280 KB |
Output is correct |
41 |
Correct |
242 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
644 KB |
Output is correct |
45 |
Correct |
104 ms |
8280 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 |
61 ms |
5892 KB |
Output is correct |
50 |
Correct |
252 ms |
22264 KB |
Output is correct |
51 |
Correct |
242 ms |
22180 KB |
Output is correct |
52 |
Correct |
258 ms |
22136 KB |
Output is correct |
53 |
Correct |
111 ms |
12280 KB |
Output is correct |
54 |
Correct |
100 ms |
8184 KB |
Output is correct |
55 |
Correct |
240 ms |
22220 KB |
Output is correct |
56 |
Correct |
256 ms |
22232 KB |
Output is correct |
57 |
Correct |
102 ms |
12280 KB |
Output is correct |
58 |
Correct |
0 ms |
288 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
4 ms |
640 KB |
Output is correct |
61 |
Correct |
105 ms |
8312 KB |
Output is correct |
62 |
Correct |
1 ms |
256 KB |
Output is correct |
63 |
Correct |
1 ms |
256 KB |
Output is correct |
64 |
Correct |
0 ms |
256 KB |
Output is correct |
65 |
Correct |
63 ms |
5832 KB |
Output is correct |
66 |
Correct |
102 ms |
8312 KB |
Output is correct |
67 |
Correct |
102 ms |
8184 KB |
Output is correct |
68 |
Correct |
106 ms |
8276 KB |
Output is correct |
69 |
Correct |
102 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 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 |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
250 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 |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
235 ms |
22392 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
101 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 |
62 ms |
5880 KB |
Output is correct |
21 |
Correct |
251 ms |
22228 KB |
Output is correct |
22 |
Correct |
237 ms |
22136 KB |
Output is correct |
23 |
Correct |
256 ms |
22136 KB |
Output is correct |
24 |
Correct |
234 ms |
22136 KB |
Output is correct |
25 |
Correct |
116 ms |
8188 KB |
Output is correct |
26 |
Correct |
117 ms |
8312 KB |
Output is correct |
27 |
Correct |
262 ms |
22156 KB |
Output is correct |
28 |
Correct |
234 ms |
22264 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 |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
235 ms |
22392 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
10 ms |
1280 KB |
Output is correct |
41 |
Correct |
242 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
644 KB |
Output is correct |
45 |
Correct |
104 ms |
8280 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 |
61 ms |
5892 KB |
Output is correct |
50 |
Correct |
252 ms |
22264 KB |
Output is correct |
51 |
Correct |
242 ms |
22180 KB |
Output is correct |
52 |
Correct |
258 ms |
22136 KB |
Output is correct |
53 |
Correct |
111 ms |
12280 KB |
Output is correct |
54 |
Correct |
100 ms |
8184 KB |
Output is correct |
55 |
Correct |
240 ms |
22220 KB |
Output is correct |
56 |
Correct |
256 ms |
22232 KB |
Output is correct |
57 |
Correct |
102 ms |
12280 KB |
Output is correct |
58 |
Correct |
1 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 |
5884 KB |
Output is correct |
62 |
Correct |
266 ms |
22104 KB |
Output is correct |
63 |
Correct |
245 ms |
22140 KB |
Output is correct |
64 |
Correct |
259 ms |
22136 KB |
Output is correct |
65 |
Correct |
0 ms |
256 KB |
Output is correct |
66 |
Correct |
61 ms |
5812 KB |
Output is correct |
67 |
Correct |
249 ms |
22136 KB |
Output is correct |
68 |
Correct |
242 ms |
22136 KB |
Output is correct |
69 |
Correct |
258 ms |
22136 KB |
Output is correct |
70 |
Correct |
0 ms |
256 KB |
Output is correct |
71 |
Correct |
1 ms |
276 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
61 ms |
5880 KB |
Output is correct |
74 |
Correct |
266 ms |
22152 KB |
Output is correct |
75 |
Correct |
250 ms |
22136 KB |
Output is correct |
76 |
Correct |
244 ms |
22136 KB |
Output is correct |
77 |
Correct |
233 ms |
22236 KB |
Output is correct |
78 |
Correct |
0 ms |
288 KB |
Output is correct |
79 |
Correct |
0 ms |
256 KB |
Output is correct |
80 |
Correct |
4 ms |
640 KB |
Output is correct |
81 |
Correct |
105 ms |
8312 KB |
Output is correct |
82 |
Correct |
1 ms |
256 KB |
Output is correct |
83 |
Correct |
1 ms |
256 KB |
Output is correct |
84 |
Correct |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
63 ms |
5832 KB |
Output is correct |
86 |
Correct |
102 ms |
8312 KB |
Output is correct |
87 |
Correct |
102 ms |
8184 KB |
Output is correct |
88 |
Correct |
106 ms |
8276 KB |
Output is correct |
89 |
Correct |
102 ms |
12280 KB |
Output is correct |
90 |
Correct |
0 ms |
256 KB |
Output is correct |
91 |
Correct |
1 ms |
256 KB |
Output is correct |
92 |
Correct |
4 ms |
640 KB |
Output is correct |
93 |
Correct |
102 ms |
8184 KB |
Output is correct |
94 |
Correct |
0 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 |
24 ms |
2296 KB |
Output is correct |
98 |
Correct |
97 ms |
8184 KB |
Output is correct |
99 |
Correct |
96 ms |
8184 KB |
Output is correct |
100 |
Correct |
97 ms |
8184 KB |
Output is correct |
101 |
Correct |
94 ms |
8184 KB |
Output is correct |
102 |
Correct |
99 ms |
8184 KB |
Output is correct |
103 |
Correct |
94 ms |
8188 KB |
Output is correct |
104 |
Correct |
95 ms |
8184 KB |
Output is correct |
105 |
Correct |
105 ms |
8184 KB |
Output is correct |