Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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