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();

  sin>>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 <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

vector<int> g[100005];
int color[100005];
const int INF = 1001001001;
int nc = 0;

void dfs(int x, int b=-1){
    color[x] = nc;
    if(g[x].empty())return;
    for (int i = 0; i < g[x].size(); i++){
        int v = g[x][i];
        if(color[v] != INF)continue;
        color[v] = nc;
        dfs(v, x);
    }
    return;
}

int main() {
    int n, m; cin >> n >> m;
    rep(i, m){
        int s, t; cin >> s >> t;
        g[s].push_back(t);
        g[t].push_back(s);
    }

    rep(i, n){
        color[i] = INF;
    }

    rep(i, n){
        if(color[i] == INF){
            dfs(i);
            nc++;
        }
    }
    
    int q; cin >> q;
    rep(i, q){
        int s, t; cin >> s >> t;
        if(color[s] == color[t]){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    
    return 0;
}

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

vector< int > connections;

void addConnection(int a, int b) {
  if (connections[a] == a && connections[b] == b)
    a < b ? connections[b] = a : connections[a] = b;
  else if (connections[a] == a)
    addConnection(a, connections[b]);
  else if (connections[b] == b)
    addConnection(connections[a], b);
  else
    addConnection(connections[a], connections[b]);
}

bool checkConnection(int a, int b) {
  if (connections[a] == a && connections[b] == b) {
    if (a == b)
      return true;
    else
      return false;
  } else if (connections[a] == a) {
    return checkConnection(a, connections[b]);
  } else if (connections[b] == b) {
    return checkConnection(connections[a], b);
  } else
    return checkConnection(connections[a], connections[b]);
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  connections = vector< int > (n, -1);
  for (int i = 0; i < n; i++)
    connections[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    addConnection(a, b);
  }
  int q;
  cin >> q;
  // for (int i = 0; i < n; i++)
  //   cout << connections[i] << ' ';
  // cout << endl;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    // cout << a << ' ' << b << endl;
    if (checkConnection(a, b))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxx=100100;
vector<int> A[maxx];
int color[maxx];
int n,c=1;

void dfs(int u){
	if(color[u]!=-1) return ;//当该节点已经属于某一联通分量时，不需要染色
	color[u]=c;
	int si=A[u].size();
	for(int i=0;i<si;i++){
		if(color[A[u][i]]==-1){//注意是对A[u][i]进行染色
			dfs(A[u][i]);
		}
	}
	return ;
}

int main(){
	int q,a,b;//这里有个问题，n如果放在global里定义的话会出现问题，只能在main里定义，不知道为什么
	cin>>n>>q;
//	
//	cout<<&n<<endl<<&color<<endl<<&color[109]<<endl;
//	cout<<n<<endl;
	for(int i=0;i<=maxx;i++) color[i]=-1;
//	cout<<n<<endl;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	for(int i=0;i<n;i++){
		dfs(i);
		c++;
	}
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		if(color[a]==color[b]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

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

#define null -1

void DFS(vector<int> rVal[], map<int, int> &uColor_m, int idOrSs) {
	stack<int> stack;
	stack.push(idOrSs);
	uColor_m[idOrSs] = idOrSs;

	while (!stack.empty()) {
		int curUVal = stack.top();
		stack.pop();
		for (int i = 0; i < (signed)rVal[curUVal].size(); i++) {
			int curV = rVal[curUVal][i];
			if (uColor_m[curV] != null) continue;
			uColor_m[curV] = idOrSs;
			stack.push(curV);
		}
	}
}

void assignColor(int uNum, vector<int> rVal[], map<int, int> &uColor_m) {
	for (int i = 0; i < uNum; i++)
		uColor_m[i] = null;
	for (int i = 0; i < uNum; i++) {
		if (uColor_m[i] == null) DFS(rVal, uColor_m, i);
	}
}

int main() {
	int userNum;
	int relationNum;
//	int uColor_map[100000]; //<?????????, ??°??????????????????>
	map<int, int> uColor_m;
	//??????????????¨????????????
	vector<int> relationVal[100000];
	//int userColor[100000];

	cin >> userNum >> relationNum;
	for (int i = 0; i < relationNum; i++) {
		int userVal1, userVal2;
		cin >> userVal1 >> userVal2;
		relationVal[userVal1].push_back(userVal2);
		relationVal[userVal2].push_back(userVal1);
		uColor_m[i] = null;
	}

	assignColor(userNum, relationVal, uColor_m);

	int qNum;
	cin >> qNum;
	for (int i = 0; i < qNum; i++) {
		int uV1, uV2;
		cin >> uV1 >> uV2;
		if (uColor_m[uV1] == uColor_m[uV2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}