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 <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int id;
    vector<int> children{};
    int visited = 0;
    
    void resetVisited(){
        this->visited = 0;
    }
};
 
deque<int> schedule; 
vector<status> graph;

bool bfs(int target_v){
    if (schedule.empty()){
        return false;
    }
    
    int now_v = schedule.front();
    schedule.pop_front();
    
    for(auto& next_v:graph[now_v].children){
        if(graph[next_v].visited) continue;

        if(next_v==target_v) return true;
        graph[next_v].visited = 1;
        schedule.push_back(next_v);
    }
    return bfs(target_v);
}
 

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //setup
    int nb_v; cin >> nb_v;
    int nb_e; cin >> nb_e;
    graph.resize(nb_v);
    for(int i=0; i<nb_e; ++i){
        int v1, v2; cin >> v1 >> v2;
        graph[v1].children.push_back(v2);
        graph[v2].children.push_back(v1);
    }
    for(auto& a:graph){
        sort(a.children.begin(),a.children.end());
    }
    
    //bfs
    int times; cin>>times;
    for(int i=0; i<times; ++i){
        int from_v; cin>>from_v;
        int to_v; cin>>to_v;
        for(auto& a:graph) a.resetVisited();
        schedule = {};
        schedule.push_back(from_v);
        cout<<(bfs(to_v)? "yes\n":"no\n");
    }
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent = -1;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
        }
        else
            person[t].parent = person[s].parent;
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
const ll INF = 1LL<<60;
const int INT_INF = 1e9;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;

struct UnionFind {
    vector<int> parent;

    // Constructor
    UnionFind(int n) {
        parent.resize(n);
        rep(i, n) parent[i] = i;
    }

    // 親を取得
    int root(int x) {
        if (parent[x] == x) {
            return x;
        } else {
            parent[x] = root(parent[x]);
            return parent[x];
        }
    }

    // 二つのデータを同じ木にくっつける
    void connect(int x, int y) {
        int rx = root(x);
        int ry = root(y);

        if (rx == ry) return; // 元から同じ木に属する場合は何もしない
        parent[rx] = ry;
    }

    // 二つのデータが同じ木に属するか判定
    bool check(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main() {
    int n, m; cin >> n >> m;
    UnionFind tree(n);
    rep(i, m) {
        int s, t; cin >> s >> t;
        tree.connect(s, t);
    }
    int q; cin >> q;
    rep(i, q) {
        int s, t; cin >> s >> t;
        cout << (tree.check(s, t) ? "yes" : "no") << endl;
    }
}

C++: #include <cstdio>
#include <vector>
#include <cstring>

bool isFriend(std::vector<int> *a, int s, int t, int *checked, int c){
    int tmp;
    checked[s] = c;
    if(s==t)return true;
    for(int i=0;i<a[s].size();i++){
        tmp = a[s][i];
        if(checked[tmp]>=c)continue;
        if(isFriend(a,tmp,t,checked,c))return true;
    }
    return false;
}

int main(){
    int i, n, m, q, s, t;
    int *checked;
    scanf("%d %d",&n,&m);
    checked = (int *)malloc(sizeof(int)*n);
    memset(checked, 0, sizeof(int)*n);
    std::vector<int> a[n];
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(a,s,t,checked,i+1))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}