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 C# (judge verdict: Wrong Answer)

using System;

class Program{
    class Connection{
        public int s;
        public int t;
    }

    static void Merge(Connection[] connections, int left, int mid, int right){
        var n1=mid-left;
        var n2=right-mid;
        var l=new Connection[n1+1];
        var r=new Connection[n2+1];
        for (var i=0; i<n1; i++) {
            l[i]=connections[left+i];
        }
        for (var i=0; i<n2; i++) {
            r[i]=connections[mid+i];
        }
        l[n1]=new Connection();
        l[n1].s=100*1000+1;
        l[n1].t=100*1000+1;
        r[n2]=new Connection();
        r[n2].s=100*1000+1;
        r[n2].t=100*1000+1;

        var ll=0;
        var rr=0;
        for (var k=left; k<right; k++) {
            if (l[ll].s==r[rr].s) {
                if (l[ll].t<=r[rr].t) {
                    connections[k]=l[ll];
                    ll++;
                } else {
                    connections[k]=r[rr];
                    rr++;
                }
            } else{
                if (l[ll].s<=r[rr].s) {
                    connections[k]=l[ll];
                    ll++;
                } else {
                    connections[k]=r[rr];
                    rr++;
                }
            }
        }
    }

    static void MergeSort(Connection[] connections, int left, int right){
        if (left+1<right) {
            var mid=(left+right)/2;
            MergeSort(connections, left, mid);
            MergeSort(connections, mid, right);
            Merge(connections, left, mid, right);
        }
    }

    public static void Main(){
        var s=Console.ReadLine().Split(' ');
        var n=int.Parse(s[0]);
        var m=int.Parse(s[1]);
        var connections=new Connection[m];
        for (var i=0; i<m; i++) {
            s=Console.ReadLine().Split(' ');
            connections[i]=new Connection();
            var user1=int.Parse(s[0]);
            var user2=int.Parse(s[1]);
            if (user1<user2) {
                connections[i].s=user1;
                connections[i].t=user2;
            } else {
                connections[i].s=user2;
                connections[i].t=user1;
            }
        }

        MergeSort(connections, 0, m);
        var group=new int[n];
        for (var i=0; i<n; i++) {
            group[i]=i;
        }
        for (var i=0; i<m; i++) {
            group[connections[i].t]=group[connections[i].s];
        }

        var q=int.Parse(Console.ReadLine());
        for (var i=0; i<q; i++) {
            s=Console.ReadLine().Split(' ');
            var user1=int.Parse(s[0]);
            var user2=int.Parse(s[1]);
            if (group[user1]==group[user2]) {
                Console.WriteLine("yes");
            } else {
                Console.WriteLine("no");
            }
        }
    }
}