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