Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5.  
  6. class GRAPH {
  7.     ArrayList<Integer> GR = new ArrayList<>();
  8. }
  9.  
  10. public class BridgeNum {
  11.    public static void DFS(GRAPH[] graph, int[] used, int[] par, Queue<Integer> queue, int a) {
  12.         used[a] = 0;
  13.         queue.add(a);
  14.         for(int i = 0; i < graph[a].GR.size(); i++) {
  15.             if(used[graph[a].GR.get(i)] == 1) {
  16.                 par[graph[a].GR.get(i)] = a;
  17.                 DFS(graph, used, par, queue, graph[a].GR.get(i));
  18.             }
  19.         }
  20.     }
  21.  
  22.     public static int DFS2(GRAPH[] graph, int[] comp, int[] par, Queue<Integer> queue, int component) {
  23.         while(queue.size() > 0) {
  24.             int a = queue.remove();
  25.             if(comp[a] == -1) {
  26.                 VisitVertex(graph, comp, par, a, component);
  27.                 component++;
  28.             }
  29.         }
  30.         return component;
  31.     }
  32.  
  33.     public static void VisitVertex(GRAPH[] graph, int[] comp, int[] par, int a, int component) {
  34.         comp[a] = component;
  35.         for(int i = 0; i < graph[a].GR.size(); i++) {
  36.             if (comp[graph[a].GR.get(i)] == -1 && par[graph[a].GR.get(i)] != a)
  37.                 VisitVertex(graph, comp, par, graph[a].GR.get(i), component);
  38.         }
  39.     }
  40.     public static void main(String[] args) {
  41.         Scanner in = new Scanner(System.in);
  42.         int n ,m;
  43.         n = in.nextInt();
  44.         int[] comp = new int[n];
  45.         int[] par = new int[n];
  46.         int[] used = new int[n];
  47.         for(int i = 0; i < n; i++) {
  48.             comp[i] = -1;
  49.             par[i] = -1;
  50.             used[i] = 1;
  51.         }
  52.         GRAPH[] G = new GRAPH[n];
  53.         for(int i = 0; i < n; i++) {
  54.             G[i] = new GRAPH();
  55.         }
  56.         m = in.nextInt();
  57.         int u, v;
  58.         for(int i = 0; i < m; i++) {
  59.             u = in.nextInt();
  60.             v = in.nextInt();
  61.             G[v].GR.add(u);
  62.             G[u].GR.add(v);
  63.         }
  64.         int component = 1;
  65.         Queue<Integer> queue = new LinkedList<Integer>();
  66.         for(int i = 0; i < n; i++) {
  67.             if(used[i] == 1) {
  68.                 component--;
  69.                 DFS(G, used, par, queue, i);
  70.             }
  71.             component = DFS2(G, comp, par, queue, component);
  72.         }
  73.         System.out.println(component-1);
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement