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 <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
void refresh(queue<Node> que,Node node[],int n){
    while(!que.empty())
        que.pop();

    for(int i=0; i<n; i++)
        node[i].flag=false;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

    for(int i=0; i<m; i++){
        cin >> s >> t;

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(x==t){
                answer[i]=true;
                break;
            }

            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);
            }
         }

          que.pop();
        }

        refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}

C++: //
// Created by tattsun on 2017/02/14.
//

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

#define VERTEX_MAX 100001

typedef vector<int> Vertex;

int color = 0;
int n, m;
Vertex vs[VERTEX_MAX];
int cs[VERTEX_MAX];

void _dfs(int color, int i) {
    cs[i] = color;

    auto& v = vs[i];
    for(auto it = v.begin(); it != v.end(); it++) {
        if(cs[*it] != -1)
            continue;

        _dfs(color, *it);
    }
}

void dfs() {
    for(int i=0; i<n; i++) {
        if(cs[i] != -1)
            continue;

        _dfs(color, i);
        color++;
    }
}

int main() {
    for(int i=0; i<VERTEX_MAX; i++)
        cs[i] = -1;

    cin >> n >> m;

    int a, b;
    for(int i=0; i<m; i++) {
        cin >> a >> b;
        vs[a].push_back(b);
        vs[b].push_back(a);
    }

    dfs();

    int q;
    cin >> q;
    for(int i=0; i<q; i++) {
        cin >> a >> b;
        if(cs[a] == cs[b] && cs[a] != -1)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}

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

void dfs(std::vector<std::vector<int>> &n) {
	/*
	std::stack<int> s;
	std::vector<std::vector<int> > nn(n.size());
	std::vector<int> dd(n.size());
	int count = 0;

	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		nn[count].push_back(i);
		std::vector<int> d;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d.push_back(num);
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				if (std::find(d.begin(), d.end(), *it) == d.end()) {
					nn[count].push_back(*it);
					s.push(*it);
					d.push_back(*it);
				}
			}
		}
		count++;
	}
	*/
	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		std::stac	std::vectork<int> s;
	<int> went;
		s.push(from);
		bool flag = false;
		while (s.size() != 0 && flag == false){
			int num = s.top(); s.pop();
			for (size_t j = 0; j < n[num].size(); j++) {
				if (n[num][j] == to) {
					flag = true;
					break;
				}
				if (std::find(went.begin(), went.end(), n[num][j]) == went.end()) {
					s.push(n[num][j]);
					went.push_back(n[num][j]);
				}
			}
		}
		if (flag) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}
	/*
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		for (int j = 0; j < count; j++) {
			if (std::find(nn[j].begin(), nn[j].end(), from) != nn[j].end() &&
				std::find(nn[j].begin(), nn[j].end(), to) != nn[j].end()) {
				std::cout << "yes" << std::endl;
				break;
			}
			if (j == count - 1) {
				std::cout << "no" << std::endl;
			}
		}
	}
	*/
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int>> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	dfs(n);

	return 0;
}

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

#define MAX 100000

using namespace std;

int visited[MAX];
//adjacent list
vector<int> G[MAX];

//can go from s to t or not
int color[MAX];

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

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

  //coloring
  for(int i=0;i<n;i++){
    visited[i]=0;
    color[i]=-1;
  }
  int c=0;
  for(int i=0;i<n;i++){
    if(color[i]==-1){
      dfs(i,c);
      c++;
    }
  }

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