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 <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;
class UnionFind {
public:
	vector<int> Parent;
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}
	int root(int A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	int size(int A) {
		return -Parent[root(A)];
	}

	bool connect(int A, int B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};
int main()
{
	int n, m, q;
	cin >> n >> m;
	UnionFind uni(n);
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		uni.connect(s, t);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (uni.root(s) == uni.root(t)) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <iterator>
using namespace std;

int n, m;
vector<int> *v;

int bfs(int s, int t) {
    vector<int> *vc = new vector<int>[n];
    for (int i = 0; i < n; i++) {
        vc[i] = v[i];
    }

    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; i++) {
        int u = q.front();
        q.pop();
        for (vector<int>::iterator it = vc[i].begin(); it != vc[i].end(); it++) {
            if (*it == t) {
                return 1;
            }
            q.push(*it);
            vector<int>::iterator result = find(vc[*it].begin(), vc[*it].end(), i);
            if (result != vc[*it].end()) { vc[*it].erase(result); }
        }
    }
    return 0;
}

int main() {
    scanf("%d %d", &n, &m);
    v = new vector<int>[n];

    for (int i = 0; i < m; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        v[s].push_back(t);
        v[t].push_back(s);
    }

    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        printf("%s\n", bfs(s, t) ? "yes" : "no");
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define N 100005
using namespace std;
vector<int> e[N];
int n,m,q,s,t,cnt;
int used[N];

void dfs(int x){
  if(used[x])return;
  used[x]=cnt;
  for(int i=0;i<e[x].size();i++)
    dfs(e[x][i]);
}

int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    e[s].push_back(t);
    e[t].push_back(s);
  }
  cnt=1;
  for(int i=0;i<n;i++){
    if(!used[i])dfs(i),cnt++;
  }
  cin>>q;
  while(q--){
    cin>>s>>t;
    if(used[s]==used[t])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

struct Info{
	Info(){s = t = -1;}
	Info(int tmp_s,int tmp_t){
		s = tmp_s;
		t = tmp_t;
	}
	int s;
	int t;
};

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		global = new Info[m];
		each = new int*[n];
		each_index = new int[n];
		num_of_each = new int[n];
		group = new int[n];
		for(int i=0;i<n;i++){
			each_index[i] = 0;
			num_of_each[i] = 0;
			group[i] = 0;
		}
		global_index = 0;
		group_number = 1;
	}

	void push(int s,int t){
		global[global_index++] = Info(s,t);
		num_of_each[s]++;
		num_of_each[t]++;
	}

	void allocate(){
		for(int i=0;i<n;i++){
			if(num_of_each[i] > 0){
				each[i] = new int[num_of_each[i]];
			}
		}
		Info tmp;
		for(int i=0;i<m;i++){
			tmp = global[i];
			each[tmp.s][each_index[tmp.s]++] = tmp.t;
			each[tmp.t][each_index[tmp.t]++] = tmp.s;
		}
	}

	void makeGroup(){
		int node,i;
		queue<int> Q;
		Q.push(0);
		group[0] = 1;
		while(true){
			while(!Q.empty()){
				node = Q.front();
				Q.pop();
				if(num_of_each[node] > 0){
					for(int k=0; k < num_of_each[node];k++){
						if(group[each[node][k]] == 0){
							group[each[node][k]] = group[node];
							Q.push(each[node][k]);
						}
					}
				}
			}
			group_number++;

			for(i=0;i<n;i++){
				if(group[i] == 0){
					group[i] = group_number;
					Q.push(i);
					break;
				}
			}
			if(i == n) break;
		}
	}

	bool canAccsess(int s,int t){
		return group[s] == group[t];
	}

private:
	Info* global;
	int** each;
	int* each_index;
	int* num_of_each;
	int* group;
	int group_number;
	int global_index,n,m;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.allocate();
	relation.makeGroup();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<set>
using namespace std;

bool DFS(vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);

    int idx = 0;
    do {
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<vector<int> > G(n);
// bool D[100000][100000];

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}