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: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;

    
public class Program
{
    public void Proc() {
        int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
        int pointCount = inpt[0];
        int pathCount = inpt[1];
        this.TreeList = new TreeNode[pointCount];
        for(int i=0;i<pathCount; i++) {
            inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
            if(this.TreeList[inpt[0]] == null) {
                this.TreeList[inpt[0]] = new TreeNode(inpt[0]);
            }
            if(this.TreeList[inpt[1]] == null) {
                this.TreeList[inpt[1]] = new TreeNode(inpt[1]);
            }
            if(this.TreeList[inpt[1]].GetRoot().ID == this.TreeList[inpt[0]].GetRoot().ID) {
                continue;
            }
            this.TreeList[inpt[1]].UpsideDown();
            this.TreeList[inpt[1]].SetRoot(TreeList[inpt[0]].GetRoot());
            this.TreeList[inpt[0]].Add(TreeList[inpt[1]]);
        }

        int queryCount = int.Parse(Reader.ReadLine());
        StringBuilder ans = new StringBuilder();
        for(int i=0; i<queryCount; i++) {
            inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
            if(this.TreeList[inpt[0]] == null || this.TreeList[inpt[1]] == null) {
                ans.AppendLine("no");
                continue;
            }
            if(this.TreeList[inpt[0]].GetRoot().ID == this.TreeList[inpt[1]].GetRoot().ID) {
                ans.AppendLine("yes");
                continue;
            }
            ans.AppendLine("no");
        }
        Console.Write(ans.ToString());

    }

    private TreeNode[] TreeList;
    private class TreeNode {
        public TreeNode Parent;
        private TreeNode Root;
        public List<TreeNode> Items = new List<TreeNode>();
        public int ID;

        public TreeNode GetRoot() {
            if(this.Parent == null) {
                return this;
            }
            if(this.Root != null) {
                return this.Root;
            }
            this.Root = this.Parent.GetRoot();
            return this.Root;
        }

        public void SetRoot(TreeNode r) {
            this.Root = r;
            this.Items.ForEach(a=>a.SetRoot(r));
        }
        public TreeNode(int id) {
            this.ID = id;
        }

        public void Add(TreeNode cld) {
            this.Items.Add(cld);
            cld.Parent = this;
        }

        public void UpsideDown() {
            if(this.Parent == null) {
                return;
            }
            TreeNode tmp = this.Parent;
            tmp.Items.Remove(this);
            this.Parent = null;
            tmp.UpsideDown();
            this.Add(tmp);
        }
    }





    public class Reader {
        public static bool IsDebug = false;
        private static System.IO.StringReader SReader;
        private static string InitText = @"





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





";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        #if DEBUG
        Reader.IsDebug = true;
        #endif
        Program prg = new Program();
        prg.Proc();
    }
}