Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

}