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>
using namespace std;
#include<stdio.h>

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[10000][10000];
    int dist[10000];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

    return 0;
}

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

void color(std::vector<std::vector<int> > n) {
	std::vector<int> color(n.size(), 0);
	std::stack<int> s;

	int count = 1;
	for (size_t i = 0; i < n.size(); i++) {
		if (color[i] != 0) continue;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				if (color[*it] == 0) {
					s.push(*it);
					color[*it] = count;
				}
			}
		}
		count++;
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (color[from] == color[to]) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

}

int main() {
	int people, relation;

	std::cin >> people >> relation;

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

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

	color(n);

	return 0;
}

C++: #include<iostream>
#include<vector>
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 col) {
	color[r] = col;
	for (int i = 0; i < G[r].size(); i++) {
		int v = G[r][i];
		if (color[v] == NIL) {
			dfs(v, col);
		}
	}
}

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

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

	assignColor();

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

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

#define N 100000

using namespace std;

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

void dfs(int s,int c){
  color[s]=c;
  for(int i=0;i<G[s].size();i++){
    if(color[G[s][i]]!=c) dfs(G[s][i],c);
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<m;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int q; scanf("%d",&q);
  for(int i=0;i<q;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    dfs(s,i);
    if(color[s]==color[t]) cout << "yes\n";
    else cout << "no\n";
  }
}

C++: #include <iostream>
#include <cstdio>
using namespace std;

const int maxm = 100000 + 5;
const int maxn = 100000 + 5;

struct Edge{
  int to,next;
} G[maxm<<1];

int head[maxn],cnt = 0;
int color[maxn];
bool vis[maxn];

void Insert(int u,int v){
  G[++cnt].next = head[u];
  G[cnt].to = v;
  head[u] = cnt;
}

void Dfs(int u,int c){
  vis[u] = true;
  color[u] = c;
  for (int e = head[u];e; e = G[e].next) {
    int v = G[e].to;
    if (!vis[v]) {
      Dfs(v,c);
    }
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for (int i=1;i<=m; i++) {
    int u,v;
    scanf("%d %d",&u,&v);
    Insert(u,v);
    Insert(v,u);
  }
  for (int i=1; i<=n; i++) {
    if (!vis[i]) {
      Dfs(i,i);
    }
  }
  int q;
  scanf("%d",&q);
  for(int i=1; i<=q; i++){
    int u,v;
    scanf("%d%d",&u,&v);
    if (color[u]==color[v]) {
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>

int *P;

int root(int a)
{
    if(P[a] == a) return a;
    P[a] = root(P[a]);
    return P[a]; 
}

void join(int a, int b){ int x = root(a), y = root(b); x = P[y]; }

bool same(int a, int b){ return (root(a) == root(b) ? true : false); }

int main()
{
    int i, n, m, a, b, q;
    scanf("%d %d", &n, &m);
    P = new int [n];
    for(i = 0; i < n; i++) P[i] = i;

    for(i = 0; i < m; i++){ scanf("%d %d", &a, &b); join(a, b); }

    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(same(a, b)){ printf("yes\n"); }else{ printf("no\n"); }
    }
    delete [] P;
    return 0; 
}