Advertisement
Riposati

Untitled

Jun 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. class Vertice {
  6.  
  7. private int rotulo;
  8. private boolean visitado;
  9. private List<Aresta> listaArestas;
  10.  
  11. public Vertice() {
  12. this.visitado = false;
  13. this.listaArestas = new ArrayList<Aresta>(10);
  14. }
  15.  
  16. public boolean isVisitado() {
  17.  
  18. return this.visitado;
  19. }
  20.  
  21. public int getRotulo() {
  22. return rotulo;
  23. }
  24.  
  25. public void setRotulo(int rotulo) {
  26. this.rotulo = rotulo;
  27. }
  28.  
  29. public List<Aresta> getListaArestas() {
  30. return listaArestas;
  31. }
  32.  
  33. public void addAresta(int rotulo) {
  34.  
  35. Aresta a = new Aresta();
  36. a.setRotulo(rotulo);
  37. this.listaArestas.add(a); // o vértice recebe esse cara
  38. }
  39.  
  40. public void setVisitado() {
  41. this.visitado = true;
  42.  
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return "Vertice [listaArestas=" + listaArestas + "]";
  48. }
  49. }
  50.  
  51. class Aresta {
  52.  
  53. private int rotulo;
  54.  
  55. public int getRotulo() {
  56. return rotulo;
  57. }
  58.  
  59. public void setRotulo(int rotulo) {
  60. this.rotulo = rotulo;
  61. }
  62.  
  63. @Override
  64. public String toString() {
  65. return "Aresta [rotulo=" + rotulo + "]";
  66. }
  67. }
  68.  
  69. class Grafo {
  70.  
  71. private List<Vertice> listaVertices;
  72. public static int cont = 0;
  73.  
  74. public Grafo() {
  75. this.listaVertices = new ArrayList<Vertice>(10);
  76. }
  77.  
  78. public List<Vertice> getListaVertices() {
  79. return listaVertices;
  80. }
  81.  
  82. public void addVertice(int rotuloVertice) {
  83.  
  84. Vertice v = new Vertice();
  85. v.setRotulo(rotuloVertice);
  86. this.listaVertices.add(v);
  87. }
  88.  
  89. public void addAresta(Grafo g, int verticeRecebeAresta, int verticeParteAresta) {
  90.  
  91. boolean flag = false;
  92. boolean flag2 = false;
  93.  
  94. int x = 0;
  95. for (int i = 0; i < g.getListaVertices().size(); i++) {
  96.  
  97. if (verticeRecebeAresta == g.getListaVertices().get(i).getRotulo()) {
  98. x = i;
  99. flag = true;
  100. break;
  101. }
  102. }
  103.  
  104. int t = 0;
  105. for (int i = 0; i < g.getListaVertices().size(); i++) {
  106.  
  107. if (verticeParteAresta == g.getListaVertices().get(i).getRotulo()) {
  108. t = i;
  109. flag2 = true;
  110. break;
  111. }
  112. }
  113.  
  114. if (flag && flag2) {
  115.  
  116. g.getListaVertices().get(x).addAresta(verticeParteAresta);
  117. g.getListaVertices().get(t).addAresta(verticeRecebeAresta);
  118.  
  119. // System.out.println("Vértice que recebeu = " + verticeRecebeAresta + " "
  120. // + g.getListaVertices().get(x).getListaArestas());
  121. // System.out.println(
  122. // "Vértice que partiu = " + verticeParteAresta + " " + g.getListaVertices().get(t).getListaArestas());
  123. }
  124. }
  125.  
  126. public void buscaProfundidade(Vertice v) {
  127.  
  128. cont++;
  129. v.setVisitado();
  130.  
  131. for (int i = 0; i < v.getListaArestas().size(); i++) {
  132.  
  133. Aresta v2 = v.getListaArestas().get(i);
  134.  
  135. for (int j = 0; j < this.getListaVertices().size(); j++) {
  136.  
  137. if (v2.getRotulo() == this.getListaVertices().get(j).getRotulo()) {
  138.  
  139. Vertice v3 = this.getListaVertices().get(j);
  140.  
  141. if (!v3.isVisitado()) {
  142. cont++;
  143. buscaProfundidade(v3);
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. public void mostraContChamadasEBacktracking(){
  151.  
  152. cont -=1;
  153. System.out.println(cont);
  154. cont = 0;
  155. }
  156.  
  157. @Override
  158. public String toString() {
  159.  
  160. StringBuilder s = new StringBuilder();
  161.  
  162. for (int i = 0; i < listaVertices.size(); i++) {
  163.  
  164. s.append(listaVertices.get(i).getRotulo() + " Já visitado => " + listaVertices.get(i).isVisitado() + "\n");
  165. }
  166. return "Grafo = Vértices => \n" + s.toString();
  167. }
  168. }
  169.  
  170. public class Main {
  171.  
  172. public static void main(String[] args) {
  173.  
  174. Scanner r = new Scanner(System.in);
  175.  
  176. int qtdTestes;
  177.  
  178. qtdTestes = r.nextInt();
  179. int repeticoes, v, qtdArestas, verticeInicial,t;
  180.  
  181. while (qtdTestes > 0) {
  182.  
  183. Grafo grafo = new Grafo();
  184. v = 0;
  185.  
  186. verticeInicial = r.nextInt();
  187. repeticoes = r.nextInt();
  188.  
  189. while (repeticoes >= 0) {
  190.  
  191. grafo.addVertice(v);
  192. v++;
  193.  
  194. repeticoes--;
  195. }
  196.  
  197. t = 0;
  198. qtdArestas = r.nextInt();
  199.  
  200. while (t < qtdArestas) {
  201.  
  202. int verticeQueRecebeAresta = r.nextInt();
  203. int rotuloAresta = r.nextInt();
  204.  
  205. grafo.addAresta(grafo, verticeQueRecebeAresta, rotuloAresta);
  206. t++;
  207. }
  208. grafo.buscaProfundidade(grafo.getListaVertices().get(verticeInicial));
  209. grafo.mostraContChamadasEBacktracking();
  210. qtdTestes--;
  211. }
  212.  
  213. r.close();
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement