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 <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())

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;

class vertex{
public:
  vector<vertex*> to;
  bool visited;
  int num;
  int group;

  vertex(){
    visited=false;
  }
  
  void dfs(int group){
    visited=true;
    this->group = group;
    for(int i=0; i<to.size(); i++){
      if(!to[i]->visited) to[i]->dfs(group);
    }
  }
};

int main(){
  int n, m; cin >> n >> m;
  vertex graph[n];

  //隣接リストにグラフを格納
  int v, k, to;
  for(int i=0; i<m; i++){
    cin >> v >> to;
    graph[v].num = v;
    graph[v].to.push_back(&graph[to]);
  }

  //頂点0からdfsを行い、グループ分け(各連結成分ごとに1から昇順に番号をつける)
  for(int v=0, group=1; v<n; v++){
    if(!graph[v].visited){
      graph[v].dfs(group);
      group++;
    }
  }

  //回答を得る
  int q, v1, v2; cin >> q;
  bool ans[q];
  for(int i=0; i<q; i++){
    cin >> v1 >> v2;
    if(graph[v1].group==graph[v2].group) ans[i]=true;
    else ans[i]=false;
  }

  //回答出力
  for(int i=0; i<q; i++){
    if(ans[i]) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}

C++: #include <stdio.h>
#include <stack>
using namespace std;
int G[100000][100000];
int vis[100000];
int n;

bool dfs(int s, int t)
{
    if (G[s][t] == 1) return true;
    vis[s] = 1;
    for (int i = 0; i < n; i++)
    {
        if (G[s][i] == 1 && vis[i] == 0) return dfs(i,t); 
    } 
    return false;
}

int main()
{
    int m;
    scanf("%d %d",&n, &m);
    for (int i = 0; i < n; i++)
    {
        vis[i] = 0; 
    } 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            G[i][j] = 0; 
        }  
    } 
    for (int i = 0; i < m; i++)
    {
        int s, t;
        scanf("%d %d",&s,&t); 
        G[s][t] = 1;
    } 
    int q,s,t;
    scanf("%d",&q);
    for (int i = 0; i < q; i++)
    {
        scanf("%d %d",&s,&t);
        if(dfs(s,t)) printf("yes\n");
        else printf("no\n");
    } 
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#define MAX 100005
#define NIL -1 
using namespace std;

int color[MAX];
int n,m,s,t,q;
vector<int> G[MAX];

void dfs(int x, int y) {
	stack<int> S;
	S.push(x);
	color[x]=y;
	while(!S.empty()) {
		int tmp=S.top();
		S.pop();
		for(int i=0;i<G[tmp].size();i++) {
			int l=G[tmp][i];
			if(color[l]==NIL) {
				color[l]=y;
				S.push(l);
			}
		}
	}
}

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

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

C++: #ifndef ___Class_Union_Find
#define ___Class_Union_Find
 
#include <vector>
#include <utility>
#include <algorithm>
 
using namespace std;
 
class Union_Find
{
private:
 
    vector<int> data;
 
public:
 
    Union_Find(unsigned size_) : data(size_, -1) { }
 
    void unite(int x, int y);
     
    bool same(int x, int y);
 
    int root(int x);
};
 
void Union_Find::unite(int x, int y)
{
    x = root(x);
    y = root(y);
 
    if (x != y)
    {
        if (data[x] < data[y])
        {
            swap(x, y);
        }
 
        data[x] += data[y];
        data[y] = x;
    }
}
 
bool Union_Find::same(int x, int y)
{
    return root(x) == root(y);
}
 
int Union_Find::root(int x)
{
    return data[x] < 0 ? x : data[x] = root(data[x]);
}
 
#endif // ___Class_Union_Find
 
 
// ------ main ------ //
 
#include <cstdio>
 
using namespace std;
 
int n, m, q, x, y;
 
int main()
{
    scanf("%d", &n);
    scanf("%d", &m);
 
    Union_Find u(n);
 
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y);
 
        u.unite(x, y);
    }
    
    scanf("%d", &q);
    
    for(int i = 0; i < q; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y);
        
        printf("%s\n", u.same(x, y) ? "yes" : "no");
    }
 
    return 0;
}