Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define MAX 100000
#define NIL -1

int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
    stack<int> S;
    S.push(r);
    color[r] = c;
    while(!S.empty()) {
        int u = S.top();
        S.pop();
        for(int i=0; i<G[u].size(); i++) {
            int v = G[u][i];
            if(color[v]==NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

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

int main() {
    int s, t, m, q;

    cin >> n >> m;

    for(int i=0; i<m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor();

    cin >> q;

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

    return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100000
#define NIL -1

/*

G':1-2-4 3-5
color:1 2

*/

vector<int> G[MAX];
int color[MAX];

void dfs(int u, int c) {
	stack<int> S;
	S.push(u);
	color[u] = c;
	while (!S.empty()) {
		int u = S.top(); S.pop();
		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			if (color[v] == NIL) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

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

	fill(color, color + MAX, NIL);
	int id = 1;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)dfs(u, id++);
	}

	int q; cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t; cin >> s >> t;
		//cout << color[s] << " " << color[t] << endl;
		if (color[s] == color[t]) { 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++)

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