Advertisement
Riposati

Untitled

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