Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Connected Components

Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.

Input

In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$.

In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other).

In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character.

Output

For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise.

Constraints

$2 \leq n \leq 100,000$

$0 \leq m \leq 100,000$

$1 \leq q \leq 10,000$

Sample Input

10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3

Sample Output

yes
yes
no

Solution in JAVA (judge verdict: Wrong Answer)


import java.util.ArrayList;
import java.util.Scanner;
import java.util.PriorityQueue;

public class Main {
	ArrayList<Vertex> nodes = new ArrayList<>();
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String[] holder = in.nextLine().split(" ");
		int n = Integer.parseInt(holder[0]);
		int m = Integer.parseInt(holder[1]);
		Main graph = new Main();
		for (int i = 0; i < n; i++) {
			Vertex v = new Vertex(i);
			graph.addVertex(v);
		}
		for (int i = m; i > 0; i--) {
			String[] relation = in.nextLine().split(" ");
			int s = Integer.parseInt(relation[0]);
			int t = Integer.parseInt(relation[1]);
			Vertex sv = graph.getVertex(s);
			Vertex tv = graph.getVertex(t);
			sv.addNeighbor(tv);
			tv.addNeighbor(sv);
		}
		
		
		
		int q = Integer.parseInt(in.nextLine());
		for (int i = q; i > 0; i--) {
			String[] query = in.nextLine().split(" ");
			int s = Integer.parseInt(query[0]);
			int t = Integer.parseInt(query[1]);
			Vertex start = graph.getVertex(s);
			Vertex end = graph.getVertex(t);
			ArrayList<Vertex> searched = new ArrayList<>();
			searched.add(start);
			boolean found = graph.DFS(start, end, searched);
			if (found) {
				System.out.println("yes");
			}
			else {
				System.out.println("no");
			}
		}
		
		
	}	
		
	public void addVertex(Vertex v) {
		nodes.add(v);
	}
	
	public Vertex getVertex(int index) {
		return nodes.get(index);
	}
	
	// Performs DFS from a specified starting node and returns true if the end can be reached from it
	// False otherwise
	public boolean DFS(Vertex start, Vertex end, ArrayList<Vertex> searched) {	
		if (searched.size() == nodes.size()) {
			return false;
		}
		else if (start.equals(end)) {
			return true;
		}
		else {
			boolean found = false;
			for (Vertex neigh : start.neighbors) {
				if (searched.contains(neigh)) {
					
				} else {
					searched.add(neigh);
					return DFS(neigh, end, searched);
				}
				
			}
			return false;
		}
	}
	
}

class Vertex {
	public ArrayList<Vertex> neighbors = new ArrayList<>();
	public int value;
	public boolean disc;
	
	public Vertex(int val) {
		value = val;
		disc = false;
	}
	
	public void addNeighbor (Vertex n) {
		neighbors.add(n);
	}
	
	public String toString() {
		return "" + this.value + " ";
	}
	
	public boolean equals(Vertex v) {
		if (this.value == v.value) {
			return true;
		}
		else {
			return false;
		}
	}
}