Cod sursa(job #2238841)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 7 septembrie 2018 21:32:07
Problema BFS - Parcurgere in latime Scor 100
Compilator java Status done
Runda Arhiva educationala Marime 3.58 kb
import static java.lang.Character.isDigit;
import static java.util.Arrays.fill;
import static java.util.Objects.requireNonNull;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

public final class Main {
  public static final String IN_FILE = "bfs.in";
  public static final String OUT_FILE = "bfs.out";

  public static final class FastScanner implements AutoCloseable {
    private static final int BUFFER_SIZE = 1 << 16;

    private final DataInputStream stream;
    private final byte[] buffer = new byte[BUFFER_SIZE];
    private int bufferPointer = 0;
    private int bytesRead = 0;

    public FastScanner(final String fileName) throws IOException {
      stream = new DataInputStream(new FileInputStream(fileName));
    }

    public int nextInt() throws IOException {
      byte c;
      do {
        c = read();
      } while (c != '-' && !isDigit(c));
      final boolean isNegative = c == '-';
      if (isNegative) {
        c = read();
      }
      int value = 0;
      do {
        value = value * 10 + (c - '0');
        c = read();
      } while (isDigit(c));
      return isNegative ? -value : value;
    }

    private byte read() throws IOException {
      final byte c = tryRead();
      if (c == -1) {
        throw new IOException("Reached end of stream!");
      }
      return c;
    }

    private byte tryRead() throws IOException {
      if (bufferPointer == bytesRead) {
        bufferPointer = 0;
        bytesRead = stream.read(buffer, 0, BUFFER_SIZE);
        if (bytesRead == -1) {
          bytesRead = 0;
          return -1;
        }
      }
      return buffer[bufferPointer++];
    }

    @Override
    public void close() throws IOException {
      stream.close();
    }
  }

  public static int[] bfs(final int[][] graph, final int source) {
    final int[] distances = new int[graph.length];
    fill(distances, -1);
    final int[] queue = new int[graph.length];
    int front = 0;
    int back = 0;
    distances[source] = 0;
    queue[back++] = source;
    while (front < back) {
      final int u = queue[front++];
      for (final int v : graph[u]) {
        if (distances[v] == -1) {
          distances[v] = distances[u] + 1;
          queue[back++] = v;
        }
      }
    }
    return distances;
  }

  public static int[][] readGraph(
      final FastScanner scanner, final int size, final int edgeCount)
      throws IOException {
    final int[][] edges = new int[edgeCount][];
    final int[] degrees = new int[size];
    for (int e = 0; e < edgeCount; e++) {
      final int u = scanner.nextInt() - 1;
      final int v = scanner.nextInt() - 1;
      edges[e] = new int[] {u, v};
      degrees[u]++;
    }
    final int[][] graph = new int[size][];
    for (int u = 0; u < size; u++) {
      graph[u] = new int[degrees[u]];
    }
    for (int e = 0; e < edgeCount; e++) {
      final int u = edges[e][0];
      final int v = edges[e][1];
      graph[u][degrees[u] - 1] = v;
      degrees[u]--;
    }
    return graph;
  }

  public static void main(final String[] args) throws IOException {
    try (final FastScanner scanner = new FastScanner(IN_FILE);
        final PrintWriter writer = new PrintWriter(OUT_FILE)) {
      final int size = scanner.nextInt();
      final int edgeCount = scanner.nextInt();
      final int source = scanner.nextInt() - 1;
      final int[][] graph = readGraph(scanner, size, edgeCount);
      final int[] distances = bfs(graph, source);
      for (int u = 0; u < graph.length; u++) {
        writer.print(distances[u]);
        writer.print(u + 1 < graph.length ? ' ' : '\n');
      }
    }
  }
}