Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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

bool dfs(std::vector<std::vector<int> > n, int from, int to) {
	std::stack<int> s;
	std::vector<int> went(n.size(), 0);

	s.push(from);
	while (s.size() != 0){
		int num = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
			if (*it == to) return true;
			if (!went[*it]) {
				s.push(*it);
				went[*it] = 1;
			}
		}
	}
	return false;
}

void color(std::vector<std::vector<int> > n) {
	std::vector<int> color(n.size(), 0);
	std::vector<int> went(n.size(), 0);
	std::stack<int> s;

	int count = 1;
	for (size_t i = 0; i < n.size(); i++) {
		if (color[i] != 0) continue;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				if (!went[*it]) {
					s.push(*it);
					went[*it] = 1;
					color[*it] = count;
				}
			}
		}
		count++;
	}
	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (color[from] == color[to]) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	color(n);

	return 0;
}

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

#include<stdio.h>


#include<queue>
using namespace std;
int main()
{
    int person_number,relation_number;

    cin>>person_number>>relation_number;

    vector<vector<int > > list(person_number);
    for(int i=0;i<relation_number;i++)
    {
        int start,end;
        scanf("%d %d ",&start,end);
        int flag=0;
        for(auto n:list[start])
        {
            if(n==end)
                flag=1;
        }
        if(flag==0) 
        {
            list[start].push_back(end);
            list[end].push_back(start);
        }
    }

    int test_number;
    scanf("%d",&test_number);
for(int i=0;i<test_number;i++)
{
    int from,to;
    scanf("%d %d ",&from,&to);
    queue<int> q;
    q.push(from);
    vector<int> ban_list;
    for(int i=0;i<person_number;i++)
    {
        ban_list.push_back(0);
    }
    ban_list[from]=1;
    for(;!q.empty();q.pop())
    {
        int flag=0;
        for(auto n:list[q.front()])
        {
            if(n==to) 
            { 
                flag=1;
                break;
            }
            if(ban_list[n]==0)
            {
                ban_list[n]=1;
                q.push(n);
            }
        }
        if(flag==1) break;
    }
    if(!q.empty()) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
}
    return 0;
}

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

const int MAX_V = 100010;
int par[MAX_V];
int treerank[MAX_V];

void init(int n){
  for(int i=0; i<n; i++){
    par[i] = i;
    treerank[i] = 0;
  }
}

int find(int x){
  if(par[x] == x) return x;
  return par[x] = find(par[x]);
}

void unite(int x, int y){
  x = find(x);
  y = find(y);
  if(x == y) return;
  if(treerank[x] < treerank[y]){
    par[x] = y;
  }
  else{
    par[y] = x;
    if(treerank[x] == treerank[y]) treerank[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

int main(){
  int n, m; cin >> n >> m;
  init(n);
  for(int i=0; i<m; i++){
    int s, t; cin >> s >> t;
    unite(s, t);
  }
  int q; cin >> q;
  for(int i=0; i<q; i++){
    int s, t; cin >> s >> t;
    cout << (same(s, t) ? "yes" : "no") << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
#define _GLIBCXX_DEBUG
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
struct Unionfind{
    vector<int> rank,p,si;//木の高さの管理=rank,p=親の管理
    Unionfind(){}
        //初期化
    Unionfind(int size){
                rank.resize(size,0);
                p.resize(size,0);
              si.resize(size,1);
                for(int i=0;i<size;i++){
                        makeSet(i);
        }
    }
    void makeSet(int x){
                p[x]=x;//親を自分にする。
                rank[x]=0;//最初の高さは0
              si[x]=1;
    }
        //同じ集合か判定。
    bool same(int x,int y){
                return (findSet(x)==findSet(y));
    }
        //集合を併合
    void unite(int x,int y){
                link(findSet(x),findSet(y));
    }
    void link(int x,int y){
      if(x!=y){
              if(rank[x]<rank[y]){
                  p[x]=y;
                  si[y]+=si[x];
        }
              else{
                  p[y]=x;
                  if(rank[x]==rank[y]){
                     rank[y]++; 
            }
                  si[x]+=si[y];
        }
               
      }//cout<<rank[x]<<' '<<rank[y]<<'\n';
    }
          int siz(int x){
              int a=findSet(x); 
              return si[a];
    }             
        //経路圧縮
    int findSet(int x){
        //自分自身と親が等しくなるまで再帰
                if(x!=p[x]){
                        p[x]=findSet(p[x]);
                }
                return p[x];
    }
};
signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    Unionfind uf(n);
    int a,b;
    rep(i,m){
        cin>>a>>b;
        uf.unite(a,b);
    }
    int p;
    cin>>p;
    rep(i,p){
        cin>>a>>b;
        if(uf.same(a,b)){
            cout<<"yes"<<'\n';
        }
        else{
            cout<<"no"<<'\n';
        }
    }
    return(0);
}