Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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");
	}
}

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

bool DFS(const vector<vector<int> >& G,
         vector<bool>& is_visited, int a, int b, int i) {
    is_visited[a] = true;
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    if (!is_visited[G[a][i]] && DFS(G, is_visited, G[a][i], b, 0)) {
        return true;
    }
    return DFS(G, is_visited, a, b, i + 1);
}

int main(){
    int n, m;
    cin >> n >> m;

    vector<vector<int> > G(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (int j = 0; j < G[i].size(); j++){
//         cout << G[i][j] << " ";
//     }
//     cout << endl;
// }
//


    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        vector<bool> is_visited(n, false);
        if (DFS(G, is_visited, a, b, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}