Cod sursa(job #1705742)

Utilizator Radu173Piersicanu Radu Radu173 Data 20 mai 2016 22:44:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator java Status done
Runda Arhiva educationala Marime 3.31 kb


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

class Main {

	// sursa: http://codeforces.com/blog/entry/7018
	static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		
		public MyScanner(String file) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(file));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine(){
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	public static class Graph {
		ArrayList<ArrayList<Integer>> g = new ArrayList<ArrayList<Integer>>();
		
		public void read(String FileName) throws FileNotFoundException {

			MyScanner sc = new MyScanner(FileName);
			
			int n,m;
			
			n = sc.nextInt();
			
			m = sc.nextInt();
			
			for( int i = 0 ; i < n ; i++ ) 
				g.add(new ArrayList<Integer>());
			
			int n1;
			int n2;
			
			for( int i = 0 ; i < m ; i++) {
				n1 = sc.nextInt()-1;
				n2 = sc.nextInt()-1;
				
				g.get(n1).add(n2);
				g.get(n2).add(n1);
			}
		}
		
	}
	
	static int count;
	
	public static void dfs( ArrayList<ArrayList<Integer>> g) throws FileNotFoundException {
		count = 0;
		int n = g.size();
		int[] p = new int[n];
		boolean[] visited = new boolean[n];
		
		for( int i = 0 ; i < n ; i++ ) {
			visited[i] = false;
			p[i] = -1;
		}
		for( int i = 0 ; i < n ; i++ ) {
			if( !visited[i] ) {
				count++;
				explorare( g, p, visited, i);
			}
		}
		PrintWriter writer = new PrintWriter("dfs.out");
		writer.print(count);
		writer.close();
	}
	
	public static void explorare(ArrayList<ArrayList<Integer>> g, int[] p, boolean[] visited, int u) {
		visited[u] = true;
		
		for( Integer v : g.get(u) ) {
			if( !visited[v] ) {
				p[v] = u;
				explorare(g, p, visited, v);
			}
		}
		
	}

	public static void bfs( ArrayList<ArrayList<Integer>> g, int s) throws FileNotFoundException {
		
		int[] sol = new int[g.size()];
		for( int i = 0 ; i < g.size() ; i++ ) 
			sol[i] = -1;
		
		Queue<Integer> q = new LinkedList<>();
		q.add(s);
		sol[s]++;
		while( !q.isEmpty() ){
			s = q.remove();
			
			for( Integer e : g.get(s) ) {
				if( sol[e] == -1 ) { 
					q.add(e);
					sol[e] = sol[s]+1;
				}
			}
		}
		PrintWriter writer = new PrintWriter("bfs.out");
		
		for( int i = 0 ; i < g.size() ; i++ )
			writer.print(sol[i] + " ");
		
		writer.close();
		
	}
	
	
	public static void main(String[] argv) throws IOException{
		
		Graph graph = new Graph();
		
		graph.read("dfs.in");
		
		dfs( graph.g );
		
	}
	
	
}