-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.cpp
More file actions
126 lines (110 loc) · 3.06 KB
/
02.cpp
File metadata and controls
126 lines (110 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Grupo 37, Víctor del Pino
// Comentario general sobre la solución,
// explicando cómo se resuelve el problema
#include <iostream>
#include <fstream>
#include <algorithm>
#include "bintree_eda.h"
template<typename T>
bintree<T> leerArbol(T arbolVacio) {
T raiz;
std::cin >> raiz;
if (raiz == arbolVacio) {
return{};
}
else {
bintree<T>iz = leerArbol(arbolVacio);
bintree<T>dr = leerArbol(arbolVacio);
return { iz,raiz,dr };
}
}
bool esAvl(bintree<int> datos, int& min, int& max, int&altura) {
if (datos.empty())
return true;
else {
auto arbolLf = datos.left();
auto arbolRh = datos.right();
int yo = datos.root();
int minLf, maxLf, alturaLf;
int minRh, maxRh, alturaRh;
bool resultadoLf = true, resultadoRh = true;
bool entraLf = true, entraRh = true;
if (arbolLf.empty()) {
maxLf = minLf = yo;
alturaLf = 0;
entraLf = false;
}
else {
resultadoLf = esAvl(arbolLf, minLf, maxLf, alturaLf);
}
if (arbolRh.empty()) {
maxRh = minRh = yo;
alturaRh = 0;
entraRh = false;
}
else {
resultadoRh = esAvl(arbolRh, minRh, maxRh, alturaRh);
}
if (entraLf && entraRh && resultadoLf && resultadoRh && abs(alturaLf - alturaRh) <= 1 && (minRh > yo) && (maxLf < yo)) {//&& resolver(datos.left()) && resolver(datos.right()))
max = maxRh;
min = minLf;
altura = 1 + std::max(alturaLf, alturaRh);
return true;
}
else if (entraLf && !entraRh && resultadoLf && resultadoRh && abs(alturaLf - alturaRh) <= 1 && (maxLf < yo)) {//&& resolver(datos.left()) && resolver(datos.right()))
max = maxRh;
min = minLf;
altura = 1 + alturaLf;
return true;
}
else if (!entraLf && entraRh && resultadoLf && resultadoRh && abs(alturaLf - alturaRh) <= 1 && (minRh > yo)) {//&& resolver(datos.left()) && resolver(datos.right()))
max = maxRh;
min = minLf;
altura = 1 + alturaRh;
return true;
}
else if (!entraLf && !entraRh) {//&& resolver(datos.left()) && resolver(datos.right()))
max = maxRh;
min = minLf;
altura = 1;
return true;
}
else
return false;
}
}
// función que resuelve el problema
// comentario sobre el coste, O(f(N)), donde N es el numero de nodos del arbol ya que los recorre todos para averiguarlo.
bool resolver(bintree<int> datos) {
int min = 0;
int max = 0;
int altura = 0;
return esAvl(datos, min, max, altura);
}
// Resuelve un caso de prueba, leyendo de la entrada la
// configuración, y escribiendo la respuesta
void resuelveCaso() {
auto arbol = leerArbol(-1);
bool sol = resolver(arbol);
if (sol)
std::cout << "SI" << "\n";
else
std::cout << "NO" << "\n";
}
int main() {
// ajustes para que cin extraiga directamente de un fichero
#ifndef DOMJUDGE
std::ifstream in("Casos02.txt");
auto cinbuf = std::cin.rdbuf(in.rdbuf());
#endif
int numCasos;
std::cin >> numCasos;
for (int i = 0; i < numCasos; ++i)
resuelveCaso();
// para dejar todo como estaba al principio
#ifndef DOMJUDGE
std::cin.rdbuf(cinbuf);
system("PAUSE");
#endif
return 0;
}