Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

C++: #include <cstdio>
#include <cctype>
#include <vector>
using namespace std;

#define si static inline
#define gcu getchar_unlocked
si int in() {
	int c, n = 0;
	bool minus = false;
	if ((c = gcu()) == '-') minus = true; else ungetc(c, stdin);
	while ((c = gcu()) >= '0' && c <= '9') n = 10 * n + (c - '0');
	return minus ? -n : n; }
si void scan(char *s) {while (!isspace(*s++ = gcu()));}
#define pcu putchar_unlocked
#define svo si void out
template <typename T>
svo(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
svo(const char *s){while(*s)pcu(*s++);}
svo(char *s){while(*s)pcu(*s++);}
svo(char c){pcu(c);}
template <typename T>
//svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
template <typename head, typename... tail>
svo(head&& h, tail&&... t){out(h);out(move(t)...);}

typedef vector<int> v;
struct graph {
	vector<v> nodes;

	graph(int n) {
		nodes.resize(n);
	}
	void read(int n) {
		while (n--) {
			int s = in(), t = in();
			nodes[s].push_back(t);
			nodes[t].push_back(s);
		}
	}
	bool find(int s, int t, v &f) {
		if (s == t)
			return true;
		f[s] = 1;
		for (int x: nodes[s])
			if (!f[x] && find(x, t, f))
				return true;
		return false;
	}
	bool find(int s, int t) {
		v foot_stamp(nodes.size());
		return find(s, t, foot_stamp);
	}
};

int main() {
	int n = in(), m = in();
	graph g(n);
	g.read(m);
	int q = in();
	while (q--) {
		int s = in();
		out(g.find(s, in())? "yes" : "no", '\n');
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

// grp[i]:i番目の強連結成分に含まれる頂点一覧 iはトポロジカル順
class SCC {
	public:
	static const int MV = 100000;
	vector<vector<int> > grp; int NV,cmp[MV];
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
	void init(int NV) { this->NV=NV; for(int i=0;i<MV;i++) { E[i].clear(); RE[i].clear();}}
	void add(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) {vis[cu]=1; cmp[cu]=ind; grp[ind].push_back(cu);
		for(int i=0;i<RE[cu].size();++i) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0; grp.clear(); grp.resize(MV); NUM.clear();
		memset(vis,0,sizeof(vis)); for(int i=0;i<NV;i++) if(!vis[i]) dfs(i);
		memset(vis,0,sizeof(vis)); for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]])
			{ grp[c].clear(); revdfs(NUM[i],c); sort(grp[c].begin(),grp[c].end()); c++;}
		grp.resize(c);
	}
};

int n, m, a, b, q, c[100100];
SCC scc;

/*
vector<int> scc(int n, vector<int> e[]) {
	int sz = 0, s = 0;
	vector<int> rg[n], stk, cmp(n, -1), added(n), visited(n), ord(n);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < e[i].size(); ++j) rg[e[i][j]].push_back(i);
		sz += e[i].size();
	}
	stk.resize(n + sz), sz = 0;
	for (int i = 0; i < n; i++) {
		if (visited[i]) continue;
		stk[s = 1] = i;
		while (s != 0) {
			int v = stk[s - 1];
			visited[v] = true;
			bool pushed = false;
			for (int j = 0; j < e[v].size(); ++j) {
				int dst = e[v][j];
				if (!visited[dst]) {
					stk[s++] = dst;
					pushed = true;
				}
			}
			if (pushed) continue;
			int t = stk[s - 1];
			if (!added[t]) added[t] = true, ord[n - ++sz] = t;
			--s;
		}
	}
	int k = 0;
	for (int i = 0; i < n; ++i) {
		if (cmp[ord[i]] != -1) continue;
		s = 0, stk[s++] = ord[i];
		while (s != 0) {
			int v = stk[--s];
			cmp[v] = k;
			for (int j = 0; j < rg[v].size(); ++j) {
				int d = rg[v][j];
				if (cmp[d] == -1) stk[s++] = d;
			}
		}
		++k;
	}
	return cmp;
}
*/

// paint
void rec(int v, int x) {
	c[v] = x;
	for (int i = 0; i < scc.E[v].size(); ++i) {
		if (c[scc.E[v][i]]==0) rec(scc.E[v][i],x);
	}
}

int main() {
	cin >> n >> m;
	scc.init(n);
	for (int i = 0; i < m; ++i) {
		cin >> a >> b;
		scc.add(a,b);
	}
	
	scc.scc();
	int m = 0;
	for (int i = 0; i < scc.grp.size(); ++i) {
		for (int j = 0; j < scc.grp[i].size(); ++j) {
			if (c[scc.grp[i][j]]==0) rec(scc.grp[i][j],++m);
		}
	}
	
	cin >> q;
	while (q--) {
		cin >> a >> b;
		cout << (c[a]==c[b] ? "yes" : "no") << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int id;
    vector<int> children{};
    int visited = 0;
    int depth = -1;
};
 
deque<int> schedule; 
vector<status> graph;

bool bfs(int target_v){
    if (not schedule.empty()){
        
        int now_v = schedule.front();
        schedule.pop_front();
        if(now_v==target_v) return true;
        
        for(auto& next_v:graph[now_v].children){
            if(graph[next_v].visited) continue;
            
            graph[next_v].visited = 1;
            schedule.push_back(next_v);
        }
        return bfs(target_v);
    }
    return false;
}

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //setup
    int nb_v; cin >> nb_v;
    int nb_e; cin >> nb_e;
    graph.resize(nb_v);
    for(int i=0; i<nb_e; ++i){
        int v1, v2; cin >> v1 >> v2;
        graph[v1].children.push_back(v2);
        graph[v2].children.push_back(v1);
    }
    for(auto& a:graph){
        sort(a.children.begin(),a.children.end());
    }
    
    //bfs
    int times; cin>>times;
    for(int i=0; i<times; ++i){
        int from_v; cin>>from_v;
        int to_v; cin>>to_v;
        schedule = {};
        schedule.push_back(from_v);
        cout<<(bfs(to_v)? "yes\n":"no\n");
    }
}