Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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