Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if root(s, &group) == root(t, &group) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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

#define MAX 100000
#define NIL -1

void dfs(int start_point, int id, vector<int> *graph, int *color, int n){
    
    stack<int> S;
    S.push(start_point);
    color[start_point] = id;

    while(!S.empty()){
        int node = S.top(); S.pop();
        rep(i, graph[node].size()){
            int v = graph[node][i];
            if(color[v] == NIL){
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void assignColor(vector<int> *graph, int *color, int n){
    int id = 1;
    rep(i, n){
        if(color[i] == NIL){
            dfs(i, id++, graph, color, n);
        }
    }
}

int main() {

    int n, m;
    cin >> n >> m;
    vector<int> graph[MAX];
    int color[MAX];
    rep(i, n){
        color[i] = NIL;
    }

    rep(i, m){
        int s, t;
        cin >> s >> t;
        graph[s].push_back(t);
        graph[t].push_back(s);
    }

    assignColor(graph, color, n);

    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;
        }
    }

    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[s].child.push_back(person[t].child[j]);
            }
        }
    }
    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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <set>
#include <map>
using namespace std;

vector<vector<int> >g;
int n,m,q;

vector<bool>visited;
bool dfs(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    cin>>n>>m;
    g.resize(n);
    
    for(int i=0;i<m;i++){
        int s,t;
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    cin>>q;
    for(int i=0;i<q;i++){
        visited.resize(n,false);
        int s,t;
        cin>>s>>t;
        string ret =(dfs(s,t)?"yes":"no");
        if(ret=="no")return 1;
        else cout<<ret<<endl;
    }
    return 0;
}

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

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100'000;

Vi adj[N_MAX];
int color[N_MAX];

int n;
void build_color() {
    bool isqueued[N_MAX] = {false};
    for (int i = 0, c = 0; i < n; ++i) {
        if (isqueued[i]) { continue; }
        queue<int> q;
        q.push(i);
        isqueued[i] = true;
        while (!q.empty()) {
            const int j = q.front();
            color[j] = c;
            for (const int k : adj[j]) {
                if (isqueued[k]) { continue; }
                q.push(k);
                isqueued[k] = true;
            }
            q.pop();
        }
        ++c;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int m;
    cin >> n >> m;

    int a, b;
    FOR(i,0,m) {
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    /*
    cout << "adj read." << endl;
    FOR(i,0,n) {
        for (const int j : adj[i]) {
            cout << " " << j;
        }
        cout << endl;
    }
    */
    build_color();

    int q;
    cin >> q;
    FOR(i,0,q) {
        cin >> a >> b;
        cout << (color[a] == color[b] ? "yes" : "no") << endl;
    }

    return 0;
}

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

using namespace std;

const int WHITE = 0, GREY = 1, BLACK = 2;

struct CC {
    int n;
    vector<vector<int>> g;
    vector<int> color;
    vector<int> component;
    int cId = 0; //component id
    
    CC (int n) : n(n) {
        g.assign(n, vector<int>(0));
        color.assign(n, WHITE);
        component.assign(n, -1);
    }
    
    void addEdge (int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    
    void solve () {
        for (int i = 0; i < n; i++) {
            if (color[i] == WHITE) {
                cId++;
                dfs(i);
            }
        }
    }
    
    void dfs (int u) {
        color[u] = GREY;
        component[u] = cId;
        for (int v : g[u]) {
            if (color[v] == WHITE) {
                dfs(v);
            }
        }
        color[u] = BLACK;
    }
};

int main() {
    
    int n, m;
    cin >> n >> m;
    CC cc(n);
    
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        cc.addEdge(u, v);
    }
    
    cc.solve();
    
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        if (cc.component[u] == cc.component[v]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    
    return 0;
}