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<cinttypes>
#include<numeric>


using ui32 = std::uint32_t;

class union_find {
public:
	using size_type = decltype(std::declval<std::vector<int>>().size());
private:
	std::vector<size_type> par;
	std::vector<size_type> size_;

public:
	union_find() :par{}, size_{} {}
	union_find(size_type s) :union_find{} {
		par.resize(s);
		std::iota(std::begin(par), std::end(par), 0);

		size_.resize(s, 1);
	}

	auto root(size_type index_)->size_type {
		if (par[index_] == index_)
			return
				par[index_];
		else
			return
				par[index_] = root(par[index_]);
	}

	auto size(size_type index_) {
		return size_[root(index_)];
	}

	auto same(size_type ai, size_type bi) {
		return root(ai) == root(bi);
	}

	auto unite(size_type ai, size_type bi) {
		auto ar = root(ai);
		auto br = root(bi);

		if (ar == br)
			return;

		const auto as = size(ar);
		const auto bs = size(br);

		if (as >= bs)
			std::swap(ar, br);

		par[br] = par[ar];

		if(as == bs)
			size_[ar] = size_[br] = as + bs + 1;

	}

};

int main() {

	ui32 n, m, q;

	std::cin >> n >> m;
	union_find uf(n);
	for (ui32 i{}; i < m; ++i) {
		ui32 s, t;
		std::cin >> s >> t;
		uf.unite(s, t);
	}

	std::cin >> q;
	for (ui32 i{}; i < q; ++i) {
		ui32 s, t; 
		std::cin >> s >> t;
		const auto ans = uf.same(s, t)?"yes":"no";
		std::cout << ans << "\n";
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX=100005;
vector<int> G[MAX];
bool vis[MAX];
int group[MAX];
int n,m,number=0;
void dfs(int u){
	group[u]=number;
	vis[u]=1;
	for(unsigned int i=0;i<G[u].size();i++){
		if(!vis[G[u][i]])dfs(G[u][i]);
	}
}
int main(){
	int q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int u,v;
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=0;i<n;i++){
			if(!vis[i]){
				dfs(i);
				number++;
			}
		}
	cin>>q;
	for(int i=0;i<q;i++){
		int u,v;
		cin>>u>>v;
		printf("%s\n",((group[u]==group[v])?"yes":"no"));
	}
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#define N 100000
using namespace std;

int color[N];
vector<int> G[N];
queue<int> Q;
int n;

void bfs(int s,int c) {
	Q.push(s);
	color[s] = c;
	while (!Q.empty()) {
		for (unsigned int it = 0;it < G[Q.front()].size();it++)
			if (color[G[Q.front()][it]] == 0){
				Q.push(G[Q.front()][it]);
				color[G[Q.front()][it]] = c;
		    }
		Q.pop();
	 }
}

void Color() {
	int c = 0;
	for(int i=0;i<n;i++)
	if(color[i]==0) bfs(i, ++c);
}

int main() {
	int m, q;
	cin >> n >> m;
	for (int i = 0;i < m;i++) {
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
	}

	Color();

	cin >> q;
	for (int i = 0;i < q;i++) {
		int u, v;
		cin >> u >> v;
		if (color[u] == color[v] && color[u] != 0)
			cout << "yes" << endl;
		else
			cout << "no"<<endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<stack>
#include<queue>
#include<functional>
#include<iterator>
#include<cmath>

using namespace std;

typedef pair<int,int> P;


const int max_n = 1e5;

int par[max_n];
int rnk[max_n];

void init(int n){
	for(int i=0; i<n; i++){
		par[i] = i;
		rnk[i] = 0;
	}
}

int find_root(int x){
	if(par[x] == x) return x;
	else			return par[x] = find_root(par[x]);
}

void unite(int x, int y){
	x = find_root(x);
	y = find_root(y);
	if(x == y) return;

	if(rnk[x] < rnk[y]){
		par[x] = y;
	}
	else{
		par[y] = x;
		if(rnk[x] == rnk[y]) rnk[x]++;
	}
}

bool same(int x, int y){
	return find_root(x) == find_root(y);
}


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

	init(n);

	int s,t;
	for(int i=0;i<m;i++){
		cin >> s >> t;
		unite(s,t);
	}

	int q;
	cin >> 	q;
	for(int i=0;i<q;i++){
		cin >> s >> t;
		if(same(s,t)) cout << "yes\n";
		else			cout << "no\n";
	}

	return 0;
}

C++: #include<stdio.h>
#include<queue>
 
#define MAX 100000
 
int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;
 
bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;
 
    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }
 
    return false;
}
 
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }
 
    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        //bfs(s, d) ? printf("yes\n") : printf("no\n");
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <string>
#include <climits>
using namespace std;

#define MAX_N  100000
#define MAX_M  100000
#define MAX_Q  10000

int n, m, q;
queue<int> Q;
int d[MAX_N] = {};

int bf(vector<vector<int> >& sns, int src, int dst)
{
    Q.push(src);
    while (!Q.empty()) {
        if (dst == (src = Q.front())) return 1;
        Q.pop();
        d[src]++;
        for (auto itr = sns.at(src).begin(); itr != sns.at(src).end(); itr++) {
            if (!d[*itr]) Q.push(*itr);
        }
    }
    return 0;
}

int main()
{
    cin >> n >> m;
    vector<vector<int> > sns(n);
    int s, d;
    while (m--) {
        cin >> s >> d;
        sns.at(s).push_back(d);
        sns.at(d).push_back(s);
    }
    cin >> q;
    while (q--) {
        cin >> s >> d;
        cout << (bf(sns, s, d) ? "yes" : "no") << endl;
    }
    return 0;
}