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 <queue>
static const int MAX=100000;
using namespace std;
vector<int> follow[MAX];
int color[MAX];  //-1 represent No Visitedvoid 
bfs(int i,int c){    queue<int> Q;    Q.push(i);color[i]=c;    
while(!Q.empty())  
  {        int p=Q.front();Q.pop();        for(int j=0;j<follow[p].size();j++)        {            if(color[follow[p][j]]!=-1) continue;            //else            Q.push(follow[p][j]); color[follow[p][j]]=color[p];        }    }}
int main(){   int n,m;   cin>>n>>m;   for(int i=0;i<n;i++) color[i]=-1;   for(int i=0;i<m;i++)   {       int a,b;       cin>>a>>b;       follow[a].push_back(b);       follow[b].push_back(a);   }   int c=0;   for(int i=0;i<n;i++)   {       if(color[i]==-1)       {           bfs(i, c);           c++;       }   }   int order_num;cin>>order_num;   for(int i=0;i< order_num;i++)   {       int a,b;       cin>>a>>b;       if(color[a]==color[b]) cout<<"yes"<<endl;       else cout<<"no"<<endl;   }}

C++: #include <stdio.h>
#include <vector>
#include <stack>
#define MAX 100010
#define INFTY 10000000
#define NIL -1

using namespace std;

int N;
int M;
vector<int> V[MAX];
int color[MAX];

int dfs(int s, int t)
{
	stack<int> S;
	S.push(s);
	color[s] = t;
	
	while (!S.empty()) {
		int u = S.top(); S.pop();
		for (int i=0; i<V[u].size(); i++) {
			int v = V[u][i];
			if (color[v] == NIL) {
				color[v] = t;
				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 i, n, u, v;	
	scanf("%d %d", &N, &M);	
	for (i = 0; i < M; i++) {
		scanf("%d %d", &n, &v);
		V[n].push_back(v);
		V[v].push_back(n);
	}
	
	assignColor();
	
	int Q;
	scanf("%d", &Q);
	for (i = 0; i < Q; i++) {
		scanf("%d %d", &u, &v);
		/*
		if (dfs(u, v) == 1) {
			puts("yes");
		}
		else {
			puts("no");
		}*/
		if (color[u] == color[v]) {
			puts("yes");			
		}
		else {
			puts("no");
		}
	}

	return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

class UnionFind
{
    vector<int> par;

public:
    UnionFind(int n) : par(n, -1) {}

    int root(int x)
    {
        if (par[x] < 0)
            return x;
        else
            return par[x] = root(par[x]);
    }

    bool merge(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x == y)
            return false;
        if (par[x] > par[y])
            swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) { return -par[root(x)]; }
};

void solve()
{
    int n, m, q;
    cin >> n >> m;
    UnionFind uf(n);
    while (m--)
    {
        int s, t;
        cin >> s >> t;
        uf.merge(s, t);
    }
    cin >> q;
    while (q--)
    {
        int s, t;
        cin >> s >> t;
        if (uf.root(s) == uf.root(t))
        {
            cout << "yes\n";
        }
        else
        {
            cout << "no\n";
        }
    }
}

int main()
{
    solve();
    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, vector<vector<int>> v, vector<int> &used) {
	used[s] = 1;
	int n = v.size();
	int k = v[s].size();
	REP(l, k) {
		int j = v[s][l];
		if (!used[j]) {
			dfs(j, v, used);
		}
	}
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> v(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	int q;
	cin >> q;
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		vector<int> used(n, 0);
		dfs(s, v, used);
		if (used[t]) cout << "yes\n";
		else cout << "no\n";
	}
	

	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include<math.h>
#include<queue>
#pragma warning(disable:4996)
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 s, t, m, q;
	cin >> n >> m;

	for(int i=0; i<m; i++){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
}

int main(){
	int s, t, m, q;
	cin >> n >> m;

	for(int i=0; i<n; i++){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	assignColor();
	cin >> q;

	for(int i=0; i<q; i++){
		cin >> s >> t;
		if( color[s]==color[t] ){
			cout << "yes" << "\n";
		}else{
			cout << "no" << "\n";
		}
	}
	return 0;
}