Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_D: Connected Components

#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>

using uint = unsigned int;

class Graph {
  std::vector<std::vector<uint>> g_;
  std::vector<uint> root_;
  
public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

  void add(uint from, uint to)
  {
    g_[from].push_back(to);
  }

  void setup()
  {
    std::fill(root_.begin(), root_.end(), UINT_MAX);
    for (auto i = 0u; i < g_.size(); ++i ) {
      dfs(i, i);
    }
  }

  bool reachable(uint from, uint to) const
  {
    return root_[from] == root_[to];
  }

private:
  void dfs(uint i, uint root)
  {
    if (root_[i] != UINT_MAX) return;
    root_[i] = root;
    for (auto j = 0u; j < g_[i].size(); ++j) {
      dfs(g_[i][j], root);
    }
  }
  
};

int main()
{
  uint n, m;
  std::scanf("%u %u", &n, &m);

  Graph g(n);

  uint s, t;
  for (auto i = 0u; i < m; ++i) {
    std::scanf("%u %u", &s, &t);
    g.add(s, t);
  }
  g.setup();

  uint q;
  std::scanf("%u", &q);
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%u %u", &s, &t);
    std::puts(g.reachable(s, t) ? "yes" : "no");
  }
}

// eof

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG true
// sort(a.begin(), a.end(), std::greater<int>());

/*
std::vector<std::string> split(const std::string &input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter))
    {
        result.push_back(field);
    }
    return result;
}
*/
pair<int, int> data[100000];
bool finds[100000];
int n, m;
/*
int dfs(int foo, int timer)
{
    //foo is node number
    if (finds[foo].first != -1)
        return timer;
    finds[foo].first = ++timer;
    int result = timer;
    if (data[foo].size() != 0)
    {
        rep(i, data[foo].size())
        {
            result = dfs(data[foo][i], result);
        }
    }
    finds[foo].second = ++result;
    return result;
}
*/

bool dfs(int foo, int goal)
{
    bool ans = false;
    rep(i, m)
    {
        if (data[i].first == foo && data[i].second == goal)
            return true;
        if (data[i].first == foo)
        {
            if (finds[i] == true)
                continue;
            finds[i] = true;
            ans = dfs(data[i].second, goal);
        }
    }
    return ans;
}
int main()
{
    cin >> n >> m;
    rep(i, m)
    {
        cin >> data[i].first >> data[i].second;
    }
    int q;
    cin >> q;
    rep(i, q)
    {
        int left, right;
        cin >> left >> right;
        if (dfs(left, right) || dfs(right, left))
            cout << "yes";
        else
            cout << "no";
        cout << endl;
        rep(j, m) finds[j] = false;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;

template<u32 N_>
class UnionFindTree {
private:
  u32 par[N_];
  u32 rank[N_];
public:
  UnionFindTree(u32 n) {
    assert(n < N_);
    for(u32 i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
    return;
  }
  int find(u32 x) {
    if( par[x] == x ) return x;
    return par[x] = find(par[x]);
  }
  void unite(u32 x, u32 y) {
    x = find(x);
    y = find(y);
    if( x == y ) return;
    if( rank[x] < rank[y] ) {
      par[x] = y;
      return;
    }
    par[y] = x;
    if( rank[x] == rank[y] ) rank[x] += 1;
    return;
  }  
};

int main() {

  u32 n, m;
  std::cin >> n >> m;

  UnionFindTree<200000> uft(m);

  for(u32 i = 0; i < m; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    uft.unite(x, y);
  }

  u32 q;
  std::cin >> q;

  for(u32 i = 0; i < q; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    std::cout << (uft.find(x) == uft.find(y) ? "yes" : "no") << std::endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

void set_color(const vector<vector<int> >& u, int p, vector<int>& color, int c) {
    if (color[p] != -1) return;
    color[p] = c;
    for (int i = 0; i < u[p].size(); i++) {
        set_color(u, u[p][i], color, c);
    }
}

bool is_friend(const vector<vector<int> >& u, int s, int t){
    vector<int> color(u.size(), -1);
    int c = 0;
    set_color(u, s, color, c++);
    return color[s] == color[t];
}
int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > u(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        u[s].push_back(t);
        u[t].push_back(s);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        if (is_friend(u, s, t)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}