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