Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#define N 100

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 << "OK" << 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();
                //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<stdio.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        visited.resize(n,false);
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        if(s1!=s2&&s1=="yes")return 1;
        cout<<s2<<endl;
    }
    return 0;
}

C++: /*
	 AOJ DSL_1
	 ??????????´??????????
	 2??????????´????????????????????±??????????????????¶=2??????????´??????????????????§??????
	 ?????????????????¨???father[]?????´??\?????§???????????§????????????find??¢??°??§??¢?´¢??????????????????
	 ??????????????????????????¨?????¨??????????????????
	 */
#include<iostream>
#include <string>
#define maxn 100100
using namespace std;
int n,q,m;
int father[maxn];

void init(){
	for(int i=0;i<n;i++)father[i]=i;
}

int find(int x){ // x?????????????????¢??°
	if(father[x]==x)return x; // ?????????????????????????????´??????x???????????????
	return father[x]=find(father[x]); // ??????????????§????????°??????????????????????????????(????????§?????????)
}

void unite(int x,int y){
	father[find(x)]=find(y); // x????????¨y????????????????????????
}

string same(int x,int y){
	if(find(x)==find(y))return "yes"; // ????????????????????????
	else return "no";
}

int main(void){
	cin>>n>>m;
	init();
	while(m--){
		int x,y;
		cin>>x>>y;
		unite(x,y);
	}
	cin>>q;
	while(q--){
		int x,y;
		cin>>x>>y;
		cout<<same(x,y)<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<vector<int>> makelist(int n, int m) {
	vector<vector<int>> Adjlist(n);
	int a, b;

	for(int i = 0; i < m; i++) {
		cin >> a >> b;
		Adjlist[a].push_back(b);
		Adjlist[b].push_back(a);
	}

	return Adjlist;
}

bool dfs_run(int a, int b, vector<vector<int>>& adj, vector<bool>& checker) {
	while(!adj[a].empty()) {
		int tmp;
		tmp = adj[a].back();
		adj[a].pop_back();	
		if(tmp == b) {
			return true;
		}else{
			return dfs_run(tmp, b, adj, checker);
		}
	}
	return false;
}

bool dfs(int n, int a, int b, vector<vector<int>>& adj) {
	vector<bool> checker(n, true);
	
	checker[a] = false;
	if(dfs_run(a, b, adj, checker)) {
		return true;
	}
	return false;
}

int main(void)
{
	int n, m, q;

	cin >> n >> m;
	vector<vector<int>> Adjlist = makelist(n, m);

	cin >> q;
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;

		if(dfs(n, a, b, Adjlist)) {
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}

	return 0;
}