Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include<cstdio>
#include<vector>
using namespace std;
#define MAX 100000;

void DepthFirstSearch(int present_vertex, bool has_not_been_discovered[], vector<int> Adjacency_list[], int Group_number[], int group_number){
	int pv = present_vertex;
	has_not_been_discovered[pv] = false;
	Group_number[pv] = group_number;
	for(int i = 0;i < Adjacency_list[pv].size();++i){
		if(has_not_been_discovered[Adjacency_list[pv][i]]){
			DepthFirstSearch(Adjacency_list[pv][i], has_not_been_discovered, Adjacency_list, Group_number, group_number);
		}
	}
}

void DfsInit(int n, bool has_not_been_discovered[]){
	for (int i = 0; i < n; ++i)
		has_not_been_discovered[i] = true;
}

int main(){
	int number_of_users, number_of_friendships;
	scanf("%d %d", &number_of_users, &number_of_friendships);
	vector<int> Adjacency_list[number_of_users];
	for (int i = 0; i < number_of_friendships; ++i)	{
		int friend1, friend2;
		scanf("%d %d", &friend1, &friend2);
		Adjacency_list[friend1].push_back(friend2);
		Adjacency_list[friend2].push_back(friend1);
	}
	bool has_not_been_discovered[number_of_users];
	DfsInit(number_of_users, has_not_been_discovered);
	int Group_number[number_of_users];
	int group_number = 1;
	for (int user_i = 0; user_i < number_of_users; ++user_i){
		if(has_not_been_discovered[user_i])
			DepthFirstSearch(user_i, has_not_been_discovered, Adjacency_list, Group_number, group_number++);
	}
	int number_of_questions;
	scanf("%d", &number_of_questions);
	for (int i = 0; i < number_of_questions; ++i) {
		int user1, user2;
		scanf("%d %d", &user1, &user2);
		if(Group_number[user1] == Group_number[user2])
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}

C++: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20180307_AOJ
{
    class ALDS1_11D
    {
        static Node[] nodes;
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int[] n_m = line.Split(' ').Select(int.Parse).ToArray();
                int n = n_m[0];
                int m = n_m[1];

                nodes = new Node[n];
                for(int i=0;i<n;i++)
                {
                    nodes[i] = new Node();
                }
                for(int i=0;i<m;i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    int s = s_t[0];
                    int t = s_t[1];
                    nodes[s].Friends.Add(t);
                    nodes[t].Friends.Add(s);
                }

                int q = int.Parse(Console.ReadLine());
                for(int i=0;i<q;i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    int s = s_t[0];
                    int t = s_t[1];
                    bool isFriend = IsFriend(s, t, new List<int> { s });
                    if(isFriend)
                    {
                        Console.WriteLine("yes");
                    }
                    else
                    {
                        Console.WriteLine("no");
                    }
                }
            }
        }

        static bool IsFriend(int s, int t, List<int> visited)
        {
            bool rv = false;
            Node ns = nodes[s];
            if (ns.Friends.Contains(t))
            {
                rv = true;
            }
            else
            {
                foreach (var v in ns.Friends)
                {
                    if (!visited.Contains(v))
                    {
                        visited.Add(v);
                        rv = IsFriend(v, t, visited);
                        if (rv)
                        {
                            break;
                        }
                    }
                }
            }

            return rv;
        }
        
    }

    class Node
    {
        public HashSet<int> Friends { get; set; }
        public Node()
        {
            Friends = new HashSet<int>();
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

/* UnionFind：素集合系管理の構造体(union by size)
    isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
    unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
    treeSize(x): x を含む集合の要素数。
*/
struct UnionFind {
    vector<int> size, parents;
    UnionFind() {}
    UnionFind(int n) {  // make n trees.
        size.resize(n, 0);
        parents.resize(n, 0);
        for (int i = 0; i < n; i++) {
            makeTree(i);
        }
    }
    void makeTree(int x) {
        parents[x] = x;  // the parent of x is x
        size[x] = 1;
    }
    bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
    void unite(int x, int y) {
        x = findRoot(x);
        y = findRoot(y);
        if (size[x] > size[y]) {
            parents[y] = x;
            size[x] += size[y];
        } else {
            parents[x] = y;
            size[y] += size[x];
        }
    }
    int findRoot(int x) {
        if (x != parents[x]) {
            parents[x] = findRoot(parents[x]);
        }
        return parents[x];
    }
    int treeSize(int x) { return size[findRoot(x)]; }
};

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

    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        uf.unite(a, b);
    }

    int q;
    cin >> q;
    vector<int> s(q), t(q);
    for (int i = 0; i < q; i++) {
        cin >> s.at(i) >> t.at(i);
    }

    for (int i = 0; i < q; i++) {
        if (uf.isSame(s[i], t[i])) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

typedef long long ll;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

// 深さ優先探索 r:頂点, c:色の番号
void dfs(int r, int c) {
	color[r] = c;

	for (int i = 0; i < G[r].size(); i++) {
		int v = G[r][i];
		if (color[v] == NIL) {
			color[v] = c;
			dfs(v, c);
		}
	}
}

void assignColor() {
	int id=1;
	// 色の初期化
	for(int i=0; i<n; i++) color[i]=NIL;
	// 事前にそれぞれの頂点の色分けをする
	for (int u = 0; u < n; u++) {
		// 色分けが行われていない頂点に新しい色を割り当てる
		if (color[u] == NIL) {
			dfs(u, id++);
		}
	}
}

int main() {
	int s,t,m,q;
	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	assignColor();

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