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<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;

string dfs(int s, int t, vector<vector<int>> v, vector<int> &used) {
	if (s == t) return "yes\n";
	used[s] = 1;
	int n = v.size();
	int k = v[s].size();
	REP(l, k) {
		int j = v[s][l];
		if (!used[j]) {
			return dfs(j, t, v, used);
		}
	}
	return "no\n";
}

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);
	}
	int q;
	cin >> q;
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		vector<int> used(n, 0);
		cout << dfs(s, t, v, used);
	}
	

	return 0;
}

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

bool is_friend(const vector<vector<int> >& u, int s, int t, vector<int>& searched){
    if (s == t) return true;
    for (int i = 0; i < u[s].size(); i++) {
        if (!searched[u[s][i]]) {
            searched[u[s][i]] = 1;
            if (is_friend(u, u[s][i], t, searched)) return true;
        }
    }
    return false;
}
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;
        vector<int> searched(n, 0);
        searched[s] = 1;
        if (is_friend(u, s, t, searched)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
static const int MAX=100000;
static const int NIL=-1;

int n;
std::vector<int> G[MAX];
int color[MAX];
void DFS(int,int);
void assignColor(void);

int main(){
  int s, t, m, q;
  std::cin >> n >> m;
  for(int i=0; i<m; i++){
    std::cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assignColor();
  std::cin >> q;
  for(int i=0; i<q; i++){
    std::cin >> s >> t;
    if(color[s] == color[t]) std::cout << "yes\n";
    else std::cout << "no\n";
  }
  return 0;
}
void assignColor(void){
  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++);
}
void DFS(int r, int c){
  std::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);
      }
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define NIL -1

int c = 0,n,m;
int color[100000];
vector<int> li[100000];

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 < li[u].size();i++) {
      if(color[li[u][i]] == NIL) {
        S.push(li[u].at(i));
        color[li[u][i]] = c;
      }
    }
  }
}

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



int main() {

  
  cin >> n >> m;


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

  for(int i = 0;i < n;i++) {
    color[i] = NIL;
  }

  int q;
  cin >> q;

  assignColor();

  for(int i = 0;i < q;i++) {
    int a,b;
    cin >> a >> b;
    cout << ((color[a] == color[b]) ? "yes" : "no") << endl;
  }


}

C++: #include <stdio.h>
#define N 100000

int a[N]={};

int search(int c)
{
	if(c==a[c]) return c;
	
	return a[c]=search(a[c]);
}

int main(void)
{
 int i,n,m,q,s,t;

 for(i=0;i<N;i++) a[i]=i;

 scanf("%d %d",&n,&m);

 for(i=0;i<m;i++){

  scanf("%d %d",&s,&t);

  if(search(s)==search(t)) a[t]=s;

 }

 scanf("%d",&q);

 for(i=0;i<q;i++){
 
  scanf("%d %d",&s,&t);

  if(search(s)==search(t)) printf("yes\n");
  else printf("no\n");

 }

 return 0;
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){
  bool used[m];
  for(int i=0;i<m;i++) used[i]=false;
  queue<int> Q;
  Q.push(s); used[s]=true;
  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    if(k==t){return true;}
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(!used[temp]){
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  int q; cin>>q;
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s, t)){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}