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<stdio.h>
using namespace std;

const int MAX = 100001;

int parent(int table[], int child) {
	if (table[child] == 0)return child;
	return table[child] = parent(table, table[child]);
}

int main() {
	int n, m, q;
	int table[MAX] ={0};

	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &s, &t);
		s++;t++;
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
	}

	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		
		scanf("%d %d", &s, &t);
                s++;t++;
		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_set>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< vector<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< vector<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).push_back(t);
		friendGraphList.at(t).push_back(s);
	}
	for (auto& v : friendGraphList)v.shrink_to_fit();
}

void accesibleNodeSearch(const vector<vector<int>>& graphList, vector<vector<int>>& visitMatrix, const int s) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	vector<bool> visitList;
	visitList.resize(n, false);
	nodeQueue.push(s);
	visitList.at(s).flip();
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			if (visitList.at(adjID))continue;
			const vector<int>& idList = graphList.at(adjID);
			if (find(idList.begin(), idList.end(), id) == idList.end())continue;
			nodeQueue.push(adjID);
			visitList.at(adjID).flip();
		}
		nodeQueue.pop();
	}
	const int num = count(visitList.begin(), visitList.end(), true);
	visitMatrix.at(s).reserve(num);
	for (int i = 0; i != n; ++i) {
		if (!visitList.at(i))continue;
		visitMatrix.at(s).push_back(i);
	}
}

void buildFriendAccesibility(const vector<vector<int>>& graphList, vector<vector<int>>& visitMatrix) {
	const int n = graphList.size();
	visitMatrix.resize(n, vector<int>());
	for (int i = 0; i != n; ++i) {
		accesibleNodeSearch(graphList, visitMatrix, i);
	}
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	vector<vector<int>> visitMatrix;
	buildFriendAccesibility(graphList, visitMatrix);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << ( find(visitMatrix.at(s).begin(), visitMatrix.at(s).end(),t) != visitMatrix.at(s).end() ? "yes" : "no") << endl;
	}
}

int main()
{


	execute(cin);

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int N;
vector<int> par;
void init(int m)
{
    N = m;
    par.resize(N);
    for (int i = 0; i < N; ++i) par[i] = i;
}
int find(int x) {
    return (par[x] == x) ? x : (par[x] = find(par[x]));
}
int isunion(int x, int y) {
    return find(x) == find(y);
}
int tounion(int x, int y)
{
    int p = find(x), q = find(y);
    if (p == q) return 0;
    par[q] = p;
    return 1;
}

int main() {
    int n, m, q;
    cin >> n >> m;
    init(n);
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        tounion(a, b);
    }
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int a, b;
        cin >> a >> b;
        cout << (isunion(a, b) ? "yes\n" : "no\n");
    }
    return 0;
}

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

using namespace std;

#define MAX_N 100000

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;
bool used[MAX_N];
int cmp[MAX_N];
int V;

void printv(string name, int v[], int len) {
	cout << name << " :";
	for (int i = 0; i < len; i++)
		cout << " " << v[i];
	cout << endl;
}

void printv(string name, vector<int> v) {
	cout << name << " :";
	for (int i = 0; i < V; i++)
		cout << " " << v[i];
	cout << endl;
}

void add_edge(int from, int to) {
	G[from].push_back(to);
	rG[to].push_back(from);
}

void dfs(int v) {
	used[v] = true;
	for (int i = 0; i < G[v].size(); ++i)
		if (!used[G[v][i]])
			dfs(G[v][i]);
	vs.push_back(v);
}

void rdfs(int v, int k) {
	used[v] = true;
	cmp[v] = k;
	for (int i = 0; i < rG[v].size(); ++i)
		if (!used[rG[v][i]])
			rdfs(rG[v][i], k);
}

int scc() {
	fill_n(used, MAX_N, false);
	vs.clear();
	for (int v = 0; v < V; ++v)
		if (!used[v])
			dfs(v);
	fill_n(used, MAX_N, false);
	int k = 0;
	for (int i = vs.size() - 1; i >= 0; --i)
		if (!used[vs[i]])
			rdfs(vs[i], k++);
	return k;
}

int scc(int v) {
	fill_n(used, MAX_N, false);
	vs.clear();
//	for (int v = 0; v < V; ++v)
		if (!used[v])
			dfs(v);
	fill_n(used, MAX_N, false);
	int k = 0;
	for (int i = vs.size() - 1; i >= 0; --i)
		if (!used[vs[i]])
			rdfs(vs[i], k++);
	return k;
}

int main() {
	int n, m;
	cin >> n >> m;
	V = n;
	for (int i = 0; i < m; ++i) {
		int s, t;
		cin >> s >> t;
		add_edge(s, t);
	}
	int k = scc();
//	cout << "k = " << k << endl;
//	printv("cmp", cmp, V);
//	printv("vs", vs);
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int s, t;
		cin >> s >> t;
		scc(s);
		if (used[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

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

#define MAX 100000

using namespace std;

int visited[MAX];
//adjacent list
vector<int> G[MAX];

//can go from s to t or not
int color[MAX];

void dfs(int s,int c){
  color[s]=c;
  visited[s]=1;
  for(int i=0;i<G[s].size();i++){
    if(visited[G[s][i]]==0){
      dfs(G[s][i],c);
    }
  }
}

int main(){
  int n,m; cin >> n >> m;
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<m;i++){
    int x,y; cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  int q; cin >> q;
  for(int i=0;i<q;i++){
    for(int j=0;j<n;j++) visited[j]=0;
    int s,t; cin >> s >> t;
    dfs(s,i);
    if(color[s]==color[t]) cout << "yes" << endl;
    else cout << "no" << endl;
  }
}