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 <stack>

using namespace std;

struct uft {
    uft (int s) : ps {new int[s]}, ds {new int[s]}, s {s} {
        for(int i = 0; i < s; ++i){
            ps[i] = i;
            ds[i] = 0;
        }
    };
    void unite(int x, int y){
        int px = parent(x);
        int py = parent(y);
        if(ds[px] < ds[py]){
            ps[px] = py;
        } else if(ds[px] > ds[py]){
            ps[py] = px;
        } else {
            ps[px] = py;
            ++ds[py];
        }
    };
    int parent(int p){
        return p == ps[p] ? p : ps[p] = parent(ps[p]);
    };
    bool same(int x, int y){
        return parent(x) == parent(y);
    };
    int* ps;
    int* ds;
    int s;
};

struct user {
    user() {};
    user(int i) : id {i}, fs {}, group {-1} {};
    int id;
    vector<int> fs;
    int group;
};

void add_friends(user *us, int x, int y){
    us[x].fs.push_back(y);
    us[y].fs.push_back(x);
}

void dfs(user* us, int id, int g){
    stack<int> ss {};
    ss.push(id);
    while(ss.size()){
        int p = ss.top();
        ss.pop();
        us[p].group = g;
        vector<int> fs = us[p].fs;
        for(auto x : fs){
            if(us[x].group == g){
                continue;
            }
            ss.push(x);
        }
    }
}

void make_group(user* us, int n){
    int g = 0;
    for(int i = 0; i < n; ++i){
        if(us[i].group < 0){
            dfs(us, i, g);
            ++g;
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    //user us[n];
    auto us = uft(n);
    /*for(int i = 0; i < n; ++i){
        us[i] = user(i);
    }*/
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        us.unite(a, b);
    //    add_friends(us, a, b);
    }
    //make_group(us, n);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++ i){
        int a, b;
        cin >> a >> b;
        //if(us[a].group == us[b].group){
        if(us.same(a, b)){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=1<<21;
const int maxn=100+10;
int n,m,q;
int p[maxn];
int rank[maxn];
void init(int n)
{
   for(int i=0;i<n;i++)
   {
       p[i]=i;
       rank[i]=0;
   }
}
int fnd(int x)
{
    if(x==p[x]) return x;
    return fnd(p[x]);
}
bool is_same(int x,int y)
{
   return fnd(x)==fnd(y);
}

void uni(int x,int y)
{
   x=fnd(x);
   y=fnd(y);
   if(rank[x]>rank[y])
   {
       p[y]=x;
   }
   else
   {
       p[x]=y;
       if(rank[x]==rank[y]) rank[y]++;
   }
}

int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int x,y;
    init(n);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        uni(x,y);
    }
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&x,&y);
        if(is_same(x,y)) printf("yes\n");
        else printf("no\n");
    }


    return 0;
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Node{
private:
	int id;
	int friend_num;
	Node **friends;
	int check_id;
public:
	Node(int id, int n);
	~Node(){ delete[] friends; }
	void addFriend(Node *friend_node);
	bool searchFriend(int id);
};

Node::Node(int id, int n)
{
	this->id = id;
	friend_num = 0;
	friends = new Node*[n];
	check_id = -1;
}

void Node::addFriend(Node *friend_node)
{
	friends[friend_num] = friend_node;
	friend_num++;
}

bool Node::searchFriend(int id)
{
	if (check_id == id) return false;
	check_id = id;

	if (this->id == id) return true;

	for (int i = 0; i < friend_num; i++) {
		if (friends[i]->searchFriend(id)) return true;
	}
	return false;
}

int main(void)
{
	int n;
	cin >> n;	
	Node **node = new Node*[n];
	for (int i = 0; i < n; i++) node[i] = new Node(i, n);

	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		node[s]->addFriend(node[t]);
		node[t]->addFriend(node[s]);
	}
		
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		bool result = node[s]->searchFriend(t);
		if (result) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	delete[] node;
	return 0;
}

C++: #include<stack>
#include<queue>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;
int color[100000];

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			//_getch();
			//cout << G[s][i] << endl;
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else if (color[i] == 0){
				Q.push(G[s][i]);
				color[i] = 1;
			}
		}
		if (!Q.empty())s = Q.front();
	}
}

int main(){
	int n, 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);
	}
	cin >> q; fill(color, color + 100000, 0);
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
		fill(color, color + 100000, 0);
	}

	return 0;
}