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>
#include <stdint.h>

typedef uint32_t u32;

template<u32 N_>
class UnionFindTree {
private:
  u32 par[N_];
  u32 rank[N_];
public:
  UnionFindTree(u32 n) {
    assert(n < N_);
    for(u32 i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
    return;
  }
  int find(u32 x) {
    if( par[x] == x ) return x;
    return par[x] = find(par[x]);
  }
  void unite(u32 x, u32 y) {
    x = find(x);
    y = find(y);
    if( x == y ) return;
    if( rank[x] < rank[y] ) {
      par[x] = y;
      return;
    }
    par[y] = x;
    if( rank[x] == rank[y] ) rank[x] += 1;
    return;
  }  
};

int main() {

  u32 n, m;
  std::cin >> n >> m;

  UnionFindTree<200000> uft(m);

  for(u32 i = 0; i < m; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    uft.unite(x, y);
  }

  u32 q;
  std::cin >> q;

  for(u32 i = 0; i < q; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    std::cout << (uft.find(x) == uft.find(y) ? "yes" : "no") << std::endl;
  }
  
  return 0;
}

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

void set_color(const vector<vector<int> >& u, int p, vector<int>& color, int c) {
    if (color[p] != -1) return;
    color[p] = c;
    for (int i = 0; i < u[p].size(); i++) {
        set_color(u, u[p][i], color, c);
    }
}

bool is_friend(const vector<vector<int> >& u, int s, int t){
    vector<int> color(u.size(), -1);
    int c = 0;
    set_color(u, s, color, c++);
    return color[s] == color[t];
}
int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > u(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        u[s].push_back(t);
        u[t].push_back(s);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        if (is_friend(u, s, t)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    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 id, vector<vector<int>> a, vector<int> &comp) {
	stack<int> st;
	st.push(s);
	comp[s] = id;
	while (!st.empty()) {
		int i = st.top();
		int k = a[i].size();
		REP(j, k + 1) {
			if (j < k) {
				int t = a[i][j];
				if (comp[t] != id) {
					comp[t] = id;
					st.push(t);
					break;
				}
			}
			else st.pop();
		}
	}
}

void divide(int size, vector<vector<int>> a, vector<int> &comp) {
	int s = 0;
	int id = 1;
	while (1) {
		dfs(s, id, a, comp);
		while (s < size && comp[s]>0) s++;
		if (s == size) break;
		id++;
	}
	return;
}

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

	int n, m;
	scanf("%d %d", &n, &m);
	//cin >> n >> m;
	vector<vector<int>> a(n);
	REP(i, m) {
		int s, t;
		scanf("%d %d", &s, &t);
		//cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	int q;
	cin >> q;
	vector<int> comp(n, 0);
	divide(n, a, comp);
	REP(i, q) {
		int s, t;
		scanf("%d %d", &s, &t);
		//cin >> s >> t;
		if (comp[s] == comp[t]) printf("yes\n");
		else printf("no\n");
	}

	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

struct Info{
	Info(){s = t = -1;}
	Info(int tmp_s,int tmp_t){
		s = tmp_s;
		t = tmp_t;
	}
	int s;
	int t;
};

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		global = new Info[m];
		each = new int*[n];
		each_index = new int[n];
		num_of_each = new int[n];
		went = new int[n];
		for(int i=0;i<n;i++){
			each_index[i] = 0;
			num_of_each[i] = 0;
			went[i] = 0;
		}
		global_index = 0;
	}

	void push(int s,int t){
		global[global_index++] = Info(s,t);
		num_of_each[s]++;
		num_of_each[t]++;
	}

	void allocate(){
		for(int i=0;i<n;i++){
			if(num_of_each[i] > 0){
				each[i] = new int[num_of_each[i]];
			}
		}
		Info tmp;
		for(int i=0;i<m;i++){
			tmp = global[i];
			each[tmp.s][each_index[tmp.s]++] = tmp.t;
			each[tmp.t][each_index[tmp.t]++] = tmp.s;
		}
	}


	void recursive(int s,int t,bool& FLG){
		went[s] = 1;
		for(int i=0;i<num_of_each[s];i++){
			if(each[s][i] == t){
				FLG = true;
				return;
			}
			else if(went[each[s][i]] == 0){
				recursive(each[s][i],t,FLG);
			}
		}
	}

	bool canAccsess(int s,int t){
		for(int i=0;i<n;i++)went[i]=0;
		bool FLG =false;
		recursive(s,t,FLG);
		if(FLG){
			return true;
		}
		else{
			return false;
		}
	}

private:
	Info* global;
	int** each;
	int* each_index;
	int* num_of_each;
	int global_index,n,m;
	int* went;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.allocate();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}

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

int main(){
	int n,m,q,i,j;
	int s,t;
	vector<int> L(100005,-1);
	cin >> n >> m;
	
	for (j=0;j<n;j++){
		L[j]=j;
	}
	
	for (i=0;i<m;i++){
		cin >> s >> t ;
		if (L[s]!=L[t]){
			int a=L[t];
			for (j=0;j<n;j++){
				if (L[j]==a){
					L[j]=L[s];
				}
			}
		}
	}
	
	cin >> q;
	for (i=0;i<q;i++){
		cin >> s >> t;
		if (L[s]==L[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	
	for (i=0;i<n;i++){
		cout << L[i] << 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)
{
  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);
  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;
}