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>
using namespace std;

struct Unionfind {
	vector<int> data;

	Unionfind(int n) { data.assign(n, -1); }

	int root(int n) {
		if (data[n] < 0) return (n);
		return (data[n] = root(data[n]));
	}

	int size(int n) { return (-data[root(n)]); };
	bool same(int x, int y) { return (root(x) == root(y)); }

	void unite(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) return;
		if (data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
	}
};

int main() {
    int N, M; cin >> N >> M;
    Unionfind uni(N);
    for (int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        uni.unite(a, b);
    }
    int Q; cin >> Q;
    while (Q--) {
        int a, b; cin >> a >> b;
        cout << (uni.same(a, b) ? "yes" : "no") << '\n';
    }
    return (0);
}

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

using namespace std; 

int noc;
vector<int> comp;
vector< vector<int> > adj;

void dfs(int v) {
	comp[v] = noc;
	for (int i = 0; i < adj[v].size(); ++i) {
		if (comp[adj[v][i]] < 0) {
			dfs(adj[v][i]);
		}
	}
	return;
}

int main(void) {
	int nov,noe,u,v;
	cin >> nov >> noe;
	adj = vector< vector<int> >(nov);
	for (int i = 0; i < noe; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	noc = 0;
	comp = vector<int>(nov,-1);
	for (int i = 0; i < nov; ++i) {
		if (comp[i] < 0) {
			++noc;
			dfs(i);
		}
	}

	int noq;
	cin >> noq;
	for (int q = 0; q < noq; ++q) {
		cin >> u >> v;
		if (comp[u] == comp[v]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100000;

class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(){}
    UnionFind(const int node_size) : par(node_size){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    bool unite(int x, int y){
        int z;
        while(par[x] != par[y]){
            if(par[x] > par[y]) swap(x, y);
            z = par[x], par[x] = par[y];
            if(y == z) return false;
            x = z;
        }
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int main()
{
    int n,m,q;
    int a,b;
    cin >> n >> m;
    UnionFind uf(n);
    for(int i=0;i<m;i++){
        cin >> a >> b;
        uf.unite(a,b);
    }
    cin >> q;
    for(int i=0;i<q;i++){
        cin >> a >> b;
        if(uf.same(a,b)){
            cout << "yes\n";
        }else{
            cout << "no\n";
        }
    }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 100000

struct Node{
	int group;
	queue<int> fr;
};

int N, M, Q, group_num = 0;
Node person[MAX];

void isFriend(int a, int b){
	if(person[a].group < 0 && person[b].group < 0){
		int p = a, next, m;
		queue<int> move;
		group_num++;
		person[p].group = group_num;
		//cout << a << " is group " << person[a].group << endl;
		move.push(p);

		while(!move.empty()){
			p = move.front();
			if(person[p].fr.empty()){
				move.pop();
			} else {
				m = person[p].fr.size();
				while(m--){
					next = person[p].fr.front();
					person[p].fr.pop();
					if(person[next].group < 0){
						person[next].group = group_num;
						//cout << next << " is group " << person[next].group << endl;
						move.push(next);
					}
				}
				move.pop();
			}
		}

		if(person[a].group == person[b].group){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	} else if(person[a].group == person[b].group){
		cout << "yes" << endl;
	} else {
		cout << "no" << endl;
	}
}

int main(){
	cin >> N >> M;
	for(int i = 0; i < N; i++)
		person[i].group = -1;
	while(M--){
		int a, b;
		cin >> a >> b;
		person[a].fr.push(b);
		person[b].fr.push(a);
	}
	cin >> Q;
	while(Q--){
		int a, b;
		cin >> a >> b;
		isFriend(a, b);
	}
	return 0;
}

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

using namespace std;

// const int N = 100000;
vector<vector<int>> A;
vector<bool> visited;
// int n;

bool dfs(int s, int t) {
    stack<int> S;
    S.push(s);
    visited[s] = true;

    while (!S.empty()) {
        auto j = S.top();
        S.pop();

        auto& v = A[j];
        for (auto it = v.begin(); it != v.end(); ++it) {
            auto i = *it;
            if (i == t) {
                return true;
            }
            if (!visited[i]) {
                visited[j] = true;
                S.push(i);
            }
        }
    }
    return false;
}

int main() {
    int n;
    int m;
    cin >> n >> m;
    A.resize(n);
    visited.resize(n);
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s].push_back(t);
        A[t].push_back(s);
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

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

class person
{
public:
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		while (!lis.empty())
		{
			person* temp = lis.front();
			lis.pop_front();
			if (temp->Isdone == true)continue;
			if (temp == Per)return true;
			else
			{
				if (temp->serch(Per))return true;
			}
		}
		return false;
	}
};

int main()
{
	person *P;
	P = new person[MAX + 1];
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	delete[] P;
	return 0;
}