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 <iostream>
#include <vector>
using namespace std;
vector<int> G[100];
vector<int> A;
int isVisited[100];

bool enableConnection(int l, int o)
{
    if (isVisited[l] == 1) {
	return false;
    }
    //?¨???????????????????????????????????????????????????????
    for (int i = 0; i < G[l].size(); i++) {
	if (G[l][i] == o) {
	    return true;
	}
	if (enableConnection(G[l][i], o)) {
	    return true;
	}
    }
    return false;
}

int main()
{
    int a, c;
    cin >> a;
    cin >> c;
    //setup
    for (int i = 0; i < c; i++) {
	int n;
	int m;
	cin >> n >> m;
	G[n].push_back(m);
	isVisited[i] = 0;
    }

    //then
    int b = 0;
    cin >> b;
    for (int i = 0; i < b; i++) {
	int l;
	int o;
	cin >> l;
	cin >> o;
	if (enableConnection(l, o) || enableConnection(o, l)) {
	    cout << "yes" << endl;
	} else {
	    cout << "no" << endl;
	}
    }
    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

/* DFS * TLE! *
bool canReach(int s, int d, bool* h){
	h[s] = true;
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(!h[nodes[s].rel[i]]){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}
*/

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		bool *h = new bool[n];
		for(int i = 0; i < n; i++){
			h[i] = false;
		}
		
		queue<int> qu;
		qu.push(l);
		while(qu.size()){
			h[qu.front()] = true;
			for(int i = 0; i < nodes[qu.front()].rel.size(); i++){
				if(nodes[qu.front()].rel[i] == r){
					puts("yes");
					goto BREAK;
				}else if(!h[nodes[qu.front()].rel[i]]){
					qu.push(nodes[qu.front()].rel[i]);
				}
			}
			qu.pop();
		}
		puts("no");
		BREAK:;
	}
	return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>
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[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;
		}
	}
}

C++: #include <stdio.h>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<int> relations[n];
  int index1, index2;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &index1, &index2);
    relations[index1].push_back(index2);
    relations[index2].push_back(index1);
  }
  int color[n], id;
  for (int i = 0; i < n; i++) {
    color[i] = -1;
  }
  id = 0;
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      id++;
      stack<int> S;
      S.push(i);
      while (!S.empty()) {
        int v = S.top(); S.pop();
        color[v] = id;
        for (int j = 0; j < relations[v].size(); j++) {
          int nv = relations[v][j];
          if (color[nv] == -1) {
            S.push(nv);
          }
        }
      }
    }
  }
  scanf("%d", &m);
  int u1, u2;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u1, &u2);
    if (color[u1] == color[u2]) printf("yes\n");
    else printf("no\n");
  }
  return 0;
}

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

using namespace std;

int main() {

	int n, m;
	cin >> n >> m;
	vector < vector<int>> G(n + 1);

	//グラフ隣接リスト読み込み
	int a, c;
	for (int i = 1; i <= m; i++) {
		cin >> a >> c;
		G[a].push_back(c);
		G[c].push_back(a);
	}

	//幅優先探索処理
	vector<bool> seen(n + 1, false);	//発見したらtrue
	deque<int> todo;					//行く所のスタック

	int s = 1;							//今いる場所

	bool brkflg;
	vector<long> color(n + 1);
	int colornum = 0;
	for (int j = 0; j < n; j++) {		//全ての頂点を調べる

		if (color[j] == 0) {
			colornum++;
			color[j] = colornum;
			brkflg = false;
			todo.clear();

			//for (int k = 0; k <= n; k++) {
			//	seen[k] = false;//一旦全クリア
			//}

			if (seen[j] == false) {			//訪れてなかったら処理すすめる
				seen[j] = true;
				todo.push_back(j);

				while (todo.empty() != true) {				//キューが空になるまで処理	
					s = todo.front();						//処理番号を指定
					int tugiflg = false;					//次があったらtrueにするフラグ
					for (int i = 0; i < G[s].size(); i++) {

						if (seen[G[s][i]] != true) {
							seen[G[s][i]] = true;
							color[G[s][i]] = colornum;
							todo.push_back(G[s][i]);
							tugiflg = true;
						}
					}
					todo.pop_front(); //次を全部探したので削除する
				}
			}
		}
	}

	int in1, in2;
	cin >> m;
	for (int i = 0; i <m; i++) {
		cin >> in1 >> in2;
		if (color[in1] == color[in2]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;

}