Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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

using namespace std;

struct uft {
    uft (int s) : ps {new int[s]}, ds {new int[s]}, s {s} {
        for(int i = 0; i < s; ++i){
            ps[i] = i;
            ds[i] = 0;
        }
    };
    void unite(int x, int y){
        int px = parent(x);
        int py = parent(y);
        if(ds[px] < ds[py]){
            ps[px] = py;
        } else if(ds[px] > ds[py]){
            ps[py] = px;
        } else {
            ps[px] = py;
            ++ds[py];
        }
    };
    int parent(int p){
        return p == ps[p] ? p : ps[p] = parent(ps[p]);
    };
    bool same(int x, int y){
        return parent(x) == parent(y);
    };
    int* ps;
    int* ds;
    int s;
};

struct user {
    user() {};
    user(int i) : id {i}, fs {}, group {-1} {};
    int id;
    vector<int> fs;
    int group;
};

void add_friends(user *us, int x, int y){
    us[x].fs.push_back(y);
    us[y].fs.push_back(x);
}

void dfs(user* us, int id, int g){
    stack<int> ss {};
    ss.push(id);
    while(ss.size()){
        int p = ss.top();
        ss.pop();
        us[p].group = g;
        vector<int> fs = us[p].fs;
        for(auto x : fs){
            if(us[x].group == g){
                continue;
            }
            ss.push(x);
        }
    }
}

void make_group(user* us, int n){
    int g = 0;
    for(int i = 0; i < n; ++i){
        if(us[i].group < 0){
            dfs(us, i, g);
            ++g;
        }
    }
}

int main(){
    cin.sync_with_stdio(false);
    cout.sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    //user us[n];
    auto us = uft(n);
    /*for(int i = 0; i < n; ++i){
        us[i] = user(i);
    }*/
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        us.unite(a, b);
    //    add_friends(us, a, b);
    }
    //make_group(us, n);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++ i){
        int a, b;
        cin >> a >> b;
        //if(us[a].group == us[b].group){
        if(us.same(a, b)){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;

const int NIL = -1;
const int MAX = 20001;

vector<int> G[100001];
int color[100001];
int tt = 0;

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

int main(void)
{
	int n, m, s, t, q;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	for (int i = 0; i < n; i++) {
		if (color[i] == 0) {
			tt++;
			dfs(i, n);
		}
	}
	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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const int MAX = 100005;

vector<int> to[MAX];
bool seen[MAX];
int n, color[MAX], c = 0;

void dfs(int u, int c) {
  if (seen[u]) return;
  seen[u] = true;
  color[u] = c;
  for (int v: to[u]) {
    dfs(v, c);
  }
}

int main() {
  int n, m, s, t, q;
  cin >> n >> m;
  rep(i, m) {
    cin >> s >> t;
    to[s].push_back(t);
    to[t].push_back(s);
  }

  rep(i, n) {
    seen[i] = false;
    color[i] = -1;
  }

  rep(i, n) {
    dfs(i, c);
    c++;
  }

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

  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>

struct node {
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<std::vector<int> > nn(n.size());
	std::vector<int> dd(n.size());
	//std::vector<int> d(n.size());
	int count = 0;
	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		nn[count].push_back(i);
		std::vector<int> d;
		//std::fill(d.begin(), d.end(), 0);
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d.push_back(num);
			//d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				/*
				if (d[*it] != 1) {
					nn[count].push_back(*it);
					s.push(*it);
					d[*it] = 1;
				}
				*/
				if (std::find(d.begin(), d.end(), *it) == d.end()) {
					nn[count].push_back(*it);
					s.push(*it);
					d[*it] = 1;
				}
			}
		}
		count++;
	}

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		for (int j = 0; j < count; j++) {
			if (std::find(nn[j].begin(), nn[j].end(), from) != nn[j].end() &&
				std::find(nn[j].begin(), nn[j].end(), to) != nn[j].end()) {
				std::cout << "yes" << std::endl;
				break;
			}
			if (j == count - 1) {
				std::cout << "no" << std::endl;
			}
		}
	}
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);

	return 0;
}