Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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;
}

C++: #include<iostream>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX =  100000;
int i, j, v, num, color[MAX];
vector<int> list[MAX];
 
void dfs(int r, int c){
 stack<int> S;
 S.push(r);
 color[r] = c;
 while( !S.empty() ){
   int u = S.top(); S.pop();
   for(i=0; i<list[u].size(); i++){
     v = list[u][i];
     if(color[v] == -1){
       color[v] = c;
       S.push(v);
     }
   }
 }
}
  
void assigncolor(){
 int id = 1;
 for(i=0; i<num; i++){
   color[i] = -1;
 }
 for(j=0; j<num; j++){
   if(color[j] == -1)
     dfs(j, id++);
 }
}
  
int main(){
 int a, b, trial, start, goal, kaisuu;
 cin >> num >>kaisuu;
  
 for(i=0; i<kaisuu; i++){
   cin >> a >> b;
   list[a].push_back(b);
   list[b].push_back(a);
 }
 
 assigncolor();
 
 cin >> trial;
 
 for(i=1; i<=trial; i++){
   cin >> start >> goal;
   if(color[start] == color[goal]){  
     cout << "yes" << endl;
   } else{
     cout << "no" << endl;
   }
 }
  
 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;
}