Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: // 連結成分
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>
#include<stack>

using namespace std;

#define MAX_N 100000

// 値の定義
const int INV = -1;

// 友達関係を保持する配列
vector<int> v_Link[MAX_N + 1];

// 連結情報を保持する配列
int ary_connection[MAX_N + 1];

void search_relation(int u_ID, int id, int user_num);

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

    for(int i = 0; i < MAX_N + 1; i++)
    {
        ary_connection[i] = INV;
    }

    cin >> n >> m;

    for(int i = 0; i < m; i++)
    {
        cin >> s >> t;

        // 友達関係を双方向に設定する
        v_Link[s].push_back(t);
        v_Link[t].push_back(s);
    }

    int id = 1;

    for(int u = 0; u < n; u++)
    {
        if( ary_connection[u] == INV )
        {
            // 友達関係の検索を実施
            search_relation(u, id++, n);
        }
    }

    cin >> q;

    for(int i = 0; i < q; i++)
    {
        cin >> s >> t;

        // 質問の結果を表示する
        if( ary_connection[s] == ary_connection[t] )
        {
            // 友達関係が存在する
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }

    return 0;
}

// 友達関係を検索するための関数
// u_ID : 友達関係を検索する最初のユーザーID
// id   : ary_connection[]に設定するID値
// user_num : 全ユーザー数
void search_relation(int u_ID, int id, int user_num)
{

    int user_id;

    queue<int> Q;

    // 開始点をキューにプッシュする
    Q.push(u_ID);
    // 友達関係を保存する
    ary_connection[u_ID] = id;

    while( !Q.empty() )
    {
        // キューの先頭要素を取得して, 削除する
        // ( 一人のユーザーの巡回は1回だけ )
        int u = Q.front();
        Q.pop();

        for(int v = 0; v < v_Link[u].size(); v++)
        {
            // 友達関係にあるユーザーの検索
            user_id = v_Link[u][v];

            if( ary_connection[user_id] == INV )
            {
                // ----- 繋がっている頂点が見つかった -----

                // 友達関係を保存する
                ary_connection[user_id] = id;

                // キューにプッシュして, 検索を継続する
                Q.push(user_id);
            }
        }
    }

    return;
}

C++: #include<stdio.h>
using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
	if (table[child] == -1)return child;
	return table[child] = parent(table, table[child]);
}

int main() {
	int n, m, q;
	int table[MAX];

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)table[i] = -1;
	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &s, &t);
		
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
		else {
			table[s] = ps;
			table[t] = pt;
		}
	}

	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		
		scanf("%d %d", &s, &t);

		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}
	return 0;
}

C++: //
//  main.cpp
//  connected_components
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

#define n_max 100000

#define NIL -1

bool found;
int n;
int color[n_max];
vector<int> G[n_max];

void encode_graph_list(){
    int i, id, node, f;
    
    scanf("%d", &n);
    scanf("%d", &node);
    
    for (i = 0; i < node; i++){
        scanf("%d", &id);
        scanf("%d", &f);
        G[id].push_back(f);
        G[f].push_back(id);
    }
    
}

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

void assign_color(){
    int id = 0;
    for(int i = 0; i < n; i++) color[i] = NIL;
    for(int i = 0; i < n; i++){
        if(color[i] == NIL){
            dfs(i, id++);
        }
    }
}


int main(int argc, const char * argv[]) {
    int qs, i, id, target;
    encode_graph_list();
    assign_color();
    scanf("%d", &qs);
    for (i = 0; i < qs; i++){
        scanf("%d %d", &id, &target);
        if(color[id] == color[target]) cout << "yes" <<endl;
        else cout << "no"<<endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		V = new vector<int>[n];
		group = new int[n];
		for(int i=0;i<n;i++){
			group[i] = 0;
		}
		group_number = 1;
	}

	void push(int s,int t){
		V[s].push_back(t);
		V[t].push_back(s);
	}

	void makeGroup(){
		int node;
		queue<int> Q;
		for(int i=0;i<n;i++){
			if(group[i] == 0){
				group[i] = group_number;
				Q.push(i);
				while(!Q.empty()){
					node = Q.front();
					Q.pop();
					if(V[node].size() > 0){
						for(int k=0; k < V[node].size();k++){
							if(group[V[node][k]] == 0){
								group[V[node][k]] = group[node];
								Q.push(V[node][k]);
							}
						}
					}
				}
				group_number++;
			}
		}
	}

	bool canAccsess(int s,int t){
		return group[s] == group[t];
	}

private:
	vector<int>* V;
	int* group;
	int group_number;
	int n,m;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.makeGroup();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}

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