Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- class Vertice {
- private int rotulo;
- private boolean visitado;
- private List<Aresta> listaArestas;
- public Vertice() {
- this.visitado = false;
- this.listaArestas = new ArrayList<Aresta>(10);
- }
- public boolean isVisitado() {
- return this.visitado;
- }
- public int getRotulo() {
- return rotulo;
- }
- public void setRotulo(int rotulo) {
- this.rotulo = rotulo;
- }
- public List<Aresta> getListaArestas() {
- return listaArestas;
- }
- public void addAresta(int rotulo) {
- Aresta a = new Aresta();
- a.setRotulo(rotulo);
- this.listaArestas.add(a); // o vértice recebe esse cara
- }
- public void setVisitado() {
- this.visitado = true;
- }
- @Override
- public String toString() {
- return "Vertice [listaArestas=" + listaArestas + "]";
- }
- }
- class Aresta {
- private int rotulo;
- public int getRotulo() {
- return rotulo;
- }
- public void setRotulo(int rotulo) {
- this.rotulo = rotulo;
- }
- @Override
- public String toString() {
- return "Aresta [rotulo=" + rotulo + "]";
- }
- }
- class Grafo {
- private List<Vertice> listaVertices;
- public static int cont = 0;
- public Grafo() {
- this.listaVertices = new ArrayList<Vertice>(10);
- }
- public List<Vertice> getListaVertices() {
- return listaVertices;
- }
- public void addVertice(int rotuloVertice) {
- Vertice v = new Vertice();
- v.setRotulo(rotuloVertice);
- this.listaVertices.add(v);
- }
- public void addAresta(Grafo g, int verticeRecebeAresta, int verticeParteAresta) {
- boolean flag = false;
- boolean flag2 = false;
- int x = 0;
- for (int i = 0; i < g.getListaVertices().size(); i++) {
- if (verticeRecebeAresta == g.getListaVertices().get(i).getRotulo()) {
- x = i;
- flag = true;
- break;
- }
- }
- int t = 0;
- for (int i = 0; i < g.getListaVertices().size(); i++) {
- if (verticeParteAresta == g.getListaVertices().get(i).getRotulo()) {
- t = i;
- flag2 = true;
- break;
- }
- }
- if (flag && flag2) {
- g.getListaVertices().get(x).addAresta(verticeParteAresta);
- g.getListaVertices().get(t).addAresta(verticeRecebeAresta);
- // System.out.println("Vértice que recebeu = " + verticeRecebeAresta + " "
- // + g.getListaVertices().get(x).getListaArestas());
- // System.out.println(
- // "Vértice que partiu = " + verticeParteAresta + " " + g.getListaVertices().get(t).getListaArestas());
- }
- }
- public void buscaProfundidade(Vertice v) {
- cont++;
- v.setVisitado();
- for (int i = 0; i < v.getListaArestas().size(); i++) {
- Aresta v2 = v.getListaArestas().get(i);
- for (int j = 0; j < this.getListaVertices().size(); j++) {
- if (v2.getRotulo() == this.getListaVertices().get(j).getRotulo()) {
- Vertice v3 = this.getListaVertices().get(j);
- if (!v3.isVisitado()) {
- cont++;
- buscaProfundidade(v3);
- }
- }
- }
- }
- }
- public void mostraContChamadasEBacktracking(){
- cont -=1;
- System.out.println(cont);
- cont = 0;
- }
- @Override
- public String toString() {
- StringBuilder s = new StringBuilder();
- for (int i = 0; i < listaVertices.size(); i++) {
- s.append(listaVertices.get(i).getRotulo() + " Já visitado => " + listaVertices.get(i).isVisitado() + "\n");
- }
- return "Grafo = Vértices => \n" + s.toString();
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner r = new Scanner(System.in);
- int qtdTestes;
- qtdTestes = r.nextInt();
- int repeticoes, v, qtdArestas, verticeInicial,t;
- while (qtdTestes > 0) {
- Grafo grafo = new Grafo();
- v = 0;
- verticeInicial = r.nextInt();
- repeticoes = r.nextInt();
- while (repeticoes >= 0) {
- grafo.addVertice(v);
- v++;
- repeticoes--;
- }
- t = 0;
- qtdArestas = r.nextInt();
- while (t < qtdArestas) {
- int verticeQueRecebeAresta = r.nextInt();
- int rotuloAresta = r.nextInt();
- grafo.addAresta(grafo, verticeQueRecebeAresta, rotuloAresta);
- t++;
- }
- grafo.buscaProfundidade(grafo.getListaVertices().get(verticeInicial));
- grafo.mostraContChamadasEBacktracking();
- qtdTestes--;
- }
- r.close();
- }
- }
Add Comment
Please, Sign In to add comment