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 <iostream>
#include <vector>
using namespace std;

class UnionFind {
public:
    vector<int> par, rank;
    UnionFind(int size) {
        par.resize(size);
        rank.resize(size);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }

    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        if (rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    UnionFind uf = UnionFind(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        uf.unite(s, t);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        if (uf.same(s, t)){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>

using namespace std;

struct uf_find_tree {
    vector<int> par, sizes;
    uf_find_tree(int n) : par(n), sizes(n, 1) {
        for (int i = 0; i < n; i++) { par[i] = i; }
    }
    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }
    void unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return;
        if (sizes[x] < sizes[y]) { swap(x, y); }
        par[y] = x;
        sizes[x] += sizes[y];
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return sizes[find(x)]; }
};

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

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

    uf_find_tree uf(n);

    while (m--) {
        scanf("%d %d", &s, &t);
        uf.unite(s, t);
    }

    scanf("%d", &q);

    while (q--) {
        scanf("%d %d", &s, &t);
        printf((uf.same(s, t)) ? "yes\n" : "no\n");
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define MAX 100000
#define 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[t].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<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100000
#define NIL -1

/*

G':1-2-4 3-5
color:1 2

*/

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

void dfs(int u, int c) {
	stack<int> S;
	S.push(u);
	color[u] = 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);
			}
		}
	}
}

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

	fill(color, color + MAX, NIL);
	int id = 1;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)dfs(u, id++);
	}

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); i++)

#define MAX 100000
#define NIL -1

void dfs(int start_point, int id, vector<int> *graph, int *color, int n){
    
    stack<int> S;
    S.push(start_point);
    color[start_point] = id;

    while(!S.empty()){
        int node = S.top(); S.pop();
        rep(i, graph[node].size()){
            int v = graph[node][i];
            if(color[v] == NIL){
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void assignColor(vector<int> *graph, int *color, int n){
    int id = 1;
    rep(i, n){
        if(color[i] == NIL){
            dfs(i, id++, graph, color, n);
        }
    }
}

int main() {

    int n, m;
    cin >> n >> m;
    vector<int> graph[MAX];
    int color[MAX];
    rep(i, n){
        color[i] = NIL;
    }

    rep(i, m){
        int s, t;
        cin >> s >> t;
        graph[s].push_back(t);
        graph[t].push_back(s);
    }

    assignColor(graph, color, n);

    int q;
    cin >> q;
    rep(i, q){
        int x, y;
        cin >> x >> y;
        if(color[x] == color[y]){
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }

    return 0;
}