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 <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int NIL=-1;

int n, color[10000];
vector<int> G[100000];


void dfs(int v, int id){
    color[v]=id;

    for(int i=0; i<G[v].size(); i++){
        if(color[G[v][i]]==NIL) dfs(G[v][i],id);
    }
}

void assignColor(){
    int id=1;

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

int main(void){
    int m; cin >> n >> m;

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

    assignColor();

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

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main(){
	int n,m;
	cin>>n>>m;
	vector<int> d(n);
	vector<vector<int>> g(n);
	int a,b;
	rep(i,m){
		//cin>>a[i]>>b[i];
		scanf("%d %d",&a,&b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int q;
	cin>>q;
	vector<int> s(q),t(q);
	rep(i,q){
		//cin>>s[i]>>t[i];
		scanf("%d %d",&s[i],&t[i]);
	}
	rep(i,n){
		d[i]=0;
	}
	int check=1;
	int i=1;//???
	int i2=0;//???????????°
	while(check){
		stack<int> st;
		st.push(i2);
		d[i2]=i;
		while(!st.empty()){
			int x=st.top();
			st.pop();
			rep(j,g[x].size()){
				if(d[g[x][j]]==0){
					st.push(g[x][j]);
					d[g[x][j]]=i;
				}
			}
		}
		check=0;
		rep(j,n){
			if(d[j]==0){
				check=1;
				i++;
				i2=j;
				break;
			}
		}
	}

	rep(i,q){
		if(d[s[i]]==d[t[i]]){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}



	
}

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

using namespace std;

static const int N = 100001;

int n, m;
int color[N], dist;

vector<int> G[N];

void dfs(int i, int c){
	queue<int> q; q.push(i);
	color[i] = c;

	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int i = 0; i < G[u].size(); ++i){
			int v = G[u][i];
			if (-1 == color[v]){
				color[v] = c;
				q.push(v);
			}
		}
	}
}


void search(){
	for (int i = 0; i < n; ++i) color[i] = -1;

	int c = 0;
	for (int i = 0; i < n; ++i)	{
		if (color[i] == -1) dfs(i, c++);
	}
}


int main()
{
	cin >> n >> m;

	for (int i = 0; i < m; ++i) {
		int u, v; cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	search();

	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, v; cin >> u >> v;
		if (color[u] == color[v]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

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

const long N = 100;
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[N][N];
long color[N], cc[N], cp = 0;
long nt[N];

long next(long u){
	for (long v = nt[u]; v <= n; v++){
		nt[u] = v + 1;
		if (M[u][v]) return v;
	}
	return -1;
}

void dfs_visit(long r){
	for (long i = 0; i < n; i++) nt[i] = 0;

	stack<long> S;
	S.push(r);
	color[r] = GRAY;
	cc[r] = cp;

	while (!S.empty()){
		long u = S.top();
		long v = next(u);
		if (v != -1){
			if (color[v] == WHITE){
				color[v] = GRAY;
				cc[v] = cp;
				S.push(v);
			}
		}
		else {
			S.pop();
			color[u] = BLACK;
		}
	}
	cp++;
	// cout << cp << endl;
}

void dfs(){
	for (long i = 0; i < n; i++){
		color[i] = WHITE;
		nt[i] = 1;
	}

	for (long u = 0; u < n; u++){
		if (color[u] == WHITE) dfs_visit(u);
	}

}

int main(){
	long m, q;
	cin >> n >> m;
	for (long i = 0; i < n; i++){
		for (long j = 0; j < n; j++){
			M[i][j] = 0;
		}
	}
	for (long i = 0; i < m; i++){
		long s, t;
		cin >> s >> t;
		M[s][t] = M[t][s] = 1;
	}
	dfs();
	cin >> q;
	// for (long i = 0; i < n; i++){
	// 	if (i) cout << " ";
	// 	cout << cc[i];
	// }
	// cout << endl;
	for (long i = 0; i < q; i++){
		long s, t;
		cin >> s >> t;
		if (cc[s] == cc[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <queue>


std::vector<int> V[100000];
int C[100000];
int color;
int maxi;

int main () {
    int n, m, q;
    std::cin >> n >> m;

    for (int i; i < m; i++) {
        int s, t;
        std::cin >> s >> t;
        V[s].push_back(t);
        V[t].push_back(s);
    }

    for (int i = 0; i < n; i++) {
        if(C[i] == 0) {
            color++;
            std::queue<int> Q;
            Q.push(i);
            while (Q.size()) {
                C[Q.front()] = color;
                for (auto t: V[Q.front()]) {
                    if (C[t] == 0) {
                        C[t] = color;
                        Q.push(t);
                    }
                }
                Q.pop();
            }
        }
    }

    std::cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        std::cin >> s >> t;
        if ((C[s] == C[t]) && (C[s] != 0)) {
            std::cout << "yes" << std::endl;
        }
        else{
            std::cout << "no" << std::endl;
        }
    }

}