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>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

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

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent;
    vector<int> child;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n)
        person[i].parent = -1;
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1 && person[t].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
            person[s].child.push_back(t);
        } else if(person[s].parent == -1){
            person[s].parent = person[t].parent;
            person[t].child.push_back(s);
        } else if(person[t].parent == -1){
            person[t].parent = person[s].parent;
            person[s].child.push_back(t);
        } else {
            person[t].parent = person[s].parent;
            REP(j, person[t].child.size()){
                person[person[t].child[j]].parent = person[s].parent;
                person[person[s].parent].child.push_back(person[t].child[j]);
            }
            person[t].child.clear();
        }
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n, m;
vector<int> color(MAX, NIL);
vector<vector<int>> M(MAX);

void dfs(int i, int id) {
  int size = M[i].size();
  for (int j = 0; j < size; j++) {
    if (color[M[i][j]] == NIL) {
      color[M[i][j]] = id;
      dfs(M[i][j], id);
    }
  }
}

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

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

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  assignColor();

  for (int i = 0; i < qn; i++) {
    if (color[q[i][0]] == color[q[i][1]]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i,n) for(UL i=0; i<(n); i++)

vector<UL> E[100000];
UL D[100000];

int main() {
 UL N,M; scanf("%u%u",&N,&M);
 rep(i,M){
  UL u,v; scanf("%u%u",&u,&v);
  E[u].push_back(v);
  E[v].push_back(u);
 }
 rep(i,N) D[i]=~0u;
 rep(s,N){
  if(D[s]!=~0u) continue;
  queue<UL> Q; Q.push(s); D[s]=s;
  while(Q.size()){
   UL p=Q.front(); Q.pop();
   for(UL e:E[p]){
    if(D[e]!=~0u) continue;
    D[e]=D[p];
    Q.push(e);
   }
  }
 }
 UL Q; scanf("%u",&Q);
 rep(q,Q){
  UL s,t; scanf("%u%u",&s,&t);
  if(D[s]==D[t]) printf("yes\n");
  else printf("no\n");
 }
 return 0;
}

C++: 
#include<stdio.h>
#define Num 100000
 
int a[Num];
int search(int c){
   if(c == a[c])
      return c;
   return a[c] = search(a[c]);
 }
int main(){
   int i,x,y,n,m,z;
   for(i=0;i<Num;i++){
      a[i] = i;
   }
   scanf("%d %d",&x,&y);
   for(i=0;i<y;i++){
      scanf("%d %d", &n,&m);
      if(search(n) != search(m)){
         a[search(m)] = search(n);
      }
   }
   scanf("%d",&z);
   for(i=0;i<z;i++){
      scanf("%d %d",&n,&m);
      if(search(n) == search(m))
         printf("yes\n");
      else
         printf("no\n");
   }
 
   return 0;
}

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

int group[N],nt[N];
vector<int> vec[N];
int n,m;

int dfs_visit(int x,int k){
  stack<int> S;
  S.push(x);
  while(!S.empty()){
    int u=S.top();S.pop();
    for(int i=0;i<vec[u].size();i++){
      int v=vec[u][i];
      if(group[v]==0){
        group[v]=k;
        S.push(v);
      }
    }
  }
}
void dfs(){
  int id=1;
  for(int i=0;i<n;i++){
    if(group[i]==0) dfs_visit(i,id++);
  }
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int s,t;
    cin>>s>>t;
    vec[s].push_back(t);
    vec[t].push_back(s);
  }
  dfs();
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int a,b;
    cin>>a>>b;
    if(group[a]==group[b]){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}

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

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

int main(){
  int m,s,t,q;

  cin>>n>>m;

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

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

  cin>>q;

  for(int u=0;u<n;u++){
    int id=1;
    if(color[u]==NIL){
      dfs(u,id++);
    }
  }

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

  return 0;
}