Guest User

Untitled

a guest
Apr 19th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Graph {
  4.  
  5.     static boolean adj[][];
  6.     static boolean visitado[];
  7.     static boolean cor[];
  8.     static int n;
  9.  
  10.     static void dfs(int v) {
  11.         visitado[v] = true;
  12.         for (int i = 1; i <= n; i++)
  13.             if (adj[v][i] && !visitado[i]) {
  14.                 dfs(i);
  15.             }
  16.     }
  17.  
  18.     public static void main(String args[]) {
  19.         Scanner in = new Scanner(System.in);
  20.         while (in.hasNextInt()) {
  21.  
  22.             n = in.nextInt();
  23.             if (n == 0) {
  24.                 break;
  25.             }
  26.             adj = new boolean[n + 1][n + 1];
  27.             visitado = new boolean[n + 1];
  28.             int edges = in.nextInt();
  29.             for (int i = 0; i < edges; i++) {
  30.                 int a = in.nextInt();
  31.                 int b = in.nextInt();
  32.                 adj[a][b] = adj[b][a] = true;
  33.             }
  34.  
  35.             dfs(1);
  36.             System.out.println();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment