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 <vector>
#include <iostream>

class Union{
public:
	Union():id(++n), group(this){};
	Union* get_u(){
		if (group == this){
			return group;
		}else{
			return (group = (group->get_u()));
		}
	}
	int get_id(){
		return id;
	}
	void set_u(Union &other){
		group = other.get_u();
	}
	void join(Union &other){
		get_u()->set_u(other);
	}
	bool question(Union &other){
		return get_u() == other.get_u();
	}
private:
	int id;
	Union* group;
	static int n;
};
int Union::n{-1};
int main(){
	int n, m;
	std::cin >> n >> m;
	std::vector<Union> vec(n);
	int a, b;
	for (auto i = 0; i < m; ++i){
		std::cin >> a >> b;
		vec.at(a).join(vec.at(b));
	}
	int q;
	std::cin >> q;
	for (auto i = 0; i < q; ++i){
		std::cin >> a >> b;
		if (vec.at(a).question(vec.at(b))){
			std::cout << "yes\n";
		}else{
			std::cout << "no\n";
		}
	}
}

C++: #include <vector>
#include <cstdio>
class union_find{
????????std::vector<int> parent;
public:
????????int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));}
????????union_find(int n):parent(n+1){for(int i=1;i<=n;i++)parent[i]=i;}
????????int same(int a,int b){return root(a)==root(b);}
????????int unite(int a,int b){
????????????????int x=root(a),y=root(b);//if(x==y)return 0;
????????????????parent[x]=y;
????????????????return 1;
????????}
};
int main(){
????????int n,m;
????????scanf("%d%d",&n,&m);
????????union_find uf(n);
????????int a,b;
????????for(;m--;uf.unite(a,b))scanf("%d%d",&a,&b);
????????for(scanf("%d",&m);m--;puts(uf.same(a,b)?"yes":"no"))scanf("%d%d",&a,&b);
}

C++: #include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int n,m,q,s,t,z[100000];
vector<pair<int,int>> w;

void dfs(int n,int c) {
    z[n] = c;
    auto r = lower_bound(w.begin(),w.end(),make_pair(n,0));
    auto l = lower_bound(w.begin(),w.end(),make_pair(n+1,0));
    for(auto itr = r;itr != l;itr++) if(z[itr->second] != c)dfs(itr->second,c);
}

int main() {
    for(int i = 0;i < 100000;i++)z[i] = -1;
    cin >> n >> m;
    for(int i = 0;i < m;i++) {
        cin >> s >> t;
        w.push_back(make_pair(s,t));
        w.push_back(make_pair(t,s));
    }
    sort(w.begin(),w.end());
    cin >> q;
    int cc = 1;
    for(int i = 0;i < q;i++) {
        cin >> s >> t;
        if(z[s] == -1) dfs(s,cc++);
        if(z[s] == z[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

C++: #include <cstdio>
#include <vector>
#include <cstring>

bool isFriend(std::vector<int> *a, int s, int t, int *checked, int c){
    int tmp;
    checked[s] = c;
    if(s==t)return true;
    for(int i=0;i<a[s].size();i++){
        tmp = a[s][i];
        if(checked[tmp]>=c)continue;
        if(isFriend(a,tmp,t,checked,c))return true;
    }
    return false;
}

int main(){
    int i, n, m, q, s, t;
    int *checked;
    scanf("%d %d",&n,&m);
    checked = (int *)malloc(sizeof(int)*n);
    memset(checked, 0, sizeof(int)*n);
    std::vector<int> a[n];
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(a,s,t,checked,i+1))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

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

int n;
vector<int> G[100001];
int vis[100001];
int re[100001];

void dfs( int u, int key){
    if(vis[u]) return;
    re[u] =key;
    vis[u] = 1;
    int num = G[u].size();
    for( int i = 0; i < num; i++ ){
        int v = G[u][i];
        dfs(v,key);
    }
}


int main(){
    int i,j,k,m;
    scanf("%d %d",&n,&m);
    while(m--){
        cin >> j >> k;
        G[j].push_back(k);
        G[k].push_back(j);
    }
    for( i = 0; i < n; i++ ){
        dfs(i,i+1);
    }
    int q;
    cin >> q;
    for( i = 0; i < q; i++ ){
        cin >> j >> k;
        cout << ((re[j]==re[k])? "yes":"no") << endl;
    }
}

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

#include<iostream>
#include<vector>

using namespace std;

int visited[100000];
int la = 1;

void dfsRec(vector<int>* a, int v) {
	if(visited[v] != 0) return;
	visited[v] = la;
	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);
	}

	for(int i=0; i<n; i++) {
		if(visited[i] == 0) {
			dfsRec(adj, i);
			la++;
		}
	}

	int q;
	cin >> q;
	while(q--) {
		cin >> u >> v;
		(visited[u]==visited[v]) ? printf("yes\n") : printf("no\n");
	}
}