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 <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

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

	set<int> A[n+1];
	int v1, v2;
	r(i, m){
		cin >> v1 >> v2;

		A[v1].insert(v2);
		A[v2].insert(v1);
	}

	//初期の未到達状態は0
	//必要に応じて連結成分ごとに1から順に分類
	int group[n]{};
	stack<int> s;

	int q, groupNum = 0, gn1, gn2;
	cin >> q;
	r(i, q){
		cin >> v1 >> v2;

		if(group[v1] == 0 && group[v2] == 0){
			groupNum++;

			s.push(v1);
			group[v1] = groupNum;

			while(!s.empty()){
				int v = s.top();
				int size = s(A[v]);
				for(int tmp : A[v]){
					if(!group[tmp]){
						s.push(tmp);
						group[tmp] = groupNum;
					}else{
						size--;
					}
				}
				if(!size)
					s.pop();
			}
		}

		gn1 = group[v1];
		gn2 = group[v2];
		//printf("group[%d]=%d, group[%d]=%d: ", v1, gn1, v2, gn2);
		printf(gn1 == gn2 ? "yes\n" : "no\n");
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int pa[MAX];
int ra[MAX];
void init(){
  for(int i=0;i<MAX;i++){
    pa[i]=i;
    ra[i]=0;
  }
}

int find(int x){
  if(pa[x]==x)return x;
  else return pa[x]=find(pa[x]);
}

void unite(int x,int y){
  x=find(x),y=find(y);
  if(x==y)return;
  if(ra[x]<ra[y])swap(x,y);
  pa[y]=x;
  if(ra[x]==ra[y])ra[x]++;
}

bool same(int x,int y){
  return (find(x)==find(y));
}

int n,m,q,a,b;
int main(){
  init();
  scanf("%d %d",&n,&m);
  for(int i=0;i<m;i++){
    scanf("%d %d",&a,&b);
    unite(a,b);
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++){
    scanf("%d %d",&a,&b);
    cout<<(same(a,b)?"yes":"no")<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int>G[MAX];
int color[MAX];

void dfs(int r, int c) {
	stack<int>S;
	S.push(r);
	color[r] = c;
	while (!S.empty()) {
		int u = S.top(); S.pop();
		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			if (color[v] == NIL) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor() {
	int id = 1;
	for (int i = 0; i < n; i++)color[i] = NIL;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)dfs(u, id++);
	}
}
int main() {
	int s, t, m, q;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	assignColor();
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (color[s] == color[t]) {
			cout << "yes" << "\n";
		}else {
			cout << "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<bool>>& visitMatrix, const int s) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	vector<bool>& visitList = visitMatrix.at(s);
	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();
	}
}

void buildFriendAccesibility(const vector<vector<int>>& graphList, vector<vector<bool>>& visitMatrix) {
	const int n = graphList.size();
	visitMatrix.resize(n, vector<bool>(n, false));
	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<bool>> visitMatrix;
	buildFriendAccesibility(graphList, visitMatrix);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << (visitMatrix.at(s).at(t) ? "yes" : "no") << endl;
	}
}

int main()
{


	execute(cin);


	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <string>
#include <queue>
#include <list>
#include <stack>

using namespace std;

/* constant */

const int INF = 1 << 30;
const int mod = 1000000007;
const int MAX_N = 100000;

/* typedef */

typedef long long ll;

/* global variables */

vector< vector<int> > Adj(MAX_N, vector<int>());
vector<int> link(MAX_N, -1);
int n;

/* function */
void bfs(int start);

/* main */
int main() {

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

    for (int i = 0; i < n; i++) {
        if (link[i] == -1) {
            bfs(i);
        }
    }

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

void bfs(int start) {
    
    link[start] = start;
    queue<int> q;
    q.push(start);

    while (!q.empty()) {

        int cur = q.front(); q.pop();

        for (auto v : Adj[cur]) {

            if (link[v] == -1) {
                link[v] = start;
                q.push(v);
            }

        }

    }
}