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<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t; vector<int>a[100000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	Q.push(A);
	while (!Q.empty() || x[B] == 0) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			if (x[a[Q.front()][k]] == 0) { 
				Q.push(a[Q.front()][k]); x[a[Q.front()][k]] = 1; 
				a[Q.front()].push_back(a[Q.front()][k]);
			}
		}
		Q.pop();
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

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

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

int x[100000][10];
bool y[100000];
int z[100000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][z[a]]=b;
		x[b][z[b]]=a;
		z[a]++;
		z[b]++;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<20;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<z[j];k++){
						y[x[j][k]]=true;
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 100005
#define INFTY -1

int n, m, st[MAX];
vector<vector<int> > G, C;

void bfs(int s) {
	if (st[s] != 0) return;
	vector<int> ve;
	queue<int> Q;
	Q.push(s);
	st[s] = 1;
	ve.push_back(s);
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			if (st[v] != 0) continue;
			Q.push(v);
			st[v] = 1;
			ve.push_back(v);
		}
	}
	C.push_back(ve);
}

void solution() {
	int i;
	for (i = 0; i < n; i++) {
		bfs(i);
	}
	for (i = 0; i < C.size(); i++) {
		sort(C[i].begin(), C[i].end());
	}
}

int main(void) {
	int i, j, a, b, q;
	for (i = 0; i < MAX; i++) {
		st[i] = 0;
	}
	cin >> n >> m;
	G.resize(n);
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	solution();
	cin >> q;
	while (q--) {
		bool flag = false;
		cin >> a >> b;
		for (i = 0; i < C.size(); i++) {
			if (*(lower_bound(C[i].begin(), C[i].end(), a)) != a) continue;
			if (*(lower_bound(C[i].begin(), C[i].end(), b)) != b) continue;
			flag = true;
		}
		if (flag)
			cout << "yes\n";
		else
			cout << "no\n";
	}
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#define N 100000
using namespace std;

int color[N];
vector<int> G[N];
queue<int> Q;
int n;

void bfs(int s,int c) {
	Q.push(s);
	color[s] = c;
	while (!Q.empty()) {
		for (unsigned int it = 0;it < G[Q.front()].size();it++)
			if (color[G[Q.front()][it]] == 0){
				Q.push(G[Q.front()][it]);
				color[G[Q.front()][it]] = c;
		    }
		Q.pop();
	 }
}

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

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

	Color();

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

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

using namespace std;


vector<int> vertexes[100000];
stack<int> visiting;
bool discovered[100000];
int belong[100000];
int subgraph_id = 0;


void dfs(int v)
{
    visiting.push(v);
    discovered[v] = true;
    belong[v] = ++subgraph_id;

    while (!visiting.empty()) {
        int now = visiting.top();
        visiting.pop();


        for (vector<int>::iterator it = vertexes[now].begin(); it != vertexes[now].end(); it++) {
            if (!discovered[*it]) {
                discovered[*it] = true;
                belong[*it] = subgraph_id;
                visiting.push(*it);
            }
        }
    }
}


int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        vertexes[x].push_back(y);
        vertexes[y].push_back(x);
    }

    for (int i = 0; i < n; i++)
        if (!discovered[i])
            dfs(i);

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x, y;
        cin >> x >> y;
        if (belong[x] == belong[y])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef complex<double> P;

int main() {
  int n, m; cin >> n >> m;
  int adj[n][n] = {};

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

  int q; cin >> q;
  int sd[n] = {};

  for (int i = 0; i < q; i++) {
    int s, t; cin >> s >> t;
    fill(sd, sd+n, 0);

    queue<int> que;
    que.push(s);
    bool f = false;

    while(!que.empty()) {
      int v = que.front(); que.pop();
      if(v == t) {
        f = true;
        break;
      }

      for (int j = 0; j < n; j++) {
        if(adj[v][j] == 1 && sd[j] == 0) {
          que.push(j);
        }
      }
    }

    cout << (f ? "yes" : "no") << endl;

  }
}