Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>

#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i,n) for(int i=0;i<(n); i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;
typedef long long ll;
const int NMAX = 100000;
const int k = 200000;
static const int INF = 1<<21;
static const int NIL = -1;

int dp[3001][3001];
ll gcd(ll a, ll b){
if(a<b) swap(a,b);
    if(b==0) return a;
    return gcd(b, a%b);
}

int n,color[NMAX];
vector<int> G[NMAX];

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,G[u].size()){
            int v=G[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0); //この2行はより速度を向上させる
    // 追加２, 以降 cin の入力元が 'input.txt' になる


    int m,s,t,q;
    cin >> n >> m;
    rep(i,m){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s); //無向グラフなので双方に格納する
    }
    assignColor(); //色塗り

    cin >> q;

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

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

// union - find tree !!!!!!!!!!!!!!!!!!!!!!!!!
vector<int> par; //oya
vector<int> rnk; //ki no hu ka sa
// n????´???§?????????
void init(int n){
    par.resize(n);rnk.resize(n);
    FOR(i, 0, n){par[i] = i;rnk[i] = 0;}
}
//??¨??????????±???????
int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
//x??¨y????±??????????????????????
void Merge(int x, int y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y])par[x] = y;
    else{par[y] = x;if(rnk[x] == rnk[y]) rnk[x]++;}
    return;
}

int main(){
  int n, m; cin >> n >> m;
  init(n);
  FOR(i,0,m){
    int a, b;
    cin >> a >> b;
    Merge(a, b);
  }
  int q;
  cin >> q;
  FOR(i,0,q){
    int a, b;
    cin >> a >> b;
    if(find(a) == find(b)){
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

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

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF INT_MAX

vector< vector<int> > adj;
vector<int> serial;

void dfs(int v, int sn)
{
    serial[v] = sn;
    for (auto e : adj[v]) {
        if (serial[e]) continue;
        dfs(e, sn);
    }
}

int main(int, char**)
{
    int n, m;
    cin >> n >> m;

    adj.assign(n, vector<int>());
    int s, t;
    rep(i,m) {
        cin >> s >> t;
        adj[s].push_back(t);
        adj[t].push_back(s);
    }


    serial.assign(n, 0);
    int sn = 1;
    rep(i,n) {
        if (serial[i]) continue;
        dfs(i, sn++);
    }
    int q;
    cin >> q;
    rep(i,q) {
        cin >> s >> t;
        if (serial[s] == serial[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>
using namespace std;
#define MAX 100000

vector<int> G[MAX];
int color[MAX];
int c = 0;

void dfs(int v) {
    color[v] = c;
    for (int i = 0; i < G[v].size(); i++) {
        if (color[G[v][i]] == 0) dfs(G[v][i]);
    }
}

int main(void) {
    int n, m, s, t, q, i;
    scanf("%d %d", &n, &m);

    for (i = 0; i < m; i++) {
        scanf("%d %d", &s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }

    for (i = 0; i < n; i++) color[i] = 0;

    for (i = 0; i < n; i++) {
        if (color[i] == 0) {
            c++;
            dfs(i);
        }
    }

    scanf("%d", &q);
    for (i = 0; i < q; i++) {
        scanf("%d %d", &s, &t);
        if (color[s] > 0 && color[s] == color[t]) printf("yes\n");
        else printf("no\n");
    }

    return 0;
}

C++: #include"bits/stdc++.h"
using namespace std;

#define NIL -1

struct Node {
	int type = NIL;
	bool flag = false;
	vector<int> v;
};

void dfs(vector<Node>& u, int id, int type)
{
	if (u[id].flag) return;

	u[id].flag = true;
	u[id].type = type;
	for (int i = 0; i < u[id].v.size(); i++) dfs(u, u[id].v[i], type);

	return;
}

int main(void)
{
	int i, N, m, l, r, q, type = 0;
	cin >> N >> m;
	vector<Node> u(N);

	for (i = 0; i < m; i++)
	{
		cin >> l >> r;
		u[l].v.push_back(r);
		u[r].v.push_back(l);
	}

	for (i = 0; i < N; i++)
	{
		dfs(u, i, type);
		type++;
	}

	cin >> q;
	for (i = 0; i < q; i++)
	{
		cin >> l >> r;
		cout << ((u[l].type == u[r].type) ? "yes" : "no") << endl;
	}

	return 0;
}