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++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_D: Connected Components

#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>

using uint = unsigned int;

class Graph {
  std::vector<std::vector<uint>> g_;
  std::vector<int> root_;
  enum { NIL = -1, IN_STACK= -2 };

public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

  void add(uint from, uint to)
  {
    g_[from].push_back(to);
    g_[to].push_back(from);
  }

  void setup()
  {
    std::fill(root_.begin(), root_.end(), NIL);
    for (auto i = 0u; i < g_.size(); ++i ) {
      if (root_[i] == NIL) {
        dfs(i);
      }
    }
  }

  bool reachable(uint from, uint to) const
  {
    return root_[from] == root_[to];
  }

private:
  void dfs(uint root)
  {
    std::stack<uint> s;
    s.push(root);
    while (!s.empty()) {
      auto i = s.top();
      s.pop();
      root_[i] = root;
      for (auto j : g_[i]) {
        if (root_[j] == NIL) {
          root_[j] = IN_STACK;
          s.push(j);
        }
      }
    }
  }
};

int main()
{
  uint n, m;
  std::scanf("%u %u", &n, &m);

  Graph g(n);

  uint s, t;
  for (auto i = 0u; i < m; ++i) {
    std::scanf("%u %u", &s, &t);
    g.add(s, t);
  }
  g.setup();

  uint q;
  std::scanf("%u", &q);
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%u %u", &s, &t);
    std::puts(g.reachable(s, t) ? "yes" : "no");
  }
}

// eof

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

int d[MAX];
vector<int> V[MAX];
int tt=0,n,m;
queue<int> Q;

void gogo(int x){
  Q.push(x);
  d[x]=tt;
  int v;

  while(!Q.empty()){
    v=Q.front(); Q.pop();
    for(int i=0;i<V[v].size();i++){
      if(d[V[v][i]]==-1) {
        Q.push(V[v][i]);
        d[V[v][i]]=d[v];

      }
    }
  }

}

void gostart(){
  for(int i=0;i<n;i++){
    if(d[i]==-1) {
      gogo(i);
      tt++;
    }
  }
}

int main()  {
  cin>>n>>m;
  for(int i=0;i<n;i++){
    d[i]=-1;
  }

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

  gostart();

  int q;
  cin>>q;
  int k,l;
  for(int i=0;i<q;i++){
    cin>>k>>l;
    if(d[k]==d[l]) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
}

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

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> Cluster;
	int n;

	Graph(int n);

	bool isConnected(int s, int t);

	void MakeCluster();

	void Link(int s, int t);

};

Graph::Graph(int n){
	A.resize(n);
	Cluster.resize(n);
	for (int i = 0; i < n; i++) Cluster[i] = 0;
	this->n = n;
}

void Graph::Link(int s, int t){
	A[s].push_back(t);
	A[t].push_back(s);
	return;
}

void Graph::MakeCluster(){
	int clusternum = 0;
	std::list<int> Q;
	for (int i = 0; i < n; i++){
		if (Cluster[i] == 0){
			Q.clear();
			clusternum++;
			Cluster[i] = clusternum;
			Q.push_back(i);
			bool res = false;
			while (!Q.empty()){
				int f = Q.front();
				Q.pop_front();
				for (std::vector<int>::iterator itr = A[f].begin(); itr != A[f].end(); itr++){
					if (Cluster[*itr] == false){
						Cluster[*itr] = clusternum;
						Q.push_back(*itr);
					}
				}
			}
		}
	}
	return;
}

bool Graph::isConnected(int s, int t){
	return Cluster[s] == Cluster[t];
}

int main(){
	int n, m, s, t, q;
	scanf("%d%d", &n, &m);
	Graph G(n);
	for (int i = 0; i < m; i++){
		scanf("%d%d", &s, &t);
		G.Link(s, t);
	}
	scanf("%d", &q);
	G.MakeCluster();
	for (int i = 0; i < q; i++){
		scanf("%d%d", &s, &t);
		if (G.isConnected(s, t)) printf("yes\n"); else printf("no\n");
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;

int a[100000];
int search(int k){
  if(a[k]==k){
      return k;
  }
  int p=search(a[k]);
  a[k]=p;
  return p;
}

int main(){
  for(int i=0;i<100000;i++){
    a[i]=i;
  }
  int n,m,p,q,r,s,t;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>p>>q;
    a[search(p)]=search(q);
  }
  cin>>r;
  for(int i=0;i<r;i++){
    cin>>s>>t;
    if(search(s)==search(t)){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty()){
        que.pop();
    }
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

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();
        }

        que=refresh(que,node,n);
    }

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