Cod sursa(job #1678057)

Utilizator vladmatei0Vlad Matei vladmatei0 Data 6 aprilie 2016 23:03:56
Problema BFS - Parcurgere in latime Scor 80
Compilator java Status done
Runda Arhiva educationala Marime 2.96 kb
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

class Graph<T> {
	class Edge {
		T from;
		T to;

		Edge(T from, T to) {
			this.from = from;
			this.to = to;
		}
	}

	List<Edge> edges;
	List<T> verticles;

	Graph() {
		edges = new ArrayList<Edge>();
		verticles = new ArrayList<T>();
	}

	Graph(List<Edge> edges, List verticles) {
		this.edges = edges;
		this.verticles = verticles;
	}
}

class BFS {
	private Graph<Integer> input;
	private Set<Integer>[] graph;
	private int length[];
	private LinkedList<Integer> queue = new LinkedList<Integer>();
	private int n;

	BFS(Graph<Integer> graph, int n) {
		this.input = graph;
		this.graph = new HashSet[n + 1];
		this.n = n;
		length = new int[n + 1];
		convertGraph();
	}

	private void convertGraph() {
		Iterator<Graph<Integer>.Edge> iterator = input.edges.iterator();
		while (iterator.hasNext()) {
			Graph<Integer>.Edge edge = iterator.next();
			if (graph[edge.from] == null) {
				graph[edge.from] = new LinkedHashSet<Integer>();
			}
			graph[edge.from].add(edge.to);
		}
	}

	public void bfs(int s) {
		queue.add(s);
		while (!queue.isEmpty()) {
			int from = queue.getFirst();
			queue.removeFirst();
			if (graph[from] == null) {
				continue;
			}
			Iterator<Integer> iterator = graph[from].iterator();
			while (iterator.hasNext()) {
				int to = iterator.next();
				if (length[to] == 0 && to != s) {
					length[to] = length[from] + 1;
					queue.addLast(to);
				}
			}
		}
	}

	public int[] getLength() {
		return length;
	}
}

public class Main {
	private final String input;
	private final String output;
	private int n, m, s;
	private Graph graph = new Graph<Integer>();

	Main(String input, String output) {
		this.input = input;
		this.output = output;
	}

	void read() throws FileNotFoundException {
		Scanner scanner = new Scanner(new FileReader(input));
		n = scanner.nextInt();
		m = scanner.nextInt();
		s = scanner.nextInt();
		Graph<Integer>.Edge edge;
		for (int i = 0; i < m; i++) {
			int l = scanner.nextInt();
			int r = scanner.nextInt();
			edge = graph.new Edge(l, r);
			graph.edges.add(edge);
		}
		scanner.close();
	}

	void write(int[] length) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(output));
		for (int i = 1; i <= n; i++) {
			if (length[i] == 0 && i != s) {
				writer.write(Integer.toString(-1) + " ");
			} else {
				writer.write(Integer.toString(length[i]) + " ");
			}
		}
		writer.flush();
		writer.close();
	}

	private void solve() throws IOException {
		read();
		BFS bfs = new BFS(graph, n);
		bfs.bfs(s);
		write(bfs.getLength());
	}

	public static void main(String args[]) throws IOException {
		Main main = new Main("bfs.in", "bfs.out");
		main.solve();
	}
}