Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[2500][100];
    int *dist;

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);

	dist = new int [n];

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

	delete [] dist;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define NIL -1

// 頂点の個数
int n;
// 隣接リスト
vector<vector<int> > G(100000, vector<int>(0));
// 頂点のグループidを格納する配列
vector<int> color(100000, NIL);

// 深さ優先探索でuを含む連結成分をグループ(id)分け(uはグループ分けされていないと仮定)
void dfs(int u, int id)
{
  stack<int> S;
  S.push(u);
  color.at(u) = id;
  while (!S.empty())
  {
    int v = S.top();
    S.pop();
    for (int i = 0; i < G.at(v).size(); i++)
    {
      if (color.at(G.at(v).at(i)) == NIL)
      {
        // vの隣接頂点で未発見(idがない)のものをスタックに積んでidをつける
        S.push(G.at(v).at(i));
        color.at(G.at(v).at(i)) = id;
      }
    }
  }
}

void assignIds()
{
  int id = 1;
  for (int i = 0; i < n; i++)
  {
    if (color.at(i) == NIL)
    {
      dfs(i, id);
      id++;
    }
  }
}

int main()
{
  int m;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
  {
    int u, v;
    cin >> u >> v;
    G.at(u).push_back(v);
    G.at(v).push_back(u);
  }

  assignIds();

  int q;
  cin >> q;
  vector<bool> ans(q);
  for (int i = 0; i < q; i++)
  {
    int u, v;
    cin >> u >> v;
    ans.at(i) = (color.at(u) == color.at(v));
  }

  for (int i = 0; i < q; i++)
  {
    if (ans.at(i))
    {
      cout << "yes" << endl;
    }
    else
    {
      cout << "no" << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, m;
vector< list<int> > v;
vector<bool> used;

bool dfs(int pos, int dest){
  if(used[pos]) return false;
  used[pos] = true;
  if(pos == dest) return true;
  //for(int i = 0; i < v[pos].size(); i++){
  for(list<int>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
    if(dfs(*it, dest)){
      return true;
    }
  }
  return false;
}

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

  cin >> n >> m;
  v = vector< list<int> >(n, list<int>(0));
  used = vector<bool>(n, false);
  for(int i = 0; i < m; i++){
    int a, b; cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int q; cin >> q;
  for(int i = 0; i < q; i++){
    used = vector<bool>(n, false);
    int a, b; cin >> a >> b;
    if(dfs(a, b)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<functional>
#include<algorithm>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<map>
#include<utility>
#include<string>
#include<vector>

using namespace std;

#define LL long long
const int dx[4] = { 1,-1,0,0 };
const int dy[4] = { 0,0,1,-1 };
bool OOA(int x,int y,int H,int W){return (0<=x&&x<H)&&(0<=y&&y<W);}

struct Graph {
	int V, E, t;
	//vector<int>cost;
	vector<int>gr;
	//vector<bool>vzt;
	vector<vector<int>>adj;
	void init(int dflt) {//dflt!=0
		adj = vector<vector<int>>(V + 1);
	//	cost = vector<int>(V + 1, dflt);
		gr = vector<int>(V + 1, dflt);
	//	vzt = vector<bool>(V + 1, false);
	}

	void dfs(int now) {
		if (gr[now] == -1) { gr[now] = t; }
		else return;
		for (int i = 0;i<adj[now].size();i++)dfs(adj[now][i]);
		return;
	}

	void CC(){
		for(int i=0;i<V;i++){
			if(gr[i]==-1){
				t=i;
				dfs(i);
			}
		}
		return;
	}
};

Graph G;
int n,m,q;

int main() {
	cin>>n>>m;
	G.V=n;
	G.init(-1);
	for(int i=0;i<m;i++){
		int s,t;
		cin>>s>>t;
		G.adj[s].push_back(t);
		G.adj[t].push_back(s);
	}
	G.CC();
	cin>>q;
	for(int i=0;i<q;i++){
		int s,t;
		cin>>s>>t;
		cout<<(G.gr[s]==G.gr[t]?"yes":"no")<<endl;
	}
	return 0;
}