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

using namespace std;

#define ll long long
#define vi vector<int>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

#define NIL -1

int N,M,Q,s,t;
vi T[100005], Color;

void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    Color[r] = c;
    while(!S.empty()){
        int u = S.top(); S.pop();
        rep(i, T[u].size()){
            int v = T[u][i];
            if(Color[v] == NIL){ //色が違っていたらスタックに入れて探索する
                Color[v] = c;
                S.push(v);
            }
        }
    }
}

void AssignColor(){
    int id = 1;

    Color.resize(N,NIL);

    rep(u,N){ //色が違っていたらスタックに入れて探索する
        if(Color[u] == NIL) dfs(u,id++);
    }
}

void solve(int s, int t){
    if(Color[s] == Color[t]) cout << "yes" << endl;
    else cout << "no" << endl;
}

int main(){
    cin >> N >> M;

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

    AssignColor();

    cin >> Q;

    rep(i,Q){
        cin >> s >> t;
        solve(s, t);
    };
}

C++: #include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
const long long INFLL = 1LL << 60;
const int INFINT = 1 << 30;
const long long MOD = 1e9 + 7;

vector<vector<int>> G(101, vector<int>(0));
vector<int> c(101, 0);
vector<int> d(101, 0);
vector<int> f(101, 0);

template <class T> using vec_alias = ::std::vector<T>;

template <class T> void vecout(T V) {
    auto t = V.begin();
    while(t != V.end()) {
        cout << " " << *t++;
        // if(t != V.end()) cout << " ";
    }
    cout << endl;
};

int t = 0;
void dfs(int i) {
    if(c[i]) return;
    c[i] = 1;
    d[i] = ++t;
    for(auto e : G[i])
        dfs(e);
    f[i] = ++t;
}

class node {
  public:
    int dist = -1;
    vector<int> nex;
};

template <template <class> class Container> class union_find {
  protected:
    class node_type;

  public:
    using container_type = Container<node_type>;
    using size_type = typename container_type::size_type;

  protected:
    class node_type {
      public:
        typename union_find::size_type parent;
        typename union_find::size_type size;
    };

    container_type tree;

  public:
    union_find() : tree() {}
    explicit union_find(const size_type size) : tree(size, {0, 1}) {
        for(size_type i = 0; i < size; ++i)
            tree[i].parent = i;
    }

    bool empty() const { return tree.empty(); }
    size_type size() const { return tree.size(); }

    size_type find(size_type x) {
        assert(x < size());
        while(tree[x].parent != x)
            x = tree[x].parent = tree[tree[x].parent].parent;
        return x;
    }
    bool same(const size_type x, const size_type y) {
        assert(x < size());
        assert(y < size());
        return find(x) == find(y);
    }
    size_type size(const size_type x) {
        assert(x < size());
        return tree[find(x)].size;
    }

    ::std::pair<size_type, size_type> unite(size_type x, size_type y) {
        assert(x < size());
        assert(y < size());
        x = find(x);
        y = find(y);
        if(x != y) {
            if(tree[x].size < tree[y].size) ::std::swap(x, y);
            tree[x].size += tree[y].size;
            tree[y].parent = x;
        }
        return {x, y};
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, q, s, t;
    cin >> n >> m;
    union_find<vec_alias> uf(n);
    rep(i, m) {
        cin >> s >> t;
        uf.unite(s, t);
    }
    cin >> q;
    rep(i, q) {
        cin >> s >> t;
        cout << (uf.same(s, t) ? "yes" : "no") << "\n";
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
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;
  for (int i = 0; i < n; i++) color[i] = NIL;
  for (int u = 0; u < n; u++) {
    if (color[u] == NIL) dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;

  cin >> n >> m;

  for ( int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
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();
    int i;

    for(i=0;i<G[u].size();i++){
      int v;
      v=G[u][i];
      if(color[v]==NIL){
	color[v]=c;
	S.push(v);
      }
    }
  }
}

void ass(){
  int id=1;
  int i,u;

  for(i=0;i<n;i++)
    color[i]=NIL;
  for(u=0;u<n;u++){
    if(color[u]==NIL)
      dfs(u,id++);
  }
}

int main(){
  int s,t,m,q;

  cin>>n>>m;

  int i;
  for(i=0;i<m;i++){
    cin>>s>>t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  ass();

  cin>>q;

  for(i=0;i<q;i++){
    cin>>s>>t;
    if(color[s]==color[t]){
      cout<<"yes"<<endl;
    }
    else{
      cout<<"no"<<endl;
    }
  }
  return 0;
}