Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     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() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		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()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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

using namespace std;

struct Info{
	Info(){s = t = -1;}
	Info(int tmp_s,int tmp_t){
		s = tmp_s;
		t = tmp_t;
	}
	int s;
	int t;
};

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		global = new Info[m];
		each = new int*[n];
		each_index = new int[n];
		num_of_each = new int[n];
		for(int i=0;i<n;i++){
			each_index[i] = 0;
			num_of_each[i] = 0;
		}
		global_index = 0;
	}

	void push(int s,int t){
		global[global_index++] = Info(s,t);
		num_of_each[s]++;
		num_of_each[t]++;
	}

	void allocate(){
		for(int i=0;i<n;i++){
			if(num_of_each[i] > 0){
				each[i] = new int[num_of_each[i]];
			}
		}
		Info tmp;
		for(int i=0;i<m;i++){
			tmp = global[i];
			each[tmp.s][each_index[tmp.s]++] = tmp.t;
			each[tmp.t][each_index[tmp.t]++] = tmp.s;
		}
	}

	bool canAccsess(int s,int t){
		queue<int> Q;
		Q.push(s);
		int node,went[n];
		for(int i=0;i<n;i++)went[i]=0;
		while(!Q.empty()){
			node = Q.front();
			went[node] = 1;
			Q.pop();
			for(int i=0;i<num_of_each[node];i++){
				if(each[node][i] == t) return true;
				else if(went[each[node][i]] == 0){
					Q.push(each[node][i]);
				}
			}
		}
		return false;
	}

private:
	Info* global;
	int** each;
	int* each_index;
	int* num_of_each;
	int global_index,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.allocate();
	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 <cstdio>
#include <cctype>
#include <vector>
using namespace std;

#define si static inline
#define gcu getchar_unlocked
si int in() {
	int c, n = 0;
	bool minus = false;
	if ((c = gcu()) == '-') minus = true; else ungetc(c, stdin);
	while ((c = gcu()) >= '0' && c <= '9') n = 10 * n + (c - '0');
	return minus ? -n : n; }
si void scan(char *s) {while (!isspace(*s++ = gcu()));}
#define pcu putchar_unlocked
#define svo si void out
template <typename T>
svo(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
svo(const char *s){while(*s)pcu(*s++);}
svo(char *s){while(*s)pcu(*s++);}
svo(char c){pcu(c);}
template <typename T>
//svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
template <typename head, typename... tail>
svo(head&& h, tail&&... t){out(h);out(move(t)...);}

typedef vector<int> v;
struct graph {
	struct node {v n; int gid = 0;};
	vector<node> nodes;
	int gid_counter = 1;

	graph(int n) {
		nodes.resize(n);
	}
	void read(int n) {
		while (n--) {
			int s = in(), t = in();
			nodes[s].n.push_back(t);
			nodes[t].n.push_back(s);
		}
	}
	void traverse(int id, int gid) {
		node &n = nodes[id];
		if (n.gid)
			return;
		n.gid = gid ? gid : gid_counter++;
		for (int i: n.n)
			traverse(i, n.gid);
	}
	void traverse() {
		int i = 0;
		for (node &n: nodes)
			traverse(i++, n.gid);
	}
	bool find(int s, int t) {
		return nodes[s].gid == nodes[t].gid;
	}
};

int main() {
	int n = in(), m = in();
	graph g(n);
	g.read(m);
	g.traverse();
	int q = in();
	while (q--) {
		int s = in();
		out(g.find(s, in())? "yes" : "no", '\n');
	}
}

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;

// 友達関係を保持するための構造体
typedef struct
{
    // 友達関係の個数
    int relation_num;
    // 関係を格納するための vector 配列
    vector<int> v_relation;
}st_Relation;

// 友達関係を保持する配列
st_Relation ary_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_Link[i].relation_num = 0;
        ary_connection[i] = INV;
    }

    cin >> n >> m;

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

        // 友達関係を双方向に設定する
        ary_Link[s].relation_num++;
        ary_Link[s].v_relation.push_back(t);

        ary_Link[t].relation_num++;
        ary_Link[t].v_relation.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 < ary_Link[u].relation_num; v++)
        {
            // 友達関係にあるユーザーの検索
            user_id = ary_Link[u].v_relation[v];

            if( ary_connection[user_id] == INV )
            {
                // ----- 未訪問の頂点が見つかった -----

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

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

    return;
}