Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

template <typename T> class union_find {
  vector<T> par, rank;

public:
  union_find(size_t n) {
    par.reserve(n + 1);
    rank.reserve(n + 1);
  }

public:
  void init(T n) { // add a set
    rep(i, 0, n) {
      par[i] = i;
      rank[i] = 0;
    }
  }

public:
  T find(T x) { // find the parent of x
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

public:
  void unite(T x, T y) { // unite x and y
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else
      par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }

public:
  bool same(T x, T y) { // compare x's parent and y's parent
    return find(x) == find(y);
  }
};

int n, m;
vector<int> s, t;
int q;  
int main() {
 ios::sync_with_stdio ( false );
 cin.tie ( 0 );
    cin >> n >> m;
    union_find<int> uf = union_find<int>(n+1);
    uf.init(n);
    rep(i, 0, m) {
        int a, b;
        cin >> a >> b;
        s.pb(a); t.pb(b);
        uf.unite(a, b);
    }
    cin >> q;
    rep(i, 0, q) {
        int k, l; cin >> k >> l;
        if (uf.same(k, l)) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

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

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent;
    vector<int> child;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n)
        person[i].parent = -1;
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1 && person[t].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
            person[s].child.push_back(t);
        } else if(person[s].parent == -1){
            person[s].parent = person[t].parent;
            person[t].child.push_back(s);
        } else if(person[t].parent == -1){
            person[t].parent = person[s].parent;
            person[s].child.push_back(t);
        } else {
            person[t].parent = person[s].parent;
            REP(j, person[t].child.size()){
                person[person[t].child[j]].parent = person[s].parent;
                person[person[s].parent].child.push_back(person[t].child[j]);
            }
            person[t].child.clear();
        }
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n, m;
vector<int> color(MAX, NIL);
vector<vector<int>> M(MAX);

void dfs(int i, int id) {
  int size = M[i].size();
  for (int j = 0; j < size; j++) {
    if (color[M[i][j]] == NIL) {
      color[M[i][j]] = id;
      dfs(M[i][j], id);
    }
  }
}

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

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  assignColor();

  for (int i = 0; i < qn; i++) {
    if (color[q[i][0]] == color[q[i][1]]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i,n) for(UL i=0; i<(n); i++)

vector<UL> E[100000];
UL D[100000];

int main() {
 UL N,M; scanf("%u%u",&N,&M);
 rep(i,M){
  UL u,v; scanf("%u%u",&u,&v);
  E[u].push_back(v);
  E[v].push_back(u);
 }
 rep(i,N) D[i]=~0u;
 rep(s,N){
  if(D[s]!=~0u) continue;
  queue<UL> Q; Q.push(s); D[s]=s;
  while(Q.size()){
   UL p=Q.front(); Q.pop();
   for(UL e:E[p]){
    if(D[e]!=~0u) continue;
    D[e]=D[p];
    Q.push(e);
   }
  }
 }
 UL Q; scanf("%u",&Q);
 rep(q,Q){
  UL s,t; scanf("%u%u",&s,&t);
  if(D[s]==D[t]) printf("yes\n");
  else printf("no\n");
 }
 return 0;
}