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 <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;

using ll = long long;

static const int MAX = 100000;
vector<vector<int>> G;
int color[MAX];

void input_graph(int m) {
    int s, t;
    rep(i, m) {
        scanf("%d %d", &s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
}

void init_graph(int n) {
    rep(i, n) {
        vector<int> vec;
        G.push_back(vec);
    }
}
void bfs(int s, int color_id) {
    queue<int> que;
    que.push(s);
    color[s] = color_id;
    while(que.size() > 0) {
        int u = que.front();
        que.pop();
        rep(i, G[u].size()) {
            int v = G[u][i];
            if(color[v] == -1) {
                que.push(v);
                color[v] = color_id;
            }
        }
    }
}

void color_assign(int n) {
    int color_id = 1;
    rep(i, n) { color[i] = -1; }
    rep(i, n) {
        if(color[i] == -1) {
            bfs(i, color_id++);
        }
    }
}

int main() {
    // 制約見て！！！デカかったらlong longにすること！
    int n, m, q, s, t;
    cin >> n >> m;
    init_graph(n);
    input_graph(m);
    color_assign(n);
    cin >> q;
    rep(i, q) {
        scanf("%d %d", &s, &t);
        if(color[s] == color[t]) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
}

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

struct Node {
	int ID;
	int rep;
	bool visited;
	bool operator<(const Node& rhs) {return this->ID < rhs.ID;}
	bool operator>(const Node& rhs) {return this->ID > rhs.ID;}
	bool operator<=(const Node& rhs) {return this->ID <= rhs.ID;}
	bool operator>=(const Node& rhs) {return this->ID >= rhs.ID;}
	bool operator==(const Node& rhs) {return this->ID == rhs.ID;}
	bool operator!=(const Node& rhs) {return this->ID != rhs.ID;}
};

void BFS(std::vector<int>* adjlist, Node* nodes, std::queue<int> que)
{	
	while (!que.empty()) {
		int id = que.front(); que.pop();
		for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
			if (!nodes[*it].visited) { 
				que.push(*it);
				nodes[*it].rep = nodes[id].rep;
				nodes[*it].visited = true;
			}
		}
	}
}

void DFS(std::vector<int>* adjlist, Node* nodes, int id, int rep)
{
	nodes[id].rep = rep;
	nodes[id].visited = true;
	
	for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
		if (!nodes[*it].visited) DFS(adjlist, nodes, *it, rep);
	}
}

int main()
{
	int n, m; std::cin >> n >> m;
	
	std::vector<int>* adjlist = new std::vector<int>[n];
	Node* nodes = new Node[n];
	
	std::queue<int> que;
	
	for (int i=0; i<n; i++) {
		nodes[i].visited = false;
	}
	
	for (int i=0; i<m; i++) {
		int x, y;
		std::cin >> x >> y;
		adjlist[x].push_back(y);
		adjlist[y].push_back(x);
	}
	
	//std::sort(nodes, nodes + n);
	
	for (int i=0; i<n; i++) {
		if (!nodes[i].visited) {
			//que.push(i); nodes[i].visited = true; nodes[i].rep = i;
			//BFS(adjlist, nodes, que);
			DFS(adjlist, nodes, i, i);
		}
	}
	
	int q; std::cin >> q;
	
	for (int i=0; i<q; i++) {
		int x, y;
		std::cin >> x >> y;
		if (nodes[x].rep == nodes[y].rep) std::cout << "yes" << std::endl;
		else std::cout << "no" << std::endl;
	}
	
	delete[] adjlist;
	delete[] nodes;
	
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;


int n;

string *color = new string[n];

queue<int> q;

int *ans = new int[n];

vector<int> g[10000];





//int c[100][100];
/*
void bfs(int x){
  color[0] = "gray";
  d[0] = 0;

  q.push(0);


  while(q.size() != 0){
    int u = q.front();
    q.pop();

    for(int v = 0; v < n; v++){
      if(c[u][v] == 1 && color[v] == "white"){
        color[v] = "gray";
        d[v] = d[u] + 1;
        q.push(v);
      }

    }

    color[u] = "black";
    ans[i++] = x;

  }
}
*/

void bfs(int x){
  color[x] = "gray";
  q.push(x);

  cout << "ok";


  while(q.size() != 0){
    int u = q.front();
    q.pop();

    for(int i = 0; i < g[u].size(); i++){
      int v = g[u][i];
      color[v] = "gray";
      q.push(v);
    }

    color[u] = "black";


  }
  for(int i = 0; i < n; i++){
    if(color[i] == "black"){
      ans[i] = x;
    }
  }
}


int main(){

  cin >> n;

  color = new string[n];

  int m;

  cin >> m;

  cout << n << m;


  int a, b;


  for(int i = 0; i < m; i++){

    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
    cout << i << "  " << m << endl;
  }

  cout << "ok";

  for(int i = 0; i < n; i++){
    color[i] = "white";
  }

  cout << "ok";


  for(int i = 0; i < n; i++){
    cout << "ok";
    if(color[i] == "white"){
      bfs(i);
    }
  }

  int q;

  /*cin >> q;

  for(int i = 0; i < q; i++){
    int a, b;
    cin >> a >> b;
    if(ans[a] == ans[b]){
      cout << "yes" << endl;
    }
    else{
      cout << "no" << endl;
    }
  }
*/





  return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
static const int MAX = 100000;
static const int INF = 1e9;

vector<int> V[MAX];
int n,m,s,t,q,d[MAX];

int bfs(int a,int b){
	for(int i = 0;i < n;i++) d[i] = INF;
	queue<int> qu;
	qu.push(a);
	d[a] = 0;
	while(!qu.empty()){
		int v = qu.front();qu.pop();
		for(int i = 0;i < n;i++){
			if(V[v][i] <= 0) continue;
			if(d[i] != INF) continue;
			d[i] = d[v] + 1;
			qu.push(i);
		}
	}
	return d[b];
}

int main(){
	cin >> n >> m;
	for(int i = 0;i < m;i++){
		cin >> s >> t;
		V[s].push_back(t);
		V[t].push_back(s);
	}
	cin >> m;
	for(int i = 0;i < m;i++){
		cin >> s >> t;
		if(bfs(s,t) != INF){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}	
	return 0;
}