Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int 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 <vector>
using namespace std;

const int MAX = 100000;
vector<int> friends[MAX + 1];
int groups[MAX + 1];

void dfs(int id, int gid) {
  if (groups[id] != -1) return;
  groups[id] = gid;

  int l = friends[id].size();
  for (int i = 0; i < l; i++) {
    dfs(friends[id][i], gid);
  }
}

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

  int n, m;
  cin >> n >> m;

  int a, b, q;
  int gid = 0;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    friends[a].push_back(b);
    friends[b].push_back(a);
  }

  for (int i = 0; i < n; i++) groups[i] = -1;

  for (int i = 0; i < n; i++) {
    if (groups[i] == -1) dfs(i, gid++);
  }

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

  for (int i = 0; i < n; i++) {
    cout << i << ":" << groups[i] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<queue>

const static int MAX = 100000;
int color[MAX];
int group_color;
std::vector<int> A[MAX];

int search(int o){
    if(color[o] != 0){
	return 0;
    }
    std::queue<int> qu;
    qu.push(o);
    color[o] = group_color;
    while(!qu.empty()){
	int cur = qu.front();
	qu.pop();
	for(int i = 0; i < A[cur].size(); i++){
	    if(color[A[cur][i]] == 0){
		qu.push(A[cur][i]);
	    	color[A[cur][i]] = group_color;
	    }
	}
    }
    return 0;
}

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

    std::cin >> n >> m;
    for(int i = 0; i < m; i++){
	std::cin >> s >> t;
	A[s].push_back(t);
	A[t].push_back(s);
    }
    for(int i = 0; i < n; i++){
        color[i] = 0;
    }
    group_color = 1;
    for(int i = 0; i < n; i++){
	search(i);
	group_color++;
    }

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

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    int u;
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty()){
        cout << que.front().u <<" \n";
        que.pop();
    }
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;
    for(int i=0; i<n; i++)
        node[i].u=i;

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

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(x==t){
                answer[i]=true;
                break;
            }

            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);
            }
         }

          que.pop();
        }

        que=refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}

C++: #include<iostream>
using namespace std;
#include<vector>
#include<stack>
#define MAX 100000
int n,m;
vector<int>G[MAX];
stack<int>mp;
int color[MAX];

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

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

int main()
{
	cin >> n >> m;
	int x,y; 
	for (int i = 0; i < m ;i++){
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	assignColor();
	cin >> m;
	for (int i = 0 ;i < m;i++){
		cin >> x >> y;
		if (color[x] == color[y]){
			cout << "yes" << endl;
		}
		else{
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct node {
	int id;
	vector<int> rel;
};

node* nodes;

void dfs(int start, int cur, int* h){
	if(h[cur] == -1){
		h[cur] = start;
		for(int i = 0; i < nodes[cur].rel.size(); i++){
			dfs(start, nodes[cur].rel[i], h);
		}
	}
}

int main() {
	int n, m, q;
	scanf("%d %d", &n, &m);
	 
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	int *h = new int[n];
	for(int i = 0; i < n; i++){
		h[i] = -1;
	}
	for(int i = 0; i < n; i++){
		dfs(i, i, h);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		printf("%s\n", h[l]==h[r] ? "yes" : "no");
	}
	return 0;
}