Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<iostream>
#include<vector>
#inlcude<stack>
using namespace std;
#define MAX 100000
#define NIL -1

int n, color[MAX];
vector<int> G[MAX];


void dfs(int r, int c) {
  stack<int> S;
  int i, v, u;
  S.push(r);
  color[r]=c;
  while (! S.empty()) {
    u=S.top();
    S.pop();
    for (i=0; i<G[u].size(); i++) {
        v=G[u][i];
      if (color[v]==NIL) {
        color[v]=c;
        S.push(v);
      }
    }
  }
}



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

int main() {

  int  m, i, j, v, s, t, q;

  cin >> n >> m;

  for (i=0; i<n; i++)
    for (j=0; j<n; j++)
      G[i][j]=0;

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

  assignColor();

  cin >> q;

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

  return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;

int par[100001], ran[100001];
int find(int x){
	if(par[x] == x)return x;
	else return par[x] = find(par[x]);
}

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

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

int main(){
	int n, m, q, x, y;
	scanf("%d%d", &n, &m);
	for(int i = 0;i < n;i++)par[i] = i;
	for(int i = 0;i < m;i++){
		scanf("%d%d", &x, &y);
		unite(x, y);
	}
	scanf("%d", &q);
	for(int i = 0;i < q;i++){
		scanf("%d%d", &x, &y);
		if(same(x, y))printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

int find(int i, vector<int>& ds) {
    if (ds[i] == i) return i;
    ds[i] = ds[ds[i]];
    return find(ds[i], ds);
}

bool unionSets(int i, int j, vector<int>& ds) {
    int p1 = find(i, ds);
    int p2 = find(j, ds);
    if (p1 == p2) return false;

    ds[p1] = p2;
    return true;
}

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

    int n, m;
    cin>>n>>m;
    vector<int> ds(n);
    rep(i, n) ds[i] = i;
    rep(i, m) {
        int v1, v2;
        cin>>v1>>v2;
        unionSets(v1, v2, ds);
    }

    int q;
    cin>>q;
    rep(i, q) {
        int s, t;
        cin>>s>>t;
        if (find(s, ds) == find(t, ds)) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 100005

using namespace std;
int n, m, q;
vector<int> A[Lens];
queue<int>	Q;

void Find(int s, int t)
{
	Q.push(s);
	while (!Q.empty())
	{
		s = Q.front();
		Q.pop();
		for (int j = 0; j < A[s].size(); j++)
		{
			Q.push(A[s][j]);
			if (A[s][j] == t)
			{
				cout << "yes" << endl;
				return;
			}
		}
	}
	cout << "no" << endl;
	return;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		A[s].push_back(t);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int flag = 0;
		int s, t;
		cin >> s >> t;
		Find(s, t);
	}
	return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using ll = long long;
using VL=vector<ll>;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
const long long INF =  999999999;
int n,m,q,d[100010],used[100100];
vector <int> g[100010];
vector <string> str;

//numは連結成分の番号
void dfs(int v,int num){
  used[v]=true;
  d[v]=num;
  REP(i,g[v].size()){
    if(!used[g[v][i]])dfs(g[v][i],num);
  }

}



int main(){
  cin >>n>>m;
  REP(i,m){
    int s,t;
    cin >>s>>t;
    g[s].push_back(t);
    g[t].push_back(s);
  }
  REP(i,n)used[i]=false;

  REP(i,n){
    if(!used[i])dfs(i,i);
  }
  cin >>q;
  REP(i,q){
    int s,t;
    cin >>s>>t;
    if(d[s]==d[t])str.push_back("yes");
    else str.push_back("no");
  }
  REP(i,str.size()){
    cout <<str[i]<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef float fl;
typedef long long ll;

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();
       rep(i,G[u].size()){
           int v = G[u][i];
           if( color[v] == NIL ){
                color[v] = c;
                s.push(v);
           }
       }
    }
}

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

int main(){
    int s,t,m,q; 
    cin >> n >> m;
    rep(i,m){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor();

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