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 <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define MAX_N 100000
#define MAX_M 100000
#define WHITE 1
#define GREY 2
#define BLACK 3

vector<int> G[MAX_N];
int V[MAX_N];
int V_num[MAX_N];

void init(void){
    for(int i = 0; i < MAX_N; i++){
        V[i] = WHITE;
        V_num[i] = i;
    }
}

void labeling(int target){
    queue<int> Q;
    Q.push(target);
    while(!Q.empty()){
        int num = Q.front(); Q.pop();
        for(int i = 0; i < G[num].size(); i++){
            if(V[G[num][i]] == WHITE){
                Q.push(G[num][i]);
                V[G[num][i]] = GREY;
                V_num[G[num][i]] = target;
            }
        }
        V[num] = BLACK;
    }
}

int main(void){
    init();
    int n, m;
    scanf("%d %d\n", &n, &m);
    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }

    for(int i = 0; i < n; i++){
        if(V[i] != WHITE){
            continue;
        }
        labeling(i);
    }

    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        if(V_num[a] == V_num[b]){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }

    /*
    for(int i = 0; i < n; i++){
        printf("%d %d\n", i, V_num[i]);
    }
    */
}

C++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void bfs(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    if (arrived[y] != 0) return;
    queue<int> mq;
    mq.push(x);
    while(!mq.empty()) {
        int curr = mq.front();
        mq.pop();
        arrived[curr] = 2;
        for (int i = 0; i < sns[curr].size(); i++) {
            int next = sns[curr][i];
            if (arrived[next] == 0) {
                mq.push(next);
                arrived[next] = 1;
            }
        }
    }
}

int main()
{
    freopen("ALDS1_11_D_in22.txt", "r", stdin);
    freopen("out22.txt", "w", stdout);
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>());
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x].push_back(y);
        sns[y].push_back(x);
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        bfs(sns, arrived, x, y);
        if (arrived[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

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


#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;
        cin>>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;
    cin>>test_number;
for(int i=0;i<test_number;i++)
{
    int from,to;
    cin>>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;
}