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: Accepted)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
//		InputStream inputStream = new FileInputStream("/workspace/AOJ/src/section12/ADSL1_11_D_in22.txt");
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int[] d;

	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			int n = in.nextInt();
			int m = in.nextInt();
			d = new int[n];

			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);
			}

			int id = 0;
			Arrays.fill(d, -1);
			for (int i = 0; i < n; i++) {
				if (d[i] == -1) {
					GraphUtils.bfs(g, i, ++id);
				}
			}

			int q = in.nextInt();
			for (int i = 0; i < q; i++) {
				int s = in.nextInt();
				int v = in.nextInt();
				String str = d[s] == d[v] ? "yes" : "no";
				out.println(str);
			}
		}
	}

	static class ArrayUtils {
		public static Map<Integer, Integer> getCountMap(int[] array) {
			Map<Integer, Integer> map = new TreeMap<>();
			for (int x : array)
				map.merge(x, 1, Integer::sum);
			return map;
		}
	}

	static class GraphUtils {
		public static void bfs(List<List<Integer>> list, int from, int id) {
			Deque<Integer> q = new ArrayDeque<Integer>();
			q.push(from);

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

		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.peek();
				boolean isConnect = false;
				for (int to : list.get(from)) {
					if (to == target) {
						isFound = true;
						return isFound;
					}
					if (d[to] != -1) {
						continue;
					}
					isConnect = true;
					stack.push(to);
					d[to] = 1;
					break;
				}
				if (!isConnect) {
					stack.pop();
				}
			}

			return isFound;
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}

	}

}