Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector< vector<int> > M;
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  cin >> n >> m;
  REP(i, n) {
    vector<int> vc;
    M.push_back(vc);
  }
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

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

int N,M;
const int maxn = 100000+5;
const int WHITE = 0;
vector<int> G[maxn];
vector<int> nodeColor;

void DFS(int u,int color){
    stack<int> nodeStack;
    nodeStack.push(u);
    while(!nodeStack.empty()){
        int u = nodeStack.top();
        nodeStack.pop();
        nodeColor[u] = color;
        for(int i = 0;i<G[u].size();i++){
            int v = G[u][i];
            if(nodeColor[v]==0) nodeStack.push(v);
        }
    }
}

int main(){
    //freopen("data2.txt","r",stdin);
    cin >> N >> M;
    nodeColor.resize(N,0);
    int u,v;
    for(int i = 0;i<M;i++){
        scanf("%d %d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int color = 1;
    for(int i = 0;i<N;i++) if(nodeColor[i]==0) DFS(i,color++);
    int q;
    scanf("%d",&q);
    for(int i = 0;i<q;i++){
        scanf("%d %d",&u,&v);
        if(nodeColor[u]==nodeColor[v]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<algorithm>

#define MAX 100000

int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;


bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;

    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }

    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }

    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        bfs(s, d) ? printf("yes\n") : printf("no\n");
    }

}

C++: #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int,int> PII;
typedef long long LL;

#define d(x)  cout<<#x<<" = "<<(x)<<endl;
#define c cout<<"#"<<endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define DREP(i,n) DFOR(i,n,0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);
////////////////////////////////////////////////
VVI a;
int n,b,d[100010],p[100010];
QI q;
int main()
{
	//??£????????????????????????
	cin>>n>>b;
	REP(i,n+1)
	{
		a.push_back({0});
	}
	REP(i,b)
	{
		int s,t;cin>>s>>t;s++;t++;
		a[s][0]++;
		a[s].push_back(t);
		a[t][0]++;
		a[t].push_back(s);
	}
	FOR(i,1,n+1)
	{
		sort(&a[i][1],&a[i][a[i][0]+1]);
	}
	
	REP(i,n+1)p[i]=1,d[i]=0;
	d[1]=1;
	int g=1;
	for(q.push(1);SZ(q)!=0;)
	{
		int u=q.front();
		q.pop();
		if(a[u][0]==0)
		{
			d[u]=g;g++;
		}
		else
		{
			for(;p[u]<=a[u][0];)
			{
				int v=a[u][p[u]];p[u]++;
				if(v!=1&&d[v]==0)
				{
					d[v]=g;
					q.push(v);
				}
			}
		}
		if(q.empty())
		{
			int i;
			for(i=2;d[i]!=0;i++);
			if(i>n)
			{
				break;
			}
			else
			{
				g++;
				q.push(i);
			}
		}
	}
	cin>>n;
	REP(i,n)
	{
		int first,second;
		cin>>first>>second;first++;second++;
		if(d[first]==d[second])cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}