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 <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
vector<int> g[100000];
vector<int> c(100000);

void dfs(int v, int col) {
    c[v] = col;
    for (int i = 0; i < g[v].size(); ++i) {
        if(c[g[v][i]] == 0) dfs(g[v][i], col);
    }
}

int main() {
    int n, m, q;
    cin >> n>> m;
    for (int i = 0; i < m; ++i) {
        int v, w;
        cin >> v >> w;
        g[w].push_back(v);
        g[v].push_back(w);
    }

    for (int k = 0; k < n; ++k) {
        c[k] = 0;
    }

    int col = 1;
    for (int j = 0; j < n; ++j) {
        if(!c[j]) {
            dfs(j, col);
            col++;
        }
    }

    cin >> q;
    for (int l = 0; l < q; ++l) {
        int v,w;
        cin >> v >> w;
        if(c[v] == c[w]) cout << "yes";
        else cout << "no";
        cout << endl;

    }


    return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int SIZE = 100001;
int group[SIZE];

void assignGroup(int u, int g, std::vector<int> *a) {
  if (group[u] != 0) return;

  group[u] = g;
  // for (int i = 0; i < a[u].size(); ++i) {
  //   if (a[u][i] && group[i] == 0) assignGroup(i, g, a);
  // }
  for (auto v : a[u]) {
    // printf("u: %d, v: %d, group[u]: %d, group[v]: %d\n", u, v, group[u],
    //        group[v]);
    if (group[v] == 0) assignGroup(v, g, a);
  }
}

bool isConnected(int s, int t) { return group[s] == group[t]; }

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> a[n];

  rep(i, m) {
    int s, t;
    std::cin >> s >> t;
    a[s].push_back(t);
    a[t].push_back(s);
  }

  // std::cout << "/* message */" << '\n';
  // rep(i, n) {
  //   for (auto v : a[i]) std::cout << v << ' ';
  //   std::cout << '\n';
  // }
  // std::cout << "/* message */" << '\n';

  int g = 0;
  rep(i, n) {
    if (group[i] == 0) { assignGroup(i, ++g, a); }
  }

  // rep(i, n) std::cout << group[i] << ' ';

  int q;
  std::cin >> q;
  rep(i, q) {
    int s, t;
    std::cin >> s >> t;
    if (isConnected(s, t))
      std::cout << "yes" << '\n';
    else
      std::cout << "no" << '\n';
  }

  return 0;
}

C++: //
// Created by ?????°?????? on 2016/05/15.
//

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

const int MAX = (1 << 21);
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>
#include <stack>

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n; //number of users
vector<int> G[MAX];
int color[MAX];

void dfs(int userid, int groupid) {
    stack<int> S;
    S.push(userid);
    color[userid] = groupid;
    while ( !S.empty() ) {
        int user = S.top(); S.pop();
        for (unsigned long i = 0; i < G[user].size(); i++) {
            int partner = G[user][i];
            //if partner is not member of a group -> join to user's group
            if (color[partner] == NIL) {
                color[partner] = groupid;
                //add partner to stack, then check partner's partner list (use while)
                S.push(partner);
            }
        }
    }
}

//create a group list named as color
//result is color = [1, 1, 1, 2, 2, 3, 3, 3, 3, 3]
void assingColor() {
    int id = 1;
    for (int i = 0; i < n; i++) color[i] = NIL; // create empty group list
    for (int u = 0; u < n; u++) {
        //u = user id, id = group id
        if (color[u] == NIL) dfs(u, id++); //fill group list with dfs
    }
    //for (int i = 0; i < n; i++) cout << color[i] << " ";
    //cout << endl;
}

int main()
{
    int m; //number of relationships
    int s, t; //user and partner id
    int q; //number of question
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t); //push user's partner id to user relationship list
        G[t].push_back(s); //push user id to user's partner relationship list
    }
    assingColor();

    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> s >> t; // user and partner data
        if (color[s] == color[t]) { cout << "yes" << endl; }
        else { cout << "no" << endl; }
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

vector<vector<int> > g;
vector<int> ids;

void rec(int v, int id)
{
  //fprintf(stderr, "DEBUG: v=%d, id=%d\n", v, id);
  int n = g[v].size();
  ids[v] = id;
  for (int i = 0; i < n; ++i) {
    if (ids[g[v][i]] == -1) {
      rec(g[v][i], id);
    }
  }
}

int main()
{
  int n, m;
  fscanf(stdin, "%d %d", &n, &m);
  g = vector<vector<int> >(n);
  ids = vector<int>(n, -1);
  for (int i = 0; i < m; ++i) {
    int s, t;
    fscanf(stdin, "%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  int id = 0;
  for (int i = 0; i < n; ++i) {
    if (ids[i] == -1) {
      rec(i, id);
      ++id;
    }
  }
  int q;
  fscanf(stdin, "%d", &q);
  // fprintf(stderr, "DEBUG: ");
  // copy(ids.begin(), ids.end(), ostream_iterator<int>(cerr, ", "));
  // fprintf(stderr, "\n");
  for (int i = 0; i < q; ++i) {
    int s, t;
    fscanf(stdin, "%d %d", &s, &t);
    fprintf(stdout, "%s\n", ids[s] == ids[t] ? "yes" : "no");
  }
  return 0;
}