Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;

vector<int> ad[100000];
vector<int> v,d;

int main()
{
	int i,j,n,m,a,b,q,l,k;
	queue<int> Q;
	
	cin >> n;
	cin >> m;
	for(i=0;i<=m-1;i++)
	{
		cin >> a;
		cin >> b;
		ad[a].push_back(b);
		ad[b].push_back(a);
	}
	for(i=0;i<=n-1;i++) 
	{
		ad[i].push_back(-1);
		v.push_back(0);
		d.push_back(-1);
	}
	j=0;
	for(i=0;i<=n-1;i++)
	{
		if(v[i]==0)
		{
			Q.push(i);
			v[i]=1;
			d[i]=j;
			while(Q.empty()!=1)
			{
				l=Q.front();
				for(k=0;ad[l][k]!=-1;k++)
				{
					if(v[ad[l][k]]==0)
					{
						Q.push(ad[l][k]);
						v[ad[l][k]]=1;
						d[ad[l][k]]=j;
					}
				}
				Q.pop();
			}
			j++;
		}
	}
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		if(d[a]==d[b]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: /*
 * ALDS1_11_D.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

#include<iostream>
#include<vector>

using namespace std;

bool visited[100000];

void dfsRec(vector<int>* a, int v) {
	if(visited[v]) return;
	visited[v] = 1;
	for(int &x:a[v]) {
		dfsRec(a, x);
	}
}

int main() {
	int n, m;
	vector<int> adj[100000];

	cin >> n >> m;
	int u, v;
	for(int i=0; i<m; i++) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	int q;
	cin >> q;
	while(q--) {
		for(int i=0; i<n; i++) {
			visited[i] = 0;
		}
		cin >> u >> v;
		dfsRec(adj, u);
		(visited[v]) ? printf("yes\n") : printf("no\n");
	}
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

vector<int> SNS[100000];
int Group[100000] = { 0 };

int main()
{
	int n, m, q, s, t;
	int man1, man2;
	int grNo = 1;
	int current, next;
	queue<int> Q;
	vector<int>::iterator it;
	
	cin >> n >> m;
	rep(i, m) {
		cin >> man1 >> man2;
		SNS[man1].push_back(man2);
		SNS[man2].push_back(man1);
	}

	rep(i, n) {
		if (Group[i] == 0) {
			Q.push(i);
			Group[i] = grNo;

			while (!Q.empty()) {
				current = Q.front();

				for (it = SNS[current].begin(); it != SNS[current].end(); it++) {
					next = *it;
					if (Group[next] == 0) {
						Q.push(next);
						Group[next] = grNo;
					}
				}

				Q.pop();
			}

			grNo++;
		}
	}

	cin >> q;
	rep(i, q) {
		cin >> s >> t;
		if (Group[s] == Group[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include <stdio.h>
int ancestor[10];
int find(int x){
    if(ancestor[x]==x)return x;
    return ancestor[x]=find(ancestor[x]);
}
int min(int a,int b){
    if(a<b)return a;
    return b;
}
int max(int a,int b){
    if(a>b)return a;
    return b;
}
int main(){
    int n,q,m,temp,x,y;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)ancestor[i]=i;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        temp=find(x);
        q=find(y);
        ancestor[max(temp,q)]=min(temp,q);
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d%d",&x,&y);
        if(find(x)!=find(y))printf("no\n");
        else printf("yes\n");
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct Vertex {
    vector<ll> node;
};

Vertex V[100005];
bool U[100005];
ll Parent[100005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    REP(_, M) {
        ll a, b;
        cin >> a >> b;
        V[a].node.push_back(b);
        V[b].node.push_back(a);
    }

    REP(i, N) {
        Parent[i] = i;
    }
    memset(U, 0, sizeof(U));

    REP(i, N) {
        if (U[i])
            continue;
        queue<ll> q;
        q.push(i);
        U[i] = true;
        while (!q.empty()) {
            ll t = q.front();
            q.pop();
            for (auto &n:V[t].node) {
                if (U[n])
                    continue;
                Parent[n] = i;
                q.push(n);
                U[n] = true;
            }
        }
    }

    ll Q;
    cin >> Q;
    REP(_, Q) {
        ll s, t;
        cin >> s >> t;
        if (Parent[s] == Parent[t])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;
#define MAX 100005
#define NIL -1

vector<int> V[MAX];
int color[MAX];


void search(int v, int c) {
	if (color[v] != NIL)return;
	color[v] = c;
	for (int i = 0; i < V[v].size(); i++) {
		search(V[v][i], c);
	}
}

int main() {
	int n, m;
	cin >> n >> m;
	V->reserve(n);
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		V[s].push_back(t);
		V[t].push_back(s);
	}

	int id = 1;
	for (int i = 0; i < n; i++)color[i] = NIL;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)search(u, id++);
	}

	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (color[s] == color[t])cout << "yes\n";
		else cout << "no\n";
	}

	return 0;
}