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 <vector>
#include <map>
#include <stack>
using namespace std;

#define null -1

void DFS(vector<int> rVal[], int uColor_m[], int idOrSs) {
	stack<int> stack;
	stack.push(idOrSs);
	uColor_m[idOrSs] = idOrSs;

	while (!stack.empty()) {
		int curUVal = stack.top();
		stack.pop();
		for (int i = 0; i < (signed)rVal[curUVal].size(); i++) {
			int curV = rVal[curUVal][i];
			if (uColor_m[curV] != null) continue;
			uColor_m[curV] = idOrSs;
			stack.push(curV);
		}
	}
}

void assignColor(int uNum, vector<int> rVal[], int uColor_m[]) {
	for (int i = 0; i < uNum; i++)
		uColor_m[i] = null;
	for (int i = 0; i < uNum; i++) {
		if (uColor_m[i] == null) DFS(rVal, uColor_m, i);
	}
}

int main() {
	int userNum;
	int relationNum;
	int uColor_map[100000]; //<?????????, ??°??????????????????>
	//??????????????¨????????????
	vector<int> relationVal[100000];
	//int userColor[100000];

	cin >> userNum >> relationNum;
	for (int i = 0; i < relationNum; i++) {
		int userVal1, userVal2;
		cin >> userVal1 >> userVal2;
		relationVal[userVal1].push_back(userVal2);
		relationVal[userVal2].push_back(userVal1);
		uColor_map[i] = null;
	}

	assignColor(userNum, relationVal, uColor_map);

	int qNum;
	cin >> qNum;
	for (int i = 0; i < qNum; i++) {
		int uV1, uV2;
		cin >> uV1 >> uV2;
		if (uColor_map[uV1] == uColor_map[uV2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

void dfs(int s, int id, vector<vector<int>> a, vector<int> &comp) {
	stack<int> st;
	st.push(s);
	comp[s] = id;
	while (!st.empty()) {
		int i = st.top();
		int k = a[i].size();
		REP(j, k + 1) {
			if (j < k) {
				int t = a[i][j];
				if (comp[t] != id) {
					comp[t] = id;
					st.push(t);
					break;
				}
			}
			else st.pop();
		}
	}
}

void divide(int size, vector<vector<int>> a, vector<int> &comp) {
	int s = 0;
	int id = 1;
	while (1) {
		dfs(s, id, a, comp);
		while (s < size && comp[s]>0) s++;
		if (s == size) break;
		id++;
	}
	return;
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	int q;
	cin >> q;
	vector<int> comp(n, 0);
	divide(n, a, comp);
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		if (comp[s] == comp[t]) printf("yes\n");
		else printf("no\n");
	}

	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxx=100100;
vector<int> A[maxx];
int color[maxx];
int c=1;

void dfs(int u){
	if(color[u]!=-1) return ;
	color[u]=c;
	int si=A[u].size();
	for(int i=0;i<si;i++){
		if(color[A[u][i]]==-1){
			dfs(A[u][i]);
		}
	}
	return ;
}

int main(){
	int n,q,a,b;// why i should put the dim n in main but not global
	cin>>n>>q;
	for(int i=0;i<=maxx;i++) color[i]=-1;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	for(int i=0;i<n;i++){
		dfs(i);
		c++;

	}
//	for(int i=0;i<n;i++){
//		cout<<i<<" "<<color[i]<<endl;
//	}
	
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		if(color[a]==color[b]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1 << 21;
const ll MOD = 1e9 + 7;

static const int N = 1e5;
static const int NIL = -1;

int n, m, q;
vector<int> G[N];
int color[N];

void dfs(int r, int c)
{
    stack<int> S;
    S.push(r);
    color[r] = c;
    while(!S.empty()) {
        int u = S.top();
        S.pop();
        rep(i, G[u].size()) {
            int v = G[u][i];
            if (color[v] == NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assigncolor()
{
    int id = 1; // 同じidがcolorに格納されているときは同じ連結成分にある
    // 初期化
    rep(i, n) {
        color[i] = NIL;
    }
    rep(u, n) {
        if (color[u] == NIL) { // 初期化されていた場合
            dfs(u, id++);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    rep(i, m)
    {
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assigncolor();

    cin >> q;
    rep(i, q)
    {
        int s, t;
        cin >> s >> t;
        if (color[s] == color[t]) {
            puts("yes");
        } else {
            puts("no");
        }
    }
    return 0;
}