Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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;
}

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

//求解图的联通分量

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
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[s].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;
}