Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; 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)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include <iostream>
#include <vector>
#include <stack>

//求解图的联通分量

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[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 (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;
    for (int i = 0; i < n; i++)
        color[i] = NIL;

    for (int u = 0; u < n; u++) {
        if (color[u] == NIL)
            dfs(u, id++);
    }
}

int main() {
    int s, t, 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);
    }

    assignColor();
    cin >> q;

    for (int i = 0; i < q; i++) {
        cin >> s >> t;
        if (color[s] == color[t])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include<stack>
using namespace std;
static const int MAX = 10000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[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(int i =0;i<G[u].size();i++){
			int v = G[u][i];
			if(color[v]==NIL){
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assign(){
	int id=1;
	for(int i=0;i<n;i++){
		color[i] = NIL;
	}
	for(int j=0;j<n;j++){
		if(color[NIL]==NIL){
			dfs(j,id++);
		}
	}
}

int main(){
	int s,t,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;
	for(int i=0;i<q;i++){
		cin>>s>>t;
		if(color[s]==color[t]){
			cout<<"yes"<<endl;
		}
		else cout<<"no"<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>

using namespace std;

const int WHITE=0, GRAY=1, BLACK=2;

struct DFS {
    int n;
    vector<vector<int>> g;
    vector<int> group;
    vector<int> state;
    int groupId = 0;
    
    DFS(int n):n(n) {
        g.assign(n, vector<int>());
        group.assign(n, -1);
        state.assign(n, WHITE);
    }
    
    void addEdge(int u, int v) {
        g[u].push_back(v);
    }
    
    void solve() {
        for (int i = 0; i < n; i++) {
            if (state[i] == WHITE) {
                groupId++;
                dfs(i);
            }
        }
    }
    
    void dfs(int u) {
        state[u] = GRAY;
        group[u] = groupId;
        for (int v : g[u]) {
            if (state[v] == WHITE) {
                dfs(v);
            } 
        }
        state[u] = BLACK;
    }
    
    void print() {
        for (int i = 0; i < n; i++) {
            cout << i << ":";
            for (int v : g[i]) {
                cout << v << " ";
            }
            cout << endl;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    int n, e;
    cin >> n >> e;
    
    DFS dfs(n);
    
    for (int i = 0; i < e; i++) {
       int u, v;
       cin >> u >> v;
       dfs.addEdge(u, v);
       dfs.addEdge(v, u);
    }
   
    dfs.solve();
    
    int q;
    cin >> q;
    
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        if (dfs.group[u] == dfs.group[v]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#define MAX 100000
using namespace std;
int n,m;
vector<vector<int> > list(MAX);
int grp[MAX];
int group;

void dfs(int now){
	stack<int> next;
	grp[now]=group;	next.push(now);
	while(!next.empty()){
		now=next.top();	next.pop();
		for(int i=0;i<(int)list[now].size();i++){
			if(grp[list[now][i]]==-1){
				grp[list[now][i]]=group;
				next.push(list[now][i]);
			}
		}
	}
}

int main(){
	cin>>n>>m;
	int s,t;
	for(int i=0;i<m;i++){
		cin>>s>>t;
		list[s].push_back(t);
		list[t].push_back(s);
	}
	for(int i=0;i<n;i++)	grp[i]=-1;
	group=1;
	for(int i=0;i<n;i++){
		if(grp[i]==-1){
			dfs(i);
			group++;
		}
	}
	int q;	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s>>t;
		if(grp[s]==grp[t])	cout<<"yes"<<endl;
		else 	cout<<"no"<<endl;
	}
	return 0;
}

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

static const int MAX =100000;
static const int NIL=-1;

int n;
vector<int> G[MAX];
int color[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(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;
  for(int i=0;i<n;i++) color[i]=NIL;
  for(int u=0;u<n;u++){
    if(color[u]==NIL)dfs(u,id++);
  }
}

int main (){
  int s,t,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);
  }

  assignColor();
  cin >>q;
  for(int i=0;i<q;i++){
    cin >> s >> t;
    if(color[s]==color[t]){
      cout <<"yes"<< endl;
    }else{
      cout << "no" <<endl;
    }
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[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 (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;
  for (int i = 0; i < n; i++)
  {
    color[i] = NIL;
  }
  for (int u = 0; u < n; u++)
  {
    if (color[u] == NIL)
    {
      dfs(u, id++);
    }
  }
}

int main()
{
  int s, t, 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);
  }

  assignColor();

  cin >> q;

  for (int i = 0; i < q; i++)
  {
    cin >> s >> t;
    if (color[s] == color[t])
    {
      cout << "yes" << endl;
    }
    else
    {
      cout << "no" << endl;
    }
  }

  return 0;
}