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: Runtime Error)

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.NoSuchElementException;

public class Main {
	private static FastScanner sc = new FastScanner();
	static Node[] V;
	static ArrayDeque<Integer> que = new ArrayDeque<>();

	public static void main(String[] args) {
		int n = sc.nextInt();	
		int m = sc.nextInt();
		
		V = new Node[n];
		for(int i=0; i<n; i++) {
			V[i] = new Node();
		}
		
		for(int i=0; i<m; i++) {
			int s = sc.nextInt();
			int t = sc.nextInt();
			
			V[s].adj.add(t);
			V[t].adj.add(s);
		}
	
		int label = 0;
		for(int i=0; i<n; i++) {
			if(V[i].label == -1) {
				V[i].label = label;
				V[i].breadthFirstSearch(label);
				label++;
			}
		}
		
		int q = sc.nextInt();
		for(int i=1; i<=q; i++) {
			int s = sc.nextInt();
			int t = sc.nextInt();
			if(V[s].label == V[t].label) {
				System.out.println("yes");
			} else {
				System.out.println("no");
			}
		}
	}

	static class Node {	
		int label = -1;
		ArrayList<Integer> adj = new ArrayList<>();
		
		public void breadthFirstSearch(int label) {
			
			for(int i=0; i<adj.size(); i++) {
				if(V[adj.get(i)].label == -1) {
					que.add(adj.get(i));
					V[adj.get(i)].label = label;	
				}
			}
			
			if(!que.isEmpty()) {
				V[que.poll()].breadthFirstSearch(label);
			}
		}
	}

	static class FastScanner {
	    private final InputStream in = System.in;
	    private final byte[] buffer = new byte[1024];
	    private int ptr = 0;
	    private int buflen = 0;
	    private boolean hasNextByte() {
	        if (ptr < buflen) {
	            return true;
	        }else{
	            ptr = 0;
	            try {
	                buflen = in.read(buffer);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            if (buflen <= 0) {
	                return false;
	            }
	        }
	        return true;
	    }
	    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
	    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
	    public String next() {
	        if (!hasNext()) throw new NoSuchElementException();
	        StringBuilder sb = new StringBuilder();
	        int b = readByte();
	        while(isPrintableChar(b)) {
	            sb.appendCodePoint(b);
	            b = readByte();
	        }
	        return sb.toString();
	    }
	    public long nextLong() {
	        return Long.parseLong(next());
	    }
	    public int nextInt(){
	    	return Integer.parseInt(next());
	    }
	}
}