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<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<int> d(n.size()), dd(n.size());
	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		n[i].parent = -1;
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
					n[*it].parent = num;
					std::vector<int>::iterator itt= std::find(n[*it].relation.begin(), n[*it].relation.end(), num);
					if (itt != n[*it].relation.end()) {
						n[*it].relation.erase(itt);
					}
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
		if (from == to) return true;
	}
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);
	//makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
	std::cout << i << " " << n[i].parent << std::endl;
	}
	*/
	/*
	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	//*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

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

int find(vector<vector<int>> &frnd, vector<int> &state, int u){
    for (int i = 0; i < frnd[u].size(); i++){
        if (state[frnd[u][i]] == 0){
            return frnd[u][i];
        }
    }
    return -1;
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> frnd(n);
    int s, t;
    for (int i = 0; i < m; i++){
        cin >> s >> t;
        frnd[s].push_back(t);
        frnd[t].push_back(s);
    }
    vector<int> color(n, 0);
    int cl = 1;
    for (int i = 0; i < n; i++){
        stack<int> s;
        //vector<int> state(n, 0);
        if (color[i] == 0){
            s.push(i);
            color[i] = cl;
            //state[i]++;
            while(!s.empty()){
                int v = s.top();

                s.pop();
                for (int j = 0; j < frnd[v].size(); j++){
                    int u = frnd[v][j];
                    if (color[u] == 0){
                        color[u] = cl;
                        s.push(u);
                    }
                }
                /*int nxt = find(frnd, state, v);
                if (nxt != -1){
                    s.push(nxt);
                    color[nxt] = cl;
                    state[nxt]++;
                }
                else{
                    state[v]++;
                    s.pop();
                }*/
            }
        }
        cl++;
    }
    int q;
    cin >> q;
    int a, b;
    for (int i = 0; i < q; i++){
        cin >> a >> b;
        if (color[a] == color[b])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n)
      : connected_(n, vector<bool>(n, false)) {}
  void register_friend(int s, int t) {
    connected_[s][t] = connected_[t][s] = true;
  }
  bool can_reach(int s, int t) {
    if (s == t)
      return true;
    vector<bool> done(connected_.size(), false);
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      if (v == t)
        return true;
      if (done[v])
        continue;
      done[v] = true;
      auto& con = connected_[v];
      for (size_t i = 0; i < con.size(); ++i) {
        if (con[i]) {
          auto v2 = static_cast<int>(i);
          if (v2 == t)
            return true;
          if (!done[v2]) {
            q.push(v2);
          }
        }
      }
    }
    return false;
  }

 private:
  vector<vector<bool>> connected_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}