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 <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

class UnionFind {
    vector<int> parent;

    public:
        UnionFind(int size) {
            parent = vector<int>(size, -1);
        }

        int find(int x) {
            if (parent[x] < 0) {
                // parent itself
                return x;
            } else {
                return parent[x] = find(parent[x]);
            }
        }

        bool unite(int x, int y) {
            int rX, rY;
            if ((rX = find(x)) == (rY = find(y))) {
                // already united
                return false;
            }
            if (size(rX) < size(rY)) {
                int tmp = rX;
                rX = rY;
                rY = tmp;
            }
            parent[rX] += parent[rY];
            parent[rY] = rX;
            return true;
        }

        int size(int x) {
            return -parent[find(x)];
        }

        bool is_same(int x, int y) {
            return find(x) == find(y);
        }

        // CAUTION!! : This method is heavy due to scanning linearly.
        int component_count() {
            int c = 0;
            for (const int x : parent) {
                if (x < 0) {
                    c++;
                }
            }
            return c;
        }
};

void solve() {
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    int a, b;
    REP(i, m) {
        cin >> a >> b;
        uf.unite(a, b);
    }
    int q;
    cin >> q;
    REP(i, q) {
        cin >> a >> b;
        cout << (uf.is_same(a, b) ? "yes" : "no") << endl;
    }
}

C++: #include <cstdio>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int NIL = -1;
const int MAX = 100000;
vector<int> SNS[MAX];
int color[MAX];

void dfs(int v, int colorIdx) {

	stack<int> S;
	S.push(v);
	color[v] = colorIdx;

	while (!S.empty()) {
		int s = S.top();
		S.pop();
		for (int i = 0; i < (int) SNS[s].size(); ++i) {
			int r = SNS[s][i];
			if (color[r] == NIL) {
				S.push(r);
				color[r] = colorIdx;
			}
		}
	}
}

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

int main() {

	int n, m;
	scanf("%d %d", &n, &m);

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

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

	makeColor(color, n);

	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; ++i) {
		int s, t;
		scanf("%d %d", &s, &t);
		if (color[s] == color[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 1e6;
constexpr int NIL = -1;
int n;
vector<vi> G(MAX);
vi color(MAX, NIL), S;
inline void dfs(int r, int c) {
	S.eb(r);
	color[r] = c;
	int u, v;
	while(!S.empty()) {
		u = S.back(); S.pop_back();
		rep(i, 0, len(G[u])) {
			v = G[u][i];
			if(color[v] == NIL) {
				color[v] = c;
				S.eb(v);
			}
		}
	}
}
inline void assignColor() {
	int id = 0;
	rep(u, 0, n) {
		if(color[u] == NIL) dfs(u, ++id);
	}
}
int main() {
	int m;
	cin >> n >> m;
	int s, t;
	rep(i, 0, m) {
		cin >> s >> t;
		if(s > t) swap(s, t);
		G[s].eb(t);
	}
	assignColor();
	int q; cin >> q;
	rep(i, 0, q) {
		cin >> s >> t;
		P(color[s] == color[t] ? "yes" : "no");
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;++i)
typedef long long int ll;
ll INF = 1LL<<60;

vector<vector<int> > G(100010);
vector<int> color(100010,-1);
int n,m; 

// sからたどり着く頂点の色をcに塗り替える
void dfs(int v, int c){
    stack<int> s;
    s.push(v);
    color[v] = c;
    while(!s.empty()){
        int u = s.top(); s.pop();
        for(auto nv : G[u]){
            if(color[nv] == -1){
                color[nv] = c;
                s.push(nv);
            }
        }
    }
}

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

int main(){
        cin >> n >> m;
    rep(i,m){
        int x,y; cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    assignColor();
    int q; cin >> q;
    rep(i,q){
        int x,y; cin >> x >> y;
        if(color[x] == color[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}