Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define  all(x) (x).begin(),(x).end()
typedef long long ll;

vector<int> frnd[100005];
vector<int> group(100005, 0);

void dfs(int node, int parentNode, int groupId){
    group[node] = groupId;
    for(int x: frnd[node]){
        if (x == parentNode) continue;
        if (group[x] != 0) continue;
        dfs(x, node, groupId);
    }
}

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

    int gropuId = 1;
    rep(i,n){
        if (group[i] != 0) continue;
        dfs(i, -1, gropuId);
        gropuId++;
    }

    int q;
    cin >> q;
    vector<string> ans(q);
    rep(i,q){
        int s, t;
        cin >> s >> t;
        if (group[s] == group[t]) ans[i] = "yes";
        else ans[i] = "no";
    }
    rep(i,q){
        cout << ans[i] << endl;
    }
}

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