Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class lab1Task4 {
  5.     BufferedReader br;
  6.     StringTokenizer in;
  7.     PrintWriter out;
  8.     public static int numberOfVerticles;
  9.     public static int numberOfEdges;
  10.     public int[][] edges;
  11.     public int[][] adjacencyMatrix;
  12.     public boolean[] isUsed;
  13.     public int[] whichComponent;
  14.     public int currentComponent;
  15.  
  16.     public static void main(String[] args) {
  17.         new lab1Task4().run();
  18.     }
  19.  
  20.     public void solve() throws IOException {
  21.         readInputFile();
  22.         fillAdjacencyMatrix();
  23.         findComponents();
  24.         fillOutputFile();
  25.     }
  26.  
  27.     public void fillAdjacencyMatrix() {
  28.         adjacencyMatrix = new int[numberOfVerticles][numberOfVerticles];
  29.         for (int i = 0; i < numberOfEdges; i++) {
  30.             adjacencyMatrix[edges[i][0] - 1][edges[i][1] - 1] = 1;
  31.             adjacencyMatrix[edges[i][1] - 1][edges[i][0] - 1] = 1;
  32.         }
  33.     }
  34.    
  35.     public void findComponents() {
  36.         whichComponent = new int[numberOfVerticles];
  37.         currentComponent = 1;
  38.         for(int i = 0; i < numberOfVerticles; i++) {
  39.             if(!isUsed[i]) {
  40.                 whichComponent[i] = currentComponent;
  41.                 depthFirstSearch(i);
  42.                 currentComponent++;
  43.             }
  44.         }
  45.     }
  46.  
  47.     public void depthFirstSearch(int currentVertilce) {
  48.         isUsed[currentVertilce] = true;
  49.         for (int i = 0; i < numberOfVerticles; i++)
  50.             if (adjacencyMatrix[currentVertilce][i] == 1) {
  51.                 int nextVerticle = i;
  52.                 whichComponent[nextVerticle] = currentComponent;
  53.                 if (!isUsed[nextVerticle])
  54.                     depthFirstSearch(nextVerticle);
  55.             }
  56.     }
  57.    
  58.  
  59.     public void readInputFile() throws IOException {
  60.         br = new BufferedReader(new FileReader("input.txt"));
  61.         numberOfVerticles = Integer.parseInt(nextToken());
  62.         numberOfEdges = Integer.parseInt(nextToken());
  63.         edges = new int[numberOfEdges][2];
  64.         isUsed = new boolean[numberOfVerticles];
  65.         for (int i = 0; i < numberOfEdges; i++) {
  66.             edges[i][0] = Integer.parseInt(nextToken());
  67.             edges[i][1] = Integer.parseInt(nextToken());
  68.         }
  69.     }
  70.  
  71.     public void fillOutputFile() throws IOException {
  72.         out = new PrintWriter("output.txt");
  73.         out.println(currentComponent - 1);
  74.         for(int i = 0; i < numberOfVerticles; i++)
  75.             out.print(whichComponent[i] + " ");
  76.         out.close();
  77.     }
  78.  
  79.     public void run() {
  80.         try {
  81.             solve();
  82.         } catch (IOException e) {
  83.             e.printStackTrace();
  84.             System.exit(1);
  85.         }
  86.     }
  87.  
  88.     public String nextToken() throws IOException {
  89.         while (in == null || !in.hasMoreTokens()) {
  90.             in = new StringTokenizer(br.readLine());
  91.         }
  92.         return in.nextToken();
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement