Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     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() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		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()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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 <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_set>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< vector<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< vector<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).push_back(t);
		friendGraphList.at(t).push_back(s);
	}
	for (auto& v : friendGraphList)v.shrink_to_fit();
}

bool checkFriendAccesibility(const vector<vector<int>>& graphList, const int s, const int t) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	//vector<int> distanceList(n, -1);//
	vector<bool> isVisitList(n, false);//距離に興味はないのでbool
	nodeQueue.push(s);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		isVisitList.at(id).flip();//trueにする
		const auto& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			if (isVisitList.at(adjID))continue;
			const vector<int>& idList = graphList.at(adjID);
			if ( find(idList.begin(), idList.end(), id ) == idList.end())continue;
			if (adjID == t)return true;
			nodeQueue.push(adjID);
		}
		nodeQueue.pop();
	}
	return false;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << (checkFriendAccesibility(graphList, s, t) ? "yes" : "no") << endl;
	}
}


int main()
{

	execute(cin);
	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

class TUser
{
	std::set<TUser*> FAccessible;
public:
	void AddFriend(TUser* Friend);
	bool IsAccesible(TUser* Target)const;
};

void TUser::AddFriend(TUser* Friend)
{
	if(IsAccesible(Friend)) return;
	FAccessible.insert(Friend);
	for(std::set<TUser*>::iterator It = FAccessible.begin(); It != FAccessible.end(); ++It){
		(*It)->AddFriend(Friend);
		Friend->AddFriend(*It);
	}
}

bool TUser::IsAccesible(TUser* Target)const
{
	return (FAccessible.find(Target) != FAccessible.end());
}

class TSns
{
	std::vector<TUser> FUsers;
public:
	TSns(int UserCount);
	
	void AddLink(int UserA, int UserB);
	std::string IsAccesible(int UserA, int UserB);
};

TSns::TSns(int UserCount)
: FUsers(UserCount)
{
}

void TSns::AddLink(int UserA, int UserB)
{
	FUsers[UserA].AddFriend(&FUsers[UserB]);
}

std::string TSns::IsAccesible(int UserA, int UserB)
{
	if(FUsers[UserA].IsAccesible(&FUsers[UserB])) return "yes";
	return "no";
}

int main()
{
	int UserCount;
	std::cin >> UserCount;
	TSns Sns(UserCount);
	
	int LinkCount;
	std::cin >> LinkCount;
	for(int i = 0; i < LinkCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		Sns.AddLink(UserA, UserB);
	}
	
	int QuestionCount;
	std::cin >> QuestionCount;
	for(int i = 0; i < QuestionCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		std::cout << Sns.IsAccesible(UserA, UserB) << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
vector< vector<int> > v(10001);
//stack<int> st;
int n, m;
bool f[10001] = { false };

int main() {
	cin >> n >> m;
	for (int i = 0;i < m; i++) {
		int a, b; cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int q; cin >> q;
	vector<string> Ans(q);
	for (int i = 0; i < q; i++) {
		int s, t; cin >> s >> t;

		stack<int> st;
		st.push(s);
		while (!st.empty()) {
			int a = st.top(); st.pop();
			f[a] = true;
			if (a == t) { break; }
			for (int i = 0; i < v[a].size();i++) {
				if (f[v[a][i]] == false) {
					st.push(v[a][i]);
				}
			}
		}
		if (f[t] == true) { Ans[i] = "yes"; memset(f, false, n); }
		else { Ans[i] = "no"; }
	
	}
	for (int i = 0; i < q;i++) {
		cout << Ans[i] << endl;
	}
}