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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i,n)   for (int i = 0; i < (n); ++i)
#define sort(a)    sort((a).begin(), (a).end());
#define uniq(a)    SORT(a);(a).erase(unique((a).begin(), (a).end()), (a).end());
#define find(a,x)  find((a).begin(), (a).end(), (x)) != (a).end()
#define reverse(a) reverse((a).begin(), (a).end());
#define out(d) cout << (d);
#define outl(d) std::cout<<(d)<<"\n";
#define Yes() printf("Yes\n");
#define No() printf("No\n");
#define YES() printf("YES\n");
#define NO() printf("NO\n");
#define ceil(x, y) ((x + y - 1) / (y))

using namespace std;
using ll = long long;
using P = pair<ll,ll>;

static const ll MOD = 1000000007; // 10^9 + 7
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;
  rep(i, n) color[i] = NIL;
  rep(u, n) {
    if (color[u] == NIL) dfs(u, id++);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));

  int s, t, m, q;
  cin >> n >> m;

  rep(i, m) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

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

C++: #include<cstdio>
#include<vector>
using namespace std;
#define MAX 100000;

void DepthFirstSearch(int present_vertex, bool has_not_been_discovered[], vector<int> Adjacency_list[], int Group_number[], int group_number){
	int pv = present_vertex;
	has_not_been_discovered[pv] = false;
	Group_number[pv] = group_number;
	for(int i = 0;i < Adjacency_list[pv].size();++i){
		if(has_not_been_discovered[Adjacency_list[pv][i]]){
			DepthFirstSearch(Adjacency_list[pv][i], has_not_been_discovered, Adjacency_list, Group_number, group_number);
		}
	}
}

void DfsInit(int n, bool has_not_been_discovered[]){
	for (int i = 0; i < n; ++i)
		has_not_been_discovered[i] = true;
}

int main(){
	int number_of_users, number_of_friendships;
	scanf("%d %d", &number_of_users, &number_of_friendships);
	vector<int> Adjacency_list[number_of_users];
	for (int i = 0; i < number_of_friendships; ++i)	{
		int friend1, friend2;
		scanf("%d %d", &friend1, &friend2);
		Adjacency_list[friend1].push_back(friend2);
		Adjacency_list[friend2].push_back(friend1);
	}
	bool has_not_been_discovered[number_of_users];
	DfsInit(number_of_users, has_not_been_discovered);
	int Group_number[number_of_users];
	int group_number = 1;
	for (int user_i = 0; user_i < number_of_users; ++user_i){
		if(has_not_been_discovered[user_i])
			DepthFirstSearch(user_i, has_not_been_discovered, Adjacency_list, Group_number, group_number++);
	}
	int number_of_questions;
	scanf("%d", &number_of_questions);
	for (int i = 0; i < number_of_questions; ++i) {
		int user1, user2;
		scanf("%d %d", &user1, &user2);
		if(Group_number[user1] == Group_number[user2])
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}

C++: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20180307_AOJ
{
    class ALDS1_11D
    {
        static Node[] nodes;
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int[] n_m = line.Split(' ').Select(int.Parse).ToArray();
                int n = n_m[0];
                int m = n_m[1];

                nodes = new Node[n];
                for(int i=0;i<n;i++)
                {
                    nodes[i] = new Node();
                }
                for(int i=0;i<m;i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    int s = s_t[0];
                    int t = s_t[1];
                    nodes[s].Friends.Add(t);
                    nodes[t].Friends.Add(s);
                }

                int q = int.Parse(Console.ReadLine());
                for(int i=0;i<q;i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    int s = s_t[0];
                    int t = s_t[1];
                    bool isFriend = IsFriend(s, t, new List<int> { s });
                    if(isFriend)
                    {
                        Console.WriteLine("yes");
                    }
                    else
                    {
                        Console.WriteLine("no");
                    }
                }
            }
        }

        static bool IsFriend(int s, int t, List<int> visited)
        {
            bool rv = false;
            Node ns = nodes[s];
            if (ns.Friends.Contains(t))
            {
                rv = true;
            }
            else
            {
                foreach (var v in ns.Friends)
                {
                    if (!visited.Contains(v))
                    {
                        visited.Add(v);
                        rv = IsFriend(v, t, visited);
                        if (rv)
                        {
                            break;
                        }
                    }
                }
            }

            return rv;
        }
        
    }

    class Node
    {
        public HashSet<int> Friends { get; set; }
        public Node()
        {
            Friends = new HashSet<int>();
        }
    }
}