Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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

C++: #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NODE_NUM 100000

queue<int> que;

class Node
{
private:
	int next_num;
	int *next_node_list;
public:
	Node(){};
	void Init(int n);
	void AddNextNode(int id);
	bool Search(int st, int ed);
	bool check; //!
	//bool *check;
	//bool *link;
};

void Node::Init(int n)
{
	next_num = 0;
	next_node_list = new int[n];
	check = 0;
	/*check = new bool[n];
	for (int i = 0; i < n; i++) {
		check[i] = false;
	}*/
	/*link = new bool[n];
	for (int i = 0; i < n; i++) {
		link[i] = false;
	}*/
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node *Node_list;

bool Node::Search(int st, int ed)
{
	//if (link[ed]) return true;

	/*for (int k = 0; k < next_num; k++) {
		Node_list[st].link[next_node_list[k]] = true;
		Node_list[next_node_list[k]].link[st] = true;
	}*/

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == ed) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		//if (Node_list[next_node_list[j]].check[ed] == 0) {
		if (Node_list[next_node_list[j]].check == 0) { //!
		//	Node_list[next_node_list[j]].check[ed] = 1;
			Node_list[next_node_list[j]].check = 1; //!
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n];
	for (int i = 0; i < n; i++) {
		Node_list[i].Init(n);
	}
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		//Node_list[s].check[t] = true;
		Node_list[s].check = true; //!
		bool ret = Node_list[s].Search(s, t);
		while (!ret && !que.empty()) {
			ret = Node_list[que.front()].Search(s, t);
			que.pop();
		};
		while (!que.empty()) {
			que.pop();
		}
		for (int j = 0; j < n; j++) {
			//Node_list[j].check[t] = false;
			Node_list[j].check = false; //!
		}

		if (ret) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}

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


int main()
{
        int n,m,n1,n2,i,j,k,v,a,b,c,s,compindx,f;
        compindx=0;
        cin>>n>>m;
        list<int> adj[100001];
        queue<int>q;
        for(i=0;i<m;i++)
        {
                cin>>n1>>n2;
                adj[n1].push_back(n2);
                adj[n2].push_back(n1);
        }
        int visited[n];
        int id[n];
        for(j=0;j<n;j++)
        {
                visited[j]=0;
        }
        for(j=0;j<n;j++)
        {
               if(visited[j]==0)
                {
                        s=j;
                        q.push(s);
                        while(q.empty()==false)
                        {
                                 v=q.front();
                                 visited[v]=1;
                                 id[v]=compindx;
                                 q.pop();
                                for(list<int>::iterator it = adj[v].begin(); it != adj[v].end(); it ++)
                                {
                                        if(visited[*it]==0)
                                        {
                                                f=*it;
                                                q.push(f);
                                                visited[*it]=1;
                                        }

                                }
                        }
                        compindx++;
                }
           }
        cin>>c;
        for(k=0;k<c;k++)
        {
                cin>>a>>b;
                if(id[a]==id[b])
                {
                        cout<<"yes"<<endl;
                }
                else
                {
                        cout<<"no"<<endl;
                }
        }

}