Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Graph {
- static boolean adj[][];
- static boolean visitado[];
- static boolean cor[];
- static int n;
- static void dfs(int v) {
- visitado[v] = true;
- for (int i = 1; i <= n; i++)
- if (adj[v][i] && !visitado[i]) {
- dfs(i);
- }
- }
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- while (in.hasNextInt()) {
- n = in.nextInt();
- if (n == 0) {
- break;
- }
- adj = new boolean[n + 1][n + 1];
- visitado = new boolean[n + 1];
- int edges = in.nextInt();
- for (int i = 0; i < edges; i++) {
- int a = in.nextInt();
- int b = in.nextInt();
- adj[a][b] = adj[b][a] = true;
- }
- dfs(1);
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment