Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#define N 100000
using namespace std;

int color[N];
vector<int> G[N];
queue<int> Q;
int n;

void bfs(int s,int c) {
	Q.push(s);
	color[s] = c;
	while (!Q.empty()) {
		for (unsigned int it = 0;it < G[Q.front()].size();it++)
			if (color[G[Q.front()][it]] == 0){
				Q.push(G[Q.front()][it]);
				color[G[Q.front()][it]] = c;
		    }
		Q.pop();
	 }
}

void Color() {
	int c = 0;
	for(int i=0;i<n;i++)
	if(color[i]==0) bfs(i, ++c);
}

int main() {
	int m, q;
	cin >> n >> m;
	for (int i = 0;i < m;i++) {
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
	}

	Color();

	cin >> q;
	for (int i = 0;i < q;i++) {
		int u, v;
		cin >> u >> v;
		if (color[u] == color[v] && color[u] != 0)
			cout << "yes" << endl;
		else
			cout << "no"<<endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<stack>
#include<queue>
#include<functional>
#include<iterator>
#include<cmath>

using namespace std;

typedef pair<int,int> P;


const int max_n = 1e5;

int par[max_n];
int rnk[max_n];

void init(int n){
	for(int i=0; i<n; i++){
		par[i] = i;
		rnk[i] = 0;
	}
}

int find_root(int x){
	if(par[x] == x) return x;
	else			return par[x] = find_root(par[x]);
}

void unite(int x, int y){
	x = find_root(x);
	y = find_root(y);
	if(x == y) return;

	if(rnk[x] < rnk[y]){
		par[x] = y;
	}
	else{
		par[y] = x;
		if(rnk[x] == rnk[y]) rnk[x]++;
	}
}

bool same(int x, int y){
	return find_root(x) == find_root(y);
}


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

	init(n);

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

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

	return 0;
}

C++: #include<stdio.h>
#include<queue>
 
#define MAX 100000
 
int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;
 
bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;
 
    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }
 
    return false;
}
 
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }
 
    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        //bfs(s, d) ? printf("yes\n") : printf("no\n");
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <string>
#include <climits>
using namespace std;

#define MAX_N  100000
#define MAX_M  100000
#define MAX_Q  10000

int n, m, q;
queue<int> Q;
int d[MAX_N] = {};

int bf(vector<vector<int> >& sns, int src, int dst)
{
    Q.push(src);
    while (!Q.empty()) {
        if (dst == (src = Q.front())) return 1;
        Q.pop();
        d[src]++;
        for (auto itr = sns.at(src).begin(); itr != sns.at(src).end(); itr++) {
            if (!d[*itr]) Q.push(*itr);
        }
    }
    return 0;
}

int main()
{
    cin >> n >> m;
    vector<vector<int> > sns(n);
    int s, d;
    while (m--) {
        cin >> s >> d;
        sns.at(s).push_back(d);
        sns.at(d).push_back(s);
    }
    cin >> q;
    while (q--) {
        cin >> s >> d;
        cout << (bf(sns, s, d) ? "yes" : "no") << endl;
    }
    return 0;
}

C++: 
#include<iostream>
#include<queue>

using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
	queue<int> q;

	while (table[child] != -1) {
		q.push(child);
		child = table[child];
	}
	while (!q.empty()) {
		table[q.front()] = child;
		q.pop();
	}
	return child;
}

int main() {
	int n, m, q;
	int table[MAX];

	cin >> n >> m;

	for (int i = 0; i < n; i++)table[i] = -1;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
	}

	cin >> q;

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

		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
 
int n, m, q;
int dp[100000];
vector<int> v[100000];
 
void solve1(int p, int idx) {
    if (dp[p]<0) {
        dp[p]=idx;
        for (int i=0;i<v[p].size();i++) {
            solve1(v[p][i],idx);
        }
    }
}
 
void syoki(){
    int idx=0;
    for(int i=0;i<n;i++) {
        if(dp[i]<0) {
            solve1(i,idx++);
        }
    }
}
 
 
int main(){
    int s, t;
    cin>>n>>m;
    for (int i=0;i<m;i++){
        cin>>s>>t;
        v[s].push_back(t);
        v[t].push_back(s);
    }
    cin>>q;
    fill(dp,dp+n,-1);
    syoki();
    for(int j=0;j<q;j++){
        cin>>s>>t;
        if(dp[s]==dp[t]){
            cout<<"yes"<<endl;
        }
        else{
            cout<<"no"<<endl;
        }
    }
 
    return 0;
}