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.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner in = new Scanner(System.in);
//		Scanner in = new Scanner(new File("/workspace/AOJ/src/section1/sample_con.txt"));

		int n = in.nextInt();
		int m = in.nextInt();

		List<List<Integer>> g = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			g.add(new ArrayList<Integer>());
		}
		for (int i = 0; i < m; i++) {
			int v1 = in.nextInt();
			int v2 = in.nextInt();

			g.get(v1).add(v2);
			g.get(v2).add(v1);
		}

		for (int i = 0; i < g.size(); i++) {
			System.out.println(i + " " + g.get(i));
		}
		int q = in.nextInt();
		for (int i = 0; i < q; i++) {
			int from = in.nextInt();
			int to = in.nextInt();
			if (bfs(g, from, to)) {
				System.out.println("yes");
			} else {
				System.out.println("no");
			}
		}
	}
	public static boolean bfs(List<List<Integer>> list, int from, int target) {
		boolean isFound = false;
		int[] d = new int[list.size()];
		Arrays.fill(d, -1);

		Deque<Integer> q = new ArrayDeque<Integer>();
		q.push(from);

		while (!q.isEmpty()) {
			int index = q.pop();
			d[index] = 1;
			for (int num : list.get(index)) {
				if (num == target) {
					isFound = true;
					return isFound;
				}
				if (d[num] == -1) {
					q.push(num);
				}
			}
		}
		return isFound;
	}


	public static boolean dfs(List<List<Integer>> list, int[] d, int index, int target) {
		boolean flg = false;
		List<Integer> l = list.get(index);
		d[index] = 1;
		for (int i = 0; i < l.size() && !flg; i++) {
			int num = l.get(i);
			if (d[num] != -1) {
				continue;
			}
			d[num] = 1;
			if (num == target) {
				return true;
			}
			flg = dfs(list, d, num, target);
		}
		return flg;
	}
	public static boolean dfs2(List<List<Integer>> list, int from, int target) {
		boolean isFound = false;
		Stack<Integer> stack = new Stack<Integer>();
		int[] d = new int[list.size()];
		Arrays.fill(d, -1);

		stack.push(from);
		d[from] = 1;

		while (!stack.isEmpty()) {
			from = stack.pop();
			for (int to : list.get(from)) {
				if (to == target) {
					isFound = true;
					return isFound;
				}
				if (d[to] != -1) {
					continue;
				}
				stack.push(to);
				d[to] = 1;
				break;
			}
		}

		return isFound;
	}

}