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<map>
#include<set>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, stc = 0;
	bool answer_yes = false;
	map<int, set<int> > Adj;
	map<int, int> Stack;
	set<int> Check;
	set<int>::iterator it;

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].insert(t);
		Adj[t].insert(s);
	}

	/*for (auto it1 = Adj.begin(); it1 != Adj.end(); it1++) {
		cout << "\n" << it1->first << " : " << flush;
		for (auto it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;
		
		// calculate
		Stack.insert(pair<int, int>(stc, s));
		Check.insert(s);
		while (!Stack.empty()) {
			id = Stack[stc];
			//cout << "\nid : " << id << " stc : " << stc << endl;
			for (it = Adj[id].begin(); it != Adj[id].end(); it++) {
				//cout << *it << " " << flush;
				if (Check.find(*it) == Check.end()) {
					if (*it == t) {
						answer_yes = true;
						Stack.clear();
						break;
					}
					stc++;
					Stack.insert(pair<int, int>(stc, *it));
					Check.insert(*it);
					break;
				}
			}
			if (it == Adj[id].end()) {
				Stack.erase(stc);
				stc--;
			}
		}
		if (answer_yes) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
			Stack.clear();
		}
		answer_yes = false;
		Check.clear();
		stc = 0;
	}
	
	return 0;
}

C++: #include<iostream>
#define MAX 1000001
using namespace std;

int main() {
	int i, n, m, q, a, b, gnum = 0, ptog[MAX] = {};

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		if (ptog[a] != 0)ptog[b] = ptog[a];
		else if (ptog[b] != 0)ptog[a] = ptog[b];
		else ptog[a] = ptog[b] = ++gnum;
	}

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> a >> b;
		if (ptog[a] == ptog[b])cout << "yes\n";
		else cout << "no\n";
	}
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};
bool r;

void dfs (int v, int marker){
	if(c[v]!=0){
		r=false;
		return;
	}
	r=true;
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		dfs(i,marker);
		if (r) marker++;
	}
	
	
	for (int i=0;i<n;i++){
		cout << c[i] <<endl;
	}
	
	cin >> q;
	for (int i=0;i<q;i++){
		cin >> s >> t;
		if(c[s]==c[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	return 0;
}

C++: // 連結成分
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>
#include<stack>

using namespace std;

#define MAX_N 100000

// 値の定義
const int INV = -1;

const int REACH = 1;
const int UN_REACH = 0;

// 友達関係を保持するための構造体
typedef struct
{
    // 友達関係の個数
    int relation_num;
    // 関係を格納するための vector 配列
    vector<int> v_relation;
}st_Relation;

// 友達関係を保持する配列
st_Relation ary_Link[MAX_N + 1];

// 訪問情報を保持する配列
int ary_visit[MAX_N];

bool search_relation(int start, int goal, int user_num);

int main(void)
{
    int n, m, q, s, t, ret;

    for(int i = 0; i < MAX_N + 1; i++)
    {
        // 配列の初期化
        ary_Link[i].relation_num = 0;
        ary_visit[i] = UN_REACH;
    }

    cin >> n >> m;

    for(int i = 0; i < m; i++)
    {
        cin >> s >> t;

        // 友達関係を双方向に設定する
        ary_Link[s].relation_num++;
        ary_Link[s].v_relation.push_back(t);

        ary_Link[t].relation_num++;
        ary_Link[t].v_relation.push_back(s);
    }

    cin >> q;

    for(int i = 0; i < q; i++)
    {
        cin >> s >> t;

        // 友達関係の検索を実施
        ret = search_relation(s, t, n);

        if( ret )
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }

        for(int i = 0; i < n; i++)
        {
            // 訪問情報の初期化
            ary_visit[i] = UN_REACH;
        }
    }

    return 0;
}

// 友達関係を検索するための関数
// start    : 検索を開始する最初のユーザー
// goal     : 友達関係の最後のユーザー
// user_num : 全ユーザー数
bool search_relation(int start, int goal, int user_num)
{

    bool ret = false;
    int user_id;

    queue<int> Q;

    // 開始点をキューにプッシュする
    Q.push(start);

    while( !Q.empty() )
    {
        // キューの先頭要素を取得して, 削除する
        // ( 一人のユーザーの巡回は1回だけ )
        int u = Q.front();
        Q.pop();

        // 訪問情報の更新
        ary_visit[u] = REACH;

        for(int v = 0; v < ary_Link[u].relation_num; v++)
        {
            // 友達関係にあるユーザーの検索
            user_id = ary_Link[u].v_relation[v];

            if( ary_visit[user_id] == UN_REACH )
            {
                // ----- 未訪問の頂点が見つかった -----

                if( user_id == goal )
                {
                    // 友達のつながりが確認できたので処理を終了する
                    ret = true;
                    break;
                }
                else
                {
                    // キューにプッシュして, 検索を継続する
                    Q.push(user_id);
                }
            }
        }

    }

    return ret;
}