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 <stack>
#include <stdio.h>
#include <vector>

using namespace std;

#define NMAX 100000
#define NIL -1

int n;
stack<int> S;
vector<int> adj[NMAX + 1]; // 0-origin
int d[NMAX + 1];           // if visited
int c[NMAX + 1];           // color

int next_unvisited() {
    for (int i = 0; i < n; i++) {
        if (d[i] == 0)
            return i;
    }
    return NIL;
}

int next_unvisited_adj(int u) {
    vector<int>::iterator it;
    for (it = adj[u].begin(); it != adj[u].end(); it++) {
        if (d[*it] == 0)
            return *it;
    }
    return NIL;
}

void dfs() {
    int x; // start vertex;
    int round = 0;
    while ((x = next_unvisited()) != NIL) {
        d[x] = 1;
        c[x] = round;
        S.push(x);
        while (!S.empty()) {
            int u = S.top();
            int v = next_unvisited_adj(u);
            if (v != NIL) {
                if (d[v] == 0) {
                    d[v] = 1;
                    c[v] = round;
                    S.push(v);
                }
            } else {
                S.pop();
            }
        }
        round++;
    }
}

int main() {
    int m, q, u, v;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 0; i < n; i++)
        d[i] = 0;
    dfs();
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &u, &v);
        if (c[u] == c[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_A
// Graph I - Connected Components

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

const int NIL = -1;
const ui64 MAX_N = 100000;

vector<int> adj[MAX_N];
int assigned_group_ids[MAX_N];
int n, m, q;
int group_id;

void dfs(int u, int id) {
    assigned_group_ids[u] = id;
    // search adj nodes
    for (const auto& v : adj[u]) {
        if (assigned_group_ids[v] == NIL) dfs(v, id);
    }
}

void assign_group() {
    memset(assigned_group_ids, NIL, sizeof(assigned_group_ids));
    group_id = 0;

    for (int u = 0; u < n; ++u) {
        if (assigned_group_ids[u] == NIL) {
            dfs(u, group_id);
            group_id++;
        }
    }
}

int main() {
    memset(adj, 0, sizeof(adj));
    cin >> n >> m;

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

    assign_group();

    cin >> q;

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

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX = 100000;
const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];
void dfs(int r,int c) {
    if(color[r] == NIL) color[r] = c;
    for(int i = 0;i < G[r].size();i++) {
        int v = G[r][i];
        if(color[v] == NIL) dfs(v,c);
    }
}
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;
    scanf("%d%d",&n,&m);
    for(int i = 0;i < m;++i) {
        scanf("%d%d",&s,&t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
    assignColor();
    scanf("%d",&q);
    for(int i = 0;i < q;++i) {
        scanf("%d%d",&s,&t);
        if(color[s] == color[t]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

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

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector<int> M[N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}