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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N, M, Q;
vector< vector<int> > graph;
vector< int > group;

void dfs(int u, int num) {
    group[u] = num;
    for (int v : graph[u])
        if (group[v] == -1) dfs(v, num);
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N >> M;
    graph.resize(N+1); group.resize(N+1);
    for (int j = 0; j < M; ++j) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    fill(begin(group), end(group), -1);
    int group_num = 0;
    for (int j = 1; j <= N; ++j) {
        if (group[j] == -1) dfs(j, group_num++);
    }

    cin >> Q;
    for (int j = 0; j < Q; ++j) {
        int u, v; cin >> u >> v;
        cout << (group[u] == group[v] ? "yes" : "no") << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

class UF{
public:
  vector<int> u;
  void init(int n){
    u.resize(n, 0);
    for(int i=0; i<n; i++) u[i] = i;
  }
  int root(int x){
    if( u[x] == x ) return x;
    else return root(u[x]);
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if( x == y ) return;
    u[x] = y;
  }
};

int main(void) {

  int n, m;
  cin >> n >> m;
  UF uf;
  uf.init(n);
  int s, t;
  for(int i=0; i<m; i++){
    scanf("%d %d", &s, &t);
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  for(int i=0; i<q; i++){
    scanf("%d %d", &s, &t);
    cout << ( uf.same(s, t) ? "yes" : "no" ) << endl;
  }



  return 0;
}

// EOF

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

using namespace std;

vector<int> G[100000];
int color[100000];
int n;

void dfs(int idx, int c)
{
	stack<int> s;
	s.push(idx);
	color[idx] = 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] == -1)
			{
				color[v] = c;
				s.push(v);
			}
		}
	}
}

void AssignColor()
{
	int id = 1;
	for (int i = 0; i < n; ++i) color[i] = -1;
	for (int u = 0; u < n; ++u)
	{
		if (color[u] == -1) 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 <bits/stdc++.h>
using namespace std;
int n, m;
vector< vector<int> > v;
vector<bool> used;

bool dfs(int pos, int dest){
  if(used[pos]) return false;
  used[pos] = true;
  if(pos == dest) return true;
  for(int i = 0; i < v[pos].size(); i++){
    if(dfs(v[pos][i], dest)){
      return true;
    }
  }
}

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

  cin >> n >> m;
  v = vector< vector<int> >(n, vector<int>(0));
  used = vector<bool>(n, false);
  for(int i = 0; i < m; i++){
    int a, b; cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int q; cin >> q;
  for(int i = 0; i < q; i++){
    used = vector<bool>(n, false);
    int a, b; cin >> a >> b;
    if(dfs(a, b)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t; vector<int>a[100000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	while (!x[B] == 1 || !Q.empty()) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			if (x[a[Q.front()][k]] == 0) { Q.push(a[Q.front()][k]); x[a[Q.front()][k]] = 1; }
		}
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	cin >> n >> m >> q;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (bfs(s, t)) { cout << "yes" << endl; }
		else { cout << "no" << endl; }
	}
	return 0;
}

C++: #pragma GCC optimize("03")
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long 
using namespace std;
int f[200000];
int find(int x){
	if(f[x]==x)
		return x;
	else
		return f[x]=find(f[x]);
}
void merge(int c,int d){
	f[d]=c;
}
int main()
{
    ios::sync_with_stdio(0);cin .tie(NULL);
    ll a,b,num;
   	cin >> a >> b;
   	for(int i=0;i<a;i++)
	f[i]=i;
    for(int i=0;i<b;i++)
    {
    	ll c,d;
    	cin >> c >> d;
    	c=find(c);
    	d=find(d);
   		merge(c,d);
	}
	cin >> num;
	for(int i=0;i<num;i++)
	{
		ll e,g;
		cin >> e >> g;
		if(find(e)==find(g))
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<set>
using namespace std;

void set_color(const vector<vector<int> >& G, vector<int>& color,
                int color_name, int a, int i) {
    if (i >= G[a].size()) return;
    if (color[G[a][i]] == -1) {
        color[G[a][i]] = color_name;
        set_color(G, color, color_name, G[a][i], 0);
    }
    set_color(G, color, color_name, a, i + 1);
}


bool DFS(const vector<vector<int> >& G, int a, int b) {
    static vector<int> color(G.size(), -1);
    static int color_name = 0;
    if (color[a] != color[b]) return false;
    if (color[a] == -1) {
        set_color(G, color, color_name++, a, 0);
    }
    if (color[a] == color[b]) return true;
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<vector<int> > G(n);

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (vector<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
           cout << "yes" << endl;
        } else {
           cout << "no"  << endl;
        }
    }
    return 0;
}