Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#define MAX 1000001
using namespace std;

int main() {
	int i, n, m, q, a, b, gnum = 0, ptog[MAX] = {};

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		if (ptog[a] != 0)ptog[b] = ptog[a];
		else if (ptog[b] != 0)ptog[a] = ptog[b];
		else ptog[a] = ptog[b] = ++gnum;
	}

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> a >> b;
		if (ptog[a] == ptog[b])cout << "yes\n";
		else cout << "no\n";
	}
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};
bool r;

void dfs (int v, int marker){
	if(c[v]!=0){
		r=false;
		return;
	}
	r=true;
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		dfs(i,marker);
		if (r) marker++;
	}
	
	
	for (int i=0;i<n;i++){
		cout << c[i] <<endl;
	}
	
	cin >> q;
	for (int i=0;i<q;i++){
		cin >> s >> t;
		if(c[s]==c[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	return 0;
}

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;

const int REACH = 1;
const int UN_REACH = 0;

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

// 友達関係を保持する配列
st_Relation ary_Link[MAX_N + 1];

// 訪問情報を保持する配列
int ary_visit[MAX_N];

bool search_relation(int start, int goal, int user_num);

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

    for(int i = 0; i < MAX_N + 1; i++)
    {
        // 配列の初期化
        ary_Link[i].relation_num = 0;
        ary_visit[i] = UN_REACH;
    }

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

    cin >> q;

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

        // 友達関係の検索を実施
        ret = search_relation(s, t, n);

        if( ret )
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }

        for(int i = 0; i < n; i++)
        {
            // 訪問情報の初期化
            ary_visit[i] = UN_REACH;
        }
    }

    return 0;
}

// 友達関係を検索するための関数
// start    : 検索を開始する最初のユーザー
// goal     : 友達関係の最後のユーザー
// user_num : 全ユーザー数
bool search_relation(int start, int goal, int user_num)
{

    bool ret = false;
    int user_id;

    queue<int> Q;

    // 開始点をキューにプッシュする
    Q.push(start);

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

        // 訪問情報の更新
        ary_visit[u] = REACH;

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

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

                if( user_id == goal )
                {
                    // 友達のつながりが確認できたので処理を終了する
                    ret = true;
                    break;
                }
                else
                {
                    // キューにプッシュして, 検索を継続する
                    Q.push(user_id);
                }
            }
        }

    }

    return ret;
}

C++: #include <iostream>
#include <vector>
#include <stack>

//求解图的联通分量

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

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

void dfs(int r, int c) {
    stack<int> S;
    S.push(r);

    color[r] = c;

    while (!S.empty()) {
        int u = S.top();
        S.pop();
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if (color[v] == NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

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[s].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;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

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

void dfs(int r,int c) {
	
	stack<int> S;

	S.push(r);
	color[r] = c;


	while (!S.empty()) {
		int u = S.top(); S.pop();
		for(int i=0;i<G[u].size();i++){
			int v = G[u][i];
			if (color[v] == NIL) {
				color[v] = c;
					S.push(v);
				}
			}
		}
	
	}

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  u, k, v,q;
	cin >> n >>v;

	for (int i = 0; i < v; i++) {
		cin >> u >> k;
		G[u].push_back(k);
		G[k].push_back(u);
	}
	assignColor();

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

	return 0;
}

C++: #include <vector>
#include <iostream>
using namespace std;
int n, m, q, x, y; int group[111111]; vector<int> f[111111];
int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) group[i] = i, f[i].push_back(i);
	for(int i = 0; i < m; i++) {
		scanf("%d%d", &x, &y);
		if(group[x] == group[y]) continue;
		x = group[x], y = group[y];
		if(f[y].size() > f[x].size()) swap(x, y);
		for(int j = f[y].size() - 1; j >= 0; j--) {
			group[f[y][j]] = group[x];
			f[x].push_back(f[y][j]);
			f[y].pop_back();
		}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &x, &y);
		printf("%s\n", group[x] == group[y] ? "yes" : "no");
	}
	return 0;
}