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<cstdio>
#include<cstdlib>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
using namespace std;

int n, m;
int d[100000];
int t = 0;
int c[100000];
queue<int> q;
vector<int> v[100000];
int ans = 2;

void bfs(int u){
  q.push(u);
  c[u] = 1;

  while(q.empty() == 0){
    u = q.front();
    q.pop();
    for(int i = 0; i < v[u].size(); i++){
      if(c[v[u][i]] == 0){
        q.push(v[u][i]);
        c[v[u][i]] = 1;
      }

    }
    c[u] = ans;
  }

}


int main(){

  cin >> n;

  for(int i = 0; i < n; i++){
    c[i] = 0;
  }

  cin >> m;

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  for(int i = 0; i < n; i++){
    if(c[i] == 0){
      bfs(i);
      ans++;
    }
  }

  int q;
  cin >> q;

  for(int i = 0; i < q; i++){
    int x, y;
    cin >> x >> y;
    if(c[x] == c[y]) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#define N 100000
#define white 0
using namespace std;

int color[N], n, S[N];
vector<int> c[N];



void tansaku_2(int a, int b) {
    int i, t, co, u;
    S[0] = a;
    t = 0;
    co = 1;
    while (1) {
        u = S[t];
        for (i = 0; i < c[u].size(); ++i) {
            if (color[c[u][i]] == white){
                color[c[u][i]] = b;
                S[co++] = c[u][i];
            }
        }
        S[t++] = 0;
        if (t == co) break;
    }
}

void tansaku() {
    int i, d;
    d = 1;
    
    for (i = 0; i < n; ++i) {
        if (color[i] == white) {
            color[i] = d;
            tansaku_2(i , d++);
        }
    }
}

int main() {
    int m, q, i, a, b;
    
    cin >> n >> m;
    for (i = 0; i < n; ++i) {
        color[i] = white;
    }
    for (i = 0; i < m; ++i) {
        scanf("%d %d", &a, &b);
        c[a].push_back(b);
        c[b].push_back(a);
    }
    tansaku();
    
    cin >> q;
    
    for (i = 0; i < q; ++i) {
        scanf("%d %d", &a, &b);
        if (color[a] == color[b]) {
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }
    return 0;
}

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

#define MAX 100000

vector<int> G[MAX];
int cond[MAX], n;
stack<int> S;

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

void check(int x, int y)
{
	for (int i = 0; i < (int)G[x].size(); i++) {
		if (G[x][i] == y) {
			cout << "yes" << endl;
			return;
		}
	}
	for (int i = 0; i < (int)G[y].size(); i++) {
		if (G[x][i] == y) {
			cout << "yes" << endl;
			return;
		}
	}
	cout << "no" << endl;
}

void dfs(int x, int id)
{
	S.push(x);
	cond[x] = id;

	int i, v;
	while (!S.empty()) {
		int u = S.top();
		S.pop();
		for (i = 0; i < (int)G[u].size(); i++) {
			v = G[u][i];			
			if (cond[v] == 0) {				
				cond[v] = id;
				S.push(v);
			}
		}
	}
}

void searchFriends()
{
	int id = 1;
	for (int i = 0; i < n; i++)
		if (cond[i] == 0) dfs(i, id++);
}

int main()
{
	int m, q, u, v;
	cin >> n >> m;

	set(m);
	searchFriends();

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

C++: #include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define maxn 100005
#define NIL -1

int n,m;
vector<int> G[maxn]; //邻接表
int color[maxn]; //编号（可以互相链接的两个节点编号相同）

void dfs(int r,int c) //给节点r分配编号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 dfs_(int r,int c) //给节点r分配编号c（深搜实现）（递归）
{
    color[r]=c;
    for(int i=0;i<G[r].size();i++) //注意这里代码于上面的不同
    {
        int v=G[r][i];
        if(color[v]==NIL)
            {
                color[v]=c;
                dfs_(v,c);
            }
    }
}

void bfs(int r,int c) //给节点r分配编号c（广搜实现）
{
    queue<int> S;
    S.push(r);
    color[r]=c;
    while(!S.empty())
    {
        int u=S.front();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 id=1;
    memset(color,NIL,sizeof(color)); //这里要记得初始化
    for(int u=0;u<n;u++)
        if(color[u]==NIL)
            dfs_(u,id++); //这里选取了广搜的方式给各个节点编号
}

int main()
{
    int s,t,q;
    cin>>n>>m;
    while(m--)
    {
        cin>>s>>t;
        G[s].push_back(t); //从顶点s向t画边
        G[t].push_back(s); //反过来连边
    }
    assigncolor();
    cin>>q;
    while(q--)
    {
        cin>>s>>t;
        if(color[s]==color[t]) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}