Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

// union - find tree !!!!!!!!!!!!!!!!!!!!!!!!!
vector<int> par; //oya
vector<int> rnk; //ki no hu ka sa
// n????´???§?????????
void init(int n){
    par.resize(n);rnk.resize(n);
    FOR(i, 0, n){par[i] = i;rnk[i] = 0;}
}
//??¨??????????±???????
int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
//x??¨y????±??????????????????????
void Merge(int x, int y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y])par[x] = y;
    else{par[y] = x;if(rnk[x] == rnk[y]) rnk[x]++;}
    return;
}

int main(){
  int n, m; cin >> n >> m;
  init(n);
  FOR(i,0,m){
    int a, b;
    cin >> a >> b;
    Merge(a, b);
  }
  int q;
  cin >> q;
  FOR(i,0,q){
    int a, b;
    cin >> a >> b;
    if(find(a) == find(b)){
      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++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF INT_MAX

vector< vector<int> > adj;
vector<int> serial;

void dfs(int v, int sn)
{
    serial[v] = sn;
    for (auto e : adj[v]) {
        if (serial[e]) continue;
        dfs(e, sn);
    }
}

int main(int, char**)
{
    int n, m;
    cin >> n >> m;

    adj.assign(n, vector<int>());
    int s, t;
    rep(i,m) {
        cin >> s >> t;
        adj[s].push_back(t);
        adj[t].push_back(s);
    }


    serial.assign(n, 0);
    int sn = 1;
    rep(i,n) {
        if (serial[i]) continue;
        dfs(i, sn++);
    }
    int q;
    cin >> q;
    rep(i,q) {
        cin >> s >> t;
        if (serial[s] == serial[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>
using namespace std;
#define MAX 100000

vector<int> G[MAX];
int color[MAX];
int c = 0;

void dfs(int v) {
    color[v] = c;
    for (int i = 0; i < G[v].size(); i++) {
        if (color[G[v][i]] == 0) dfs(G[v][i]);
    }
}

int main(void) {
    int n, m, s, t, q, i;
    scanf("%d %d", &n, &m);

    for (i = 0; i < m; i++) {
        scanf("%d %d", &s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }

    for (i = 0; i < n; i++) color[i] = 0;

    for (i = 0; i < n; i++) {
        if (color[i] == 0) {
            c++;
            dfs(i);
        }
    }

    scanf("%d", &q);
    for (i = 0; i < q; i++) {
        scanf("%d %d", &s, &t);
        if (color[s] > 0 && color[s] == color[t]) printf("yes\n");
        else printf("no\n");
    }

    return 0;
}

C++: #include"bits/stdc++.h"
using namespace std;

#define NIL -1

struct Node {
	int type = NIL;
	bool flag = false;
	vector<int> v;
};

void dfs(vector<Node>& u, int id, int type)
{
	if (u[id].flag) return;

	u[id].flag = true;
	u[id].type = type;
	for (int i = 0; i < u[id].v.size(); i++) dfs(u, u[id].v[i], type);

	return;
}

int main(void)
{
	int i, N, m, l, r, q, type = 0;
	cin >> N >> m;
	vector<Node> u(N);

	for (i = 0; i < m; i++)
	{
		cin >> l >> r;
		u[l].v.push_back(r);
		u[r].v.push_back(l);
	}

	for (i = 0; i < N; i++)
	{
		dfs(u, i, type);
		type++;
	}

	cin >> q;
	for (i = 0; i < q; i++)
	{
		cin >> l >> r;
		cout << ((u[l].type == u[r].type) ? "yes" : "no") << endl;
	}

	return 0;
}