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>
#define N 100000
#define white 0
using namespace std;

int color[N], n, S[N];
vector<int> c[N];



void tansaku_2(int a, int b) {
    int i, t, co, u;
    S[0] = a;
    t = 0;
    co = 1;
    while (1) {
        u = S[t];
        for (i = 0; i < c[u].size(); ++i) {
            if (color[c[u][i]] == white){
                color[c[u][i]] = b;
                S[co++] = c[u][i];
            }
        }
        S[t++] = 0;
        if (t == co) break;
    }
}

void tansaku() {
    int i, d;
    d = 1;
    
    for (i = 0; i < n; ++i) {
        if (color[i] == white) {
            color[i] = d;
            tansaku_2(i , d++);
        }
    }
}

int main() {
    int m, q, i, a, b;
    
    cin >> n >> m;
    for (i = 0; i < n; ++i) {
        color[i] = white;
    }
    for (i = 0; i < m; ++i) {
        cin >> a >> b;
        c[a].push_back(b);
        c[b].push_back(a);
    }
    tansaku();
    
    cin >> q;
    
    for (i = 0; i < q; ++i) {
        cin >> a >> b;
        if (color[a] == color[b]) {
            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.push_back(*it);
				}
			}
		}
		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;
}

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


using namespace std;

int *passed;

bool do_check(int s, int t, vector< vector<int> > a){
  queue<int> q;
  
  bool flag = false;
  if(s==t){flag=true;}
  q.push(s);
  while(q.size()>0 && flag==false){
    s = q.front();
    q.pop();
    for(int i=0;i<a[s].size();i++){
      int tmp = a[s].at(i);
      if(passed[tmp] == 0){
	passed[tmp] = 1;
	q.push(tmp);
	if(tmp == t){
	  flag = true;
	  break;
	}
      }
    }
  }
  return flag;
}

int main(){
  int n, m;
  cin >> n >> m;
  passed = new int[n];
  vector< vector<int> > a;

  for(int i=0;i<n;i++){
    vector<int> tmp;
    a.push_back(tmp);
  }

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

  // Questions
  int q_num;
  cin >> q_num;
  // cout << "q_num: " << q_num << endl;
  for(int i=0;i<q_num;i++){
    for(int j=0;j<n;j++){
      passed[j]=0;
    }
    int s, t;
    cin >> s >> t;

    // cout << s << ": " << do_check(s, t, a) << endl;
    if(do_check(s, t, a)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  
}

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]==INL){
	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();

  sin>>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 <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#include <list>
#define N 100000
using namespace std;
int n;
list<int> adj[N];

bool PathExists(int u, int v, bool f[])
{
    f[u] = true;
    if (u == v)
        return true;
    list<int>::iterator itr = adj[u].begin();
    while (itr != adj[u].end())
    {
        if (!f[*itr] && PathExists(*itr, v, f))
            return true;
        itr++;
    }
    return false;
}

int main()
{
    int m, u, v, q;
    cin >> n >> m;

    for (int i = 0; i < m; i++)
    {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    cin >> q;
    for (int i = 0; i < q; i++)
    {
        bool f[N] = {};
        cin >> u >> v;
        if (PathExists(u, v, f))
            printf("yes\n");
        else
            printf("no\n");
    }

    return 0;
}