Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

vector<vector<int>> a(100000);
vector<int> comp(100000, 0);

void dfs(int s, int id) {
	stack<int> st;
	st.push(s);
	comp[s] = id;
	while (!st.empty()) {
		int i = st.top();
		int k = a[i].size();
		REP(j, k + 1) {
			if (j < k) {
				int t = a[i][j];
				if (comp[t] != id) {
					comp[t] = id;
					st.push(t);
					break;
				}
			}
			else st.pop();
		}
	}
}

void divide(int size) {
	int s = 0;
	int id = 1;
	while (1) {
		dfs(s, id);
		while (s < size && comp[s]>0) s++;
		if (s == size) break;
		id++;
	}
	return;
}

int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);

	int n, m;
	scanf("%d %d", &n, &m);
	//cin >> n >> m;
	REP(i, m) {
		int s, t;
		scanf("%d %d", &s, &t);
		//cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	int q;
	cin >> q;
	divide(n);
	REP(i, q) {
		int s, t;
		scanf("%d %d", &s, &t);
		//cin >> s >> t;
		if (comp[s] == comp[t]) printf("yes\n");
		else printf("no\n");
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define same(a,b) (find(a)==find(b))
#define uni(a,b) fa[find(a)]=find(b)
#define init(n) for(int i=0;i<n;i++) fa[i]=i

int fa[100100];

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

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	init(n);
	for(int i=0;i<m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		uni(u,v);
	}
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		if(same(u,v)) printf("yes\n");
		else printf("no\n");
	}
	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) des(u,id++);
  }
 }
int main (){
  int s,t,q,m;

  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" << endl;
  }else{
   cout << "no" << endl;
  }
 }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;
class UnionFind {
public:
	vector<int> Parent;
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}
	int root(int A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	int size(int A) {
		return -Parent[root(A)];
	}

	bool connect(int A, int B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};
int main()
{
	int n, m, q;
	cin >> n >> m;
	UnionFind uni(n);
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		uni.connect(s, t);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (uni.root(s) == uni.root(t)) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <iterator>
using namespace std;

int n, m;
vector<int> *v;

int bfs(int s, int t) {
    vector<int> *vc = new vector<int>[n];
    for (int i = 0; i < n; i++) {
        vc[i] = v[i];
    }

    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; i++) {
        int u = q.front();
        q.pop();
        for (vector<int>::iterator it = vc[i].begin(); it != vc[i].end(); it++) {
            if (*it == t) {
                return 1;
            }
            q.push(*it);
            vector<int>::iterator result = find(vc[*it].begin(), vc[*it].end(), i);
            if (result != vc[*it].end()) { vc[*it].erase(result); }
        }
    }
    return 0;
}

int main() {
    scanf("%d %d", &n, &m);
    v = new vector<int>[n];

    for (int i = 0; i < m; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        v[s].push_back(t);
        v[t].push_back(s);
    }

    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        printf("%s\n", bfs(s, t) ? "yes" : "no");
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define N 100005
using namespace std;
vector<int> e[N];
int n,m,q,s,t,cnt;
int used[N];

void dfs(int x){
  if(used[x])return;
  used[x]=cnt;
  for(int i=0;i<e[x].size();i++)
    dfs(e[x][i]);
}

int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    e[s].push_back(t);
    e[t].push_back(s);
  }
  cnt=1;
  for(int i=0;i<n;i++){
    if(!used[i])dfs(i),cnt++;
  }
  cin>>q;
  while(q--){
    cin>>s>>t;
    if(used[s]==used[t])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}