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 <stack>
#include <vector>
using namespace std;

int n;
vector<int> g[100001];
char c[100001];
int visit[100001];
int finish[100001];
int group[100001];
int m;
int main() {

    cin >> n >> m;
    for (int i1 = 1; i1 <=m; ++i1) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

   stack<int> s;
    for (int l = 0; l < n; ++l) {
        c[l] = 'w';

    }
    int time = 0;
    int start = -1;
    int v;
    int nowg = 0;
    while(true) {
        nowg++;
        start++;
        if(start == n) break;
        if(c[start] == 'w') {
            time++;
            s.push(start);
            c[start] = 'g';
            group[start] = nowg;
            visit[start] = time;
            while (s.size() != 0) {
                time++;
                v = s.top();
                int next = -1;
                for (int i = 0; i < g[v].size() ; ++i) {
                    if(c[g[v].at(i)] == 'w') {
                        next = g[v].at(i);
                        break;
                    }
                }
                if(next == -1) {
                    c[v] = 'b';
                    finish[v] = time;
                    s.pop();
                } else {
                    c[next] = 'g';
                    group[next] = nowg;
                    visit[next] = time;
                    s.push(next);
                }
            }
        }
    }

    int q;
    cin >> q;
    for (int m = 0; m < q; ++m) {
        int a, b;
        cin >> a >> b;
        if(group[a] == group[b]) cout << "yes";
        else cout << "no";
        cout << endl; 


    }



    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

class Graph
{
public:
    Graph() {}
    Graph(int n)
    : con(vs32(n, -1))
    {}
    ~Graph() {}

    bool path(int u, int v)
    {
        bool ret;

        int repu = getRep(u);
        int repv = getRep(v);
        if (repu != repv) ret = false;
        else /*if (repu == repv)*/ ret = true;

        return ret;
    }

    void setPath(int u, int v)
    {
        int repu = getRep(u);
        int repv = getRep(v);


        if (repu == repv) return;
        if (con[repu] > con[repv])
        {
            swap(repu, repv);
        }
        con[repu] += con[repv];
        con[repv] = repu;
    }

    int getRep(int u)
    {
        int ret;
        if (con[u] >= 0)
        {
            con[u] = getRep(con[u]);
            ret = con[u];
        }
        else
        {
            ret = u;
        }
        return ret;
    }

    vs32 con;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    Graph g(n);
    int m;
    cin >> m;
    int u, v;
    rep (i, m)
    {
        cin >> u >> v;
        g.setPath(u, v);
    }

    int q;
    cin >> q;
    rep (i, q)
    {
        cin >> u >> v;
        if (g.path(u, v))
        {
            cout << "yes\n";
        }
        else
        {
            cout << "no\n";
        }
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q; int COLOR[MAX_N], N, M, Q1, A, B;
pair<int, int> R[MAX_N]; vector<pair<int, int>>S[MAX_N]; int T[MAX_N];
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); }
	for (int i = 0; i < Q1; i++) {
		cin >> R[i].first >> R[i].second; S[R[i].first].push_back(make_pair(R[i].second, i));
		S[R[i].second].push_back(make_pair(R[i].first, i));
	}
	for (int i = 0; i < N; i++) {
		Q.push(i); for (int j = 0; j < N; j++) { COLOR[j] = 0; }
		while (!Q.empty()) {
			int B = Q.front(); Q.pop();
			for (int j = 0; j < X[B].size(); j++) {
				if (COLOR[X[B][j]] == 0) { COLOR[X[B][j]] = 1; Q.push(X[B][j]); }
			}
		}
		for (int j = 0; j < S[i].size(); j++) {
			int a1 = S[i][j].first, a2 = S[i][j].second;
			if (COLOR[a1] == 1) { T[a2]++; }
		}
	}
	for (int i = 0; i < Q1; i++) {
		if (T[i] == 2) { cout << '1' << endl; }
		else { cout << '0' << endl; }
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
#define MAX 100000
#define NIL -1

int n;
vector<int> G[MAX];
int color[MAX];


void dfs(int r, int c) {
  stack<int> S;
  int i, v, u;
  S.push(r);
  color[r]=c;
  while (! S.empty()) {
    u=S.top();
    S.pop();
    for (i=0; i<G[u].size(); i++) {
        v=G[u][i];
      if (color[v]==NIL) {
        color[v]=c;
        S.push(v);
      }
    }
  }
}



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

int main() {

  int  m, i, j, v, s, t, q;

  cin >> n >> m;

  for (i=0; i<m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (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>
using namespace std;

#define MAX 100000
#define WHITE -1

int n, m, q;
vector<int> G[MAX];
int col[MAX];

void dfs(int s, int c) {
	col[s] = c;
	for (int v = 0; v < G[s].size(); v++) {
		if (col[G[s][v]] == WHITE)
			dfs(G[s][v], c);
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		col[i] = WHITE;
	}
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	int id = 0;
	for (int i = 0; i < n; i++) {
		if (col[i] == WHITE) {
			dfs(i, ++id);
		}
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		cout << (col[s] == col[t] ? "yes" : "no") << endl;
	}
	return 0;
}