Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; 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)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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