Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#define MAX 100000

void input_graph(std::vector<std::vector<int> >& g, int m){
    int s, t;
    for(int i=0; i<m; i++){//双連結リストを作る
        std::cin >> s >> t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
}

void result(std::string ans[], int q){
    for(int i=0; i<q; i++){
        std::cout << ans[i] << '\n';
    }
}

void CC(std::vector<std::vector<int> >& g, std::string ans[], int q, int n){
    int s, t;
    int cnt = 0;
    std::vector<int> tmp;   //軌跡を保存
    //深さ優先探索
    while(cnt < q){
        //std::cout << "\nOK" << cnt << '\n';
        std::cin >> s >> t;
        int i = s;
        int nexti;
        int j;
        bool flag[n] = {false};
        bool f = false; //false:未到達 true:到達or終了
        while(!f){
            while(i != -1){
                //std::cout << "start\n";
                tmp.push_back(i);
                //std::cout << "i:" << i << '\n';
                flag[i] = true;
                //次の葉を探す
                if((int)g[i].size() != 0){//葉が最深でないとき
                    j = 0;
                    nexti = g[i][j];
                    while(flag[nexti]){
                        j++;
                        if(j == (int)g[i].size()){//葉がすべて探索済みなら
                            i = -1; //2つ目のwhile文を抜ける
                            break;
                        }
                        nexti = g[i][j];
                    }
                    if(i != -1){
                        if(nexti == t){
                            ans[cnt] = "yes";
                            i = -1;
                            f = true;
                            break;
                        }
                        i = nexti;
                    }
                }
                else{//葉が最深のとき
                    i = -1;
                }
            }
            if(!f){//まだ到達していなければ
                //ここから根に向かって戻る. 未探索の葉があればそちらへ行く
                //std::cout << "back\n";
                tmp.pop_back();
                i = (int)tmp.back();
                if(i > MAX){
                    ans[cnt] = "no";
                    f = true;
                    break;
                }
                //std::cout << "i:" << i << '\n';
                tmp.pop_back();
                j = 0;
                nexti = g[i][j];
                while(flag[nexti]){
                    j++;
                    if(j == (int)g[i].size()){//現在のノードから未探索の葉がない
                        if((int)tmp.size() != 0){//まだtmpに軌跡が残っているなら
                            i = (int)tmp.back();
                            //std::cout << "i:" << i << '\n';
                            tmp.pop_back();
                            j = 0;
                            nexti = g[i][j];
                        }
                        else{//tmpに軌跡が残っていないなら
                            ans[cnt] = "no";
                            f = true;
                            break;
                        }
                    }
                }
            }
            i = nexti;
        }
        tmp.clear();    //一応clear
        cnt++;          //次のquestionへ
    }
}

int main(){
    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<int> > g;
    g.resize(n);
    input_graph(g, m);
    int q;
    std::cin >> q;          //質問数
    std::string ans[q];     //yes or no
    CC(g, ans, q, n);
    result(ans, q);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);

using namespace std;

vector<int> G[200001];
int color[200001];
int s[10000],head,tail;
void dfs(int r,int c){
  head=tail=0;
  s[tail++]=r;
  color[r]=c;
  while(head!=tail){
    int u=s[head++];
    for(int i=0;i<(int)G[u].size();i++){
      int v=G[u][i];
      if(color[v]==-1){
	s[tail++]=v;
	color[v]=c;
      }
    }
  }
}
int main(){
  int n,m,ia,ib,p,id=1;
  rep(i,200000)color[i]=-1;
  cin>>n>>p;
  rep(i,p){
    cin>>ia>>ib;
    G[ia].push_back(ib);
    G[ib].push_back(ia);
  }
  rep(i,n)if(color[i]==-1)dfs(i,id++);
  cin>>m;
  rep(i,m){
    cin>>ia>>ib;
    if(color[ia]==color[ib])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }

  return 0;
}