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