Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t 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() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		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()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: #include<iostream>    // ???????????????????§?????????£????????????
using namespace std;
#include<stdio.h>

typedef struct fox { int val; fox *prev, *next; } fox_t;

fox_t* init()
{
    fox_t* head = new fox_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

fox_t* make_tail(fox_t* head)
{
    fox_t* tail = new fox_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(fox_t* head, fox_t* tail)
{
    return (head->next == tail ? true : false);
}

fox_t* b_insert(fox_t* tail, int data)
{
    fox_t* fox = new fox_t;
    fox->val = data;
    fox->prev = tail->prev;
    fox->next = tail;
    tail->prev->next = fox;
    tail->prev = fox;
    return fox;
}
// docking.
void unionfox(fox_t* head1, fox_t* tail1, fox_t* head2, fox_t* tail2)
{
    if(isEmpty(head1, tail1) || isEmpty(head2, tail2)) return;
    fox_t* fox = tail1->prev;
    fox->next = head2->next;
    head2->next->prev = fox;
    tail2->prev->next = tail1;
    tail1->prev = tail2->prev;
    delete tail2;
    delete head2;
}

int main()
{
    int color[100001];
    int c_num = 0;
    fox_t* foxhead[100001];
    fox_t* foxtail[100001];
    int i, n, m, q;
    int a, b;
	scanf("%d %d", &n, &m);
    for(i = 0; i <= n; i++){
        color[i] = -1;
        foxhead[i] = init();
        foxtail[i] = make_tail(foxhead[i]);
    }
    int c_a, c_b, val;

    for(i = 0; i < m; i++){
		scanf("%d %d", &a, &b);
        c_a = color[a];
        c_b = color[b];
        if(c_a < c_b){ swap(c_a, c_b); swap(a, b); }  // color[a] >= color[b].
        if(c_a < 0 && c_b < 0){
            color[a] = c_num; color[b] = c_num;
            b_insert(foxtail[c_num], a);
            b_insert(foxtail[c_num], b);
            c_num++;
        }else if(c_b < 0){
            color[b] = c_a;
            b_insert(foxtail[c_a], b);
        }else if(c_a == c_b){
            continue;
        }else{ // color[a] != color[b].
            fox_t* fox = foxhead[c_b]->next;
            while(fox != foxtail[c_b]){
                val = fox->val;
                color[val] = c_a;
                fox = fox->next;
            };
            unionfox(foxhead[c_a], foxtail[c_a], foxhead[c_b], foxtail[c_b]);
        }
    }
    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(color[a] >= 0 && (color[a] == color[b])){
            printf("yes\n"); }else{ printf("no\n"); 
            }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
int M[N][N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, n) {
      if (M[s][i] == 1 && !V[i]) {
        q.push(i);
        V[i] = true;
      }
    }
  }

  return V[v];
}

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

  REP(i, n) {
    REP(j, n) {
      M[i][j] = 0;
    }
  }

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u][v] = M[v][u] = 1;
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int main(){
    //input
    int n,m;
    std::cin>>n;
    std::cin>>m;
    int A[n][n];
    bool visited[n];
    int judge[n];

    for(int i=0;i<n;i++){
        judge[i]=-1;
        visited[i]=false;
        A[i][0]=0;
        for(int j=1;j<n;j++){
            A[i][0]=0;
            A[i][j]=-1;
        }    
    }

    int cnt_i=0,cnt_j=0;
    for(int k=0;k<m;k++){
        int i,j;
        std::cin>>i;
        std::cin>>j;
        A[i][0]++; cnt_i=A[i][0];
        A[j][0]++; cnt_j=A[j][0];
        A[i][cnt_i]=j;
        A[j][cnt_j]=i;
    }

    /*for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            std::cout<<" "<<A[i][j];
        }
        std::cout<<std::endl;
    }*/

    int n_2;
    std::cin>>n_2;
    for(int i=0;i<n_2;i++){
        int s,e;
        std::cin>>s;
        std::cin>>e;
        
        int cnt=0;
        for(int i=0;i<n;i++){
            judge[i]=-1;
            visited[i]=false;
        }
        
        std::queue<int> que;
        que.push(s);
        /*for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                //std::cout<<" "<<A[i][j];
            }
            //std::cout<<std::endl;
        }*/
        while(!que.empty()){
            int q=que.size();
            for(int k=0;k<q;k++){
                //std::cout<<"quesize"<<que.size()<<"\n";
                int i=que.front();
                que.pop();
                visited[i]=true;
                judge[i]=cnt;
                //std::cout<<i<<"cnt"<<cnt<<"\n";
                //std::cout<<A[i][0]<<"\n";
                if(A[i][0]!=0){
                    for(int j=0;j<A[i][0];j++){
                        if(visited[A[i][j+1]]!=true){
                            visited[A[i][j+1]]=true;
                            que.push(A[i][j+1]);
                        }
                    }
                }
            }
            cnt++;
        }
        if(judge[e]==-1){
            std::cout<<"no\n";
        }else std::cout<<"yes\n";
    }

    return 0;
}