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>
#include<queue>
#include<algorithm>
#include<ctime>

void dfs(std::vector<std::vector<int> > &n) {
	/*
	std::stack<int> s;
	std::vector<std::vector<int> > nn(n.size());
	std::vector<int> dd(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;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d.push_back(num);
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				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;
		std::stack<int> s;
		std::vector<int> went;
		s.push(from);
		bool flag = false;
		while (s.size() != 0 && flag == false){
			int num = s.top(); s.pop();
			for (size_t j = 0; j < n[num].size(); j++) {
				if (n[num][j] == to) {
					flag = true;
					break;
				}
				if (std::find(went.begin(), went.end(), n[num][j]) == went.end()) {
					s.push(n[num][j]);
					went.push_back(n[num][j]);
				}
			}
		}
		if (flag) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}
	/*
	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<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);
	}

	dfs(n);

	return 0;
}

C++: /* 
 * ALDS1_11B.cpp
 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

using Friends = vector<int>;
using SNS = vector<Friends>;

int n, m, q;
bool visited[100000];
SNS sns;

bool is_friend(int id, int target) {
    if (visited[id])
        return false;

    visited[id] = true;
    for (int i = 0; i < sns[id].size(); ++i) {
        int now = sns[id][i];
        if (now == target)
            return true;
    }

    for (int i = 0; i < sns[id].size(); ++i) {
        int now = sns[id][i];
        if (is_friend(now, target))
            return true;
    }
    return false;
}

int main(void) {

    scanf("%d %d", &n, &m);
    sns.resize(n);
    for (int i = 0, id, f; i < m; ++i) {
        scanf("%d %d", &id, &f);
        sns[id].push_back(f);
        sns[f].push_back(id);
    }

    scanf("%d", &q);
    for (int i = 0, id, f; i < q; ++i) {
        scanf("%d %d", &id, &f);
        memset(visited, 0, sizeof(bool) * n);
        if (is_friend(id, f))
            printf("yes\n");
        else
            printf("no\n");
    }

    return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

void dfs(int s, int group, vector<vector<int>> v, vector<int> &used) {
	used[s] = group;
	int n = v.size();
	int k = v[s].size();
	REP(l, k) {
		int j = v[s][l];
		if (!used[j]) {
			dfs(j, group, v, used);
		}
	}
}

void divide(vector<vector<int>> v, vector<int> &used) {
	int s = 0;
	int group = 1;
	int n = v.size();
	while (s < n) {
		dfs(s, group++, v, used);
		while (s < n && used[s]) s++;
	}
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> v(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	vector<int> used(n, 0);
	divide(v, used);
	int q;
	cin >> q;
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		if (used[s] == used[t]) cout << "yes\n";
		else cout << "no\n";
	}
	

	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define max 100000
vector<int> vn[max];
int grp[max];
int n;

void grouping(int g,int cnt){
  if(grp[g]!=0)return;
  grp[g]=cnt;
  for(int i=0;i<(int)vn[g].size();i++)
    grouping(vn[g][i],cnt);
}

int main(){
  int m,t,u,cnt;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t>>u;
    vn[t].push_back(u);
    vn[u].push_back(t);
  }
  cnt=1;
  for(int i=0;i<n;i++){
    if(grp[i]!=0)continue;
    grouping(i,cnt);
    cnt++;
  }
  //for(int i=0;i<n;i++)cout<<"i: "<<i<<"grp "<<grp[i]<<endl;
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>t>>u;
    if(grp[t]==grp[u]&&grp[t]!=0)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#define N_MAX 100000
using namespace std;

vector<int> sns[N_MAX];
int* group;
int g = 0;

void dfs(int s) {
  if (group[s] != 0) return;
  group[s] = g;
  
  int len = sns[s].size();
  for (int i = 0; i < len; i++) {
    dfs(sns[s][i]);
  }
}

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

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

    sns[s].push_back(t);
    sns[t].push_back(s);
  }

  // dfs
  group = new int[n];
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      g++;
      dfs(i);
    }
  }

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