Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

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

}