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.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;

class Main{
    private static int[] g;
    private static ArrayList<Integer>[] f;
    @SuppressWarnings("unchecked")
    public static void main(String[] Args) throws IOException{
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String in = br.readLine();
        final int n = Integer.parseInt(in.substring(0, in.indexOf(' ')));
        final int m = Integer.parseInt(in.substring(in.indexOf(' ')+1));
        g = new int[n];
        f = new ArrayList[n];
        for(int i = 0; i<n; i++)
            f[i] = new ArrayList<Integer>();
        for(int i = 0; i<m; i++){
            in = br.readLine();
            final int s = Integer.parseInt(in.substring(0, in.indexOf(' ')));
            final int t = Integer.parseInt(in.substring(in.indexOf(' ')+1));
            f[s].add(t);
            f[t].add(s);
        }
        int id = 0;
        for(int i = 0; i<n; i++){
            if(g[i]==0) group(i, ++id);
        }
        final StringBuilder sb = new StringBuilder();
        final int q = Integer.parseInt(br.readLine());
        for(int i = 0; i<q; i++){
            in = br.readLine();
            final int s = Integer.parseInt(in.substring(0, in.indexOf(' ')));
            final int t = Integer.parseInt(in.substring(in.indexOf(' ')+1));
            sb.append(g[s]==g[t] ? "yes\n" : "no\n");
        }
        System.out.print(sb);
    }
    private static void group(int u, int id){
        final ArrayDeque<Integer> st = new ArrayDeque<Integer>();
        st.push(u);
        while(!st.isEmpty()){
            final int uid = st.pop();
            if(g[uid]!=0) continue;
            g[uid] = id;
            for(int i = 0, n = f[uid].size(); i<n; i++){
                st.push(f[uid].get(i));
            }
        }
    }
}