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++: #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;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<vector<int>> GetAdjacencyList(int n, int m)
{
	vector<vector<int>> al(n);
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		al[s].push_back(t);
		al[t].push_back(s);
	}
	return al;
}

void BFS(int i, const vector<vector<int>>& al, vector<int>& group)
{
	queue<int> q;
	q.push(i);
	group[i] = i + 1;
	while (!q.empty()) {
		int node = q.front(); q.pop();
		for (const auto& item : al[node]) {
			if (group[item]) continue;
			q.push(item);
			group[item] = i + 1;
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int>> al = GetAdjacencyList(n, m);
	vector<int> group(n);
	for (int i = 0; i < n; i++) {
		if (!group[i]) BFS(i, al, group);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (group[s] == group[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}