Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_map>

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

const vector<int>& accesibleNodeSearch(const vector<vector<int>>& graphList
	, unordered_map<int, vector<int>>& visitMatrix
	, const int s
	, vector<bool>& visitList) 
{
	pair<unordered_map<int, vector<int>>::iterator, bool> result = visitMatrix.emplace(s,vector<int>());
	vector<int>& visitIDList = result.first->second;
	if (result.second) {//未計算の場合のみ計算する
		const size_t n = graphList.size();
		queue<int> nodeQueue;
		visitList.resize(n);//resizeは必ずしもその値で埋めてくれない？
		fill(visitList.begin(), visitList.end(), false);
		nodeQueue.push(s);
		visitList.at(s).flip();
		while (!nodeQueue.empty()) {
			const int id = nodeQueue.front();
			const vector<int>& adjList = graphList.at(id);
			for (const auto& adjID : adjList) {
				if (visitList.at(adjID))continue;
				const vector<int>& idList = graphList.at(adjID);
				if (find(idList.begin(), idList.end(), id) == idList.end())continue;
				nodeQueue.push(adjID);
				visitList.at(adjID).flip();
			}
			nodeQueue.pop();
		}
		const int num = count(visitList.begin(), visitList.end(), true);
		visitIDList.reserve(num);
		for (int i = 0; i != n; ++i) {
			if (!visitList.at(i))continue;
			visitIDList.push_back(i);
		}
	}
	return visitIDList;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	unordered_map<int, vector<int>> visitMatrix;
	vector<bool> visitList;
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		const vector<int>& idList = accesibleNodeSearch(graphList, visitMatrix, s, visitList);
		cout << ( find(idList.cbegin(), idList.cend(),t) != idList.cend() ? "yes" : "no") << endl;
	}
}

int main()
{

	execute(cin);


	return 0;
}

C++: #include<iostream>
#include<vector>
#define LEN 100005

using namespace std;

vector<int> mark[LEN];
int lk[LEN]={0};
bool status[LEN]={0};
int fleed = 1;
int n;
/*
void find(int a){
//	if()return false;
	if(a==b)return true;
	status[a]=true;
	int len = mark[a].size();
	for(int i =0;i<len;++i){
		if(!status[mark[a][i]]){
		lk[mark[a][i]] = fleed;	
		if(find(mark[a][i],b))return true;
		}
	}

	return false;
	
}
*/

void dfs(int i){
	status[i] = true;
	lk[i] = fleed;
	for(int k = 0;k<mark[i].size();++k){
		if(!status[mark[i][k]])dfs(mark[i][k]);
	} 
}
void dfs(){
	for(int p = 0;p<n;++p){
		if(!status[p]){
			dfs(p);
			++fleed;
		}
	}
}
inline void insert(int a,int b){
	mark[a].push_back(b);
	mark[b].push_back(a);
}

int main(){
	int m,s,tmpa,tmpb;
	cin>>n>>m;
	while(m--){
		cin>>tmpa>>tmpb;
		insert(tmpa,tmpb);
	}
	dfs();
	cin>>s;
	while(s--){
	cin>>tmpa>>tmpb;
	if(lk[tmpa]&&lk[tmpa]==lk[tmpb])cout<<"yes"<<endl;
	else cout<<"no"<<endl;
	}
	return 0;
}

C++: /*
** Connected Components
*/

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

const int MAX = 100000;

int n, m, q, c[MAX];
vector<int> g[MAX];

void init() {
    scanf("%d%d", &n, &m);
    int u, v;
    while (m--) {
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
}

void bfs(int u, int mark) {
    queue<int> que;
    que.push(u);
    c[u] = mark;

    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = 0; i < g[t].size(); i++) {
            if (c[g[t][i]] == 0) {
                c[g[t][i]] = mark;
                que.push(g[t][i]);
            }
        }
    }
}

void set_c() {
    int mark = 1;
    for (int i = 0; i < n; i++) {
        c[i] = 0;
    }
    for (int i = 0; i < n; i++) {
        if (c[i] == 0) {
            bfs(i, mark++);
        }
    }
}

void solve() {
    init();
    scanf("%d", &q);
    set_c();
    int u, v;
    while (q--) {
        scanf("%d%d", &u, &v);
        if (c[u] == c[v]) {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
}

int main() {
    // freopen("infile", "r", stdin);
    // freopen("outfile", "w", stdout);
    solve();
    return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
int n,m;
int e;
int begin[301010];
int next[301010];
int to[301010];
int vis[301010];
void add(int x,int y){
	to[++e]=y;
	next[e]=begin[x];
	begin[x]=e;
}

bool search(int st,int ed){
	queue<int> q;
	q.push(st);
//	cout<<"push"<<st<<endl;
	vis[st]=1;
	while(!q.empty()){
		int now=q.front();
	//	cout<<q.size()<<endl;
		q.pop();
		int te=begin[now];
		while(te!=-1){
			if(to[te]==ed)
				return true;
			if(vis[to[te]]==0){
				q.push(to[te]);
		//		cout<<"push"<<to[te]<<endl;
				vis[to[te]]=1;
			}
			te=next[te];
		}
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	int n,m,q;
	while(cin>>n>>m){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=0;i<m;i++){
			int st,ed;
			cin>>st>>ed;
			add(st,ed);
			add(ed,st);
		}
		cin>>q;
		for(int i=0;i<q;i++){
			memset(vis,0,sizeof(vis));
			int st,ed;
			cin>>st>>ed;
			if(search(st,ed))
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	return 0;
}