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<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[101010];
int next[101010];
int to[101010];
int vis[101010];
//bool search(int st,int ed){
//	queue<int> q;
//	q.push(st);
//	q.push(ed);
//	vis[st]=1;
//	while(!q.empty()){
//		int now=q.front();
//		q.pop();
//		int te=begin[now];
//		while(te!=-1){
//			if(to[te]==ed)
//				return true;
//			if(vis[to[te]]==0){
//				q.push(to[te]);
//				vis[to[te]]=1;
//			}
//			te=next[te];
//		}
//	}
//	return false;
//}

bool doublesearch(int st,int ed){
	int size1=1;
	int size2=1;
	queue<int> q[2];
	q[0].push(st);
	q[1].push(ed);
	vis[st]=1;
	vis[ed]=2;
	while((!q[0].empty())&&(!q[1].empty())){
		if(size1<=size2){
			int now=q[0].front();
			q[0].pop();
			int te=begin[now];
			while(te!=-1){
				if(vis[to[te]]==2)
					return true;
				if(vis[to[te]]==0){
					q[0].push(to[te]);
					vis[to[te]]=1;
				}
				te=next[te];
			}
		}
		else{
			int now=q[1].front();
			q[1].pop();
			int te=begin[now];
			while(te!=-1){
				if(vis[to[te]]==1)
					return true;
				if(vis[to[te]]==0){
					q[1].push(to[te]);
					vis[to[te]]=1;
				}
				te=next[te];
			}
		}
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	int n,m,q;
	int st,ed;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=0;i<m;i++){
			scanf("%d%d",&st,&ed);
			to[++e]=ed;
			next[e]=begin[st];
			begin[st]=e;
			to[++e]=st;
			next[e]=begin[ed];
			begin[ed]=e;
		}
		scanf("%d",&q);
		for(int i=0;i<q;i++){
			memset(vis,0,sizeof(vis));
			scanf("%d%d",&st,&ed);
			if(doublesearch(st,ed))
				printf("yes\n");
			else
				printf("no\n");
		}
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t, p; vector<int>a[200000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	Q.push(A);
	while (!Q.empty()) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			p = x[a[Q.front()][k]];
			if (x[p] == 0) { 
				Q.push(a[Q.front()][k]);
			}
		}
		Q.pop();
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (bfs(s, t)) { cout << "yes" << endl; }
		else { cout << "no" << endl; }
	}
	return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>
using namespace std;

const int MAX = 100000;
const int NIL = -1;
vector<int> G[MAX];
int n;
int color[MAX];

void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    color[r] = c;
    while(! S.empty()){
        int u = S.top();
        S.pop();
        for(int i = 0; i <G[u].size(); ++i){
            int v = G[u][i];
            if(color[v] == NIL){
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assignColor(){
    int id = 1;
    for(int i = 0; i < n; ++i) color[i] = NIL;
    for(int u = 0; u < n; ++u){
        if(color[u] == NIL) dfs(u, id++);
    }
}

int main(){
    int s, t, m, q;
    cin >> n >> m;
    for(int i = 0; i < m; ++i){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    assignColor();
    cin >> q;
    for(int i = 0; i < q; ++i){
        cin >> s >> t;
        if(color[s] == color[t]){
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<int> d(n.size()), dd(n.size());
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
		if (dd[i] == 1)continue;
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
					n[*it].parent = num;
					std::vector<int>::iterator itt= std::find(n[*it].relation.begin(), n[*it].relation.end(), num);
					if (itt != n[*it].relation.end()) {
						n[*it].relation.erase(itt);
					}
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
		if (from == to) return true;
	}
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);
	//makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
	std::cout << i << " " << n[i].parent << std::endl;
	}
	*/
	/*
	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	//*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}