Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int id;
    node* parent;
};

int find(node x){
    if(x.parent == NULL)
        return x.id;
    else
        return find(*x.parent);
}

node person[100000];

void marge(node x, node y){
    int x_id = find(x);
    int y_id = find(y);
    if(x_id == y_id)
        return;
    else
        person[y_id].parent = &person[x_id];
    return;
}

bool judge(node x, node y){
    return find(x) == find(y);
}

int main() {
    cin >> n >> m;
    REP(i, n){
        person[i].id = i;
        person[i].parent = NULL;
    }
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[t].parent == NULL)
            person[t].parent = &person[s];
        else if(person[s].parent == NULL)
            person[s].parent = &person[t];
        else marge(person[s], person[t]);
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(judge(person[s], person[t]))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
static const int NIL = -1;
static const int MAX = 100000;
int n, color[MAX];
vector<int> G[MAX];

void dfsByStack(int s, int clr)
{
    stack<int> S;
    S.push(s);
    color[s] = clr;
    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] = clr;
                S.push(v);
            }
        }
    }
}

int main()
{
    int m, q, s, t;
    cin >> n >> m;
    for (int i = 0; i != m; ++i)
    {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    for (int i = 0; i != MAX; ++i)
    {
        color[i] = NIL;
    }
    int clr = 0;
    for (int i = 0; i != n; ++i)
    {
        if (color[i] == NIL)
        {
            dfsByStack(i, ++clr);
        }
    }
    cin >> m;
    for (int i = 0; i != m; ++i)
    {
        cin >> s >> t;
        if (color[s] == color[t])
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no" << endl;
        }
    }
    return 0;
}