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 <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

int n, m;
vector<int> G[100000];
int id[100001];
int visit[100001] = {0};
int s, t;

void check(int x, int u) {
    visit[x] = 1;
    id[x] = u;

    int v;
    REP(i, G[x].size()) {
        v = G[x][i];
        if (visit[v] == 0) check(v, u);
    }
}

int main() {
    cin >> n >> m;
    int a, b;
    REP(i, m) {
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    REP(i, n) {
        if (visit[i] == 0) check(i, i);
    }

    int q;
    cin >> q;
    REP(i, q) {
        cin >> s >> t;
        if (id[s] == id[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

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

static const int MAX = 100000;
static const int NIL = -1;
int n;
int color[MAX];
vector<int> G[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" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;
}

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

int uni[100000];

int oya(int a){
if (uni[a]<0){
	return a;
}
else{
	uni[a]=oya(uni[a]);
	return oya(uni[a]);
}
}

bool ren(int a,int b){
	a=oya(a);
	b=oya(b);
	if(a==b){
	return	false;
	}
	else{
		
	return true;	
	}
}

int main() {

int n,m;
cin>>n>>m;
for(int y=0;y<n;y++){
	uni[y]=-1;
}

for(int r=0;r<m;r++){
int s,g;
cin>>s>>g;
if(ren(s,g)){
	s=oya(s);
	uni[s]=g;
	}
}
int q;
cin>>q;
for(int rr=0;rr<q;rr++){
	int s,g;
	cin>>s>>g;
	if(ren(s,g)==true){
		cout<<"no"<<endl;
	}
	else{
		cout<<"yes"<<endl;
	}
}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;


int n;

string *color;

queue<int> q;

int *ans;

vector<int> g[10000];


void bfs(int x){
  color[x] = "gray";
  q.push(x);


  while(q.size() != 0){
    int u = q.front();
    q.pop();

    for(int i = 0; i < g[u].size(); i++){
      int v = g[u][i];
      if(color[v] == "white"){
        color[v] = "gray";
        q.push(v);
      }
    }

    color[u] = "black";
    ans[u] = x;


  }
  return;
}


int main(){

  cin >> n;

  color = new string[n];

  ans = new int[n];

  int m;

  cin >> m;


  int a, b;


  for(int i = 0; i < m; i++){
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }


  for(int i = 0; i < n; i++){
    color[i] = "white";
  }


  for(int j = 0; j < n; j++){
    if(color[j] == "white"){
      bfs(j);
    }
  }

  int q;

  cin >> q;

  for(int i = 0; i < q; i++){
    cin >> a >> b;
    if(ans[a] == ans[b]){
      cout << "yes" << endl;
    }
    else{
      cout << "no" << endl;
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, m;
vector< vector<int> > v;
vector<bool> used;

bool dfs(int pos, int dest){
  if(used[pos]) return false;
  used[pos] = true;
  if(pos == dest) return true;
  for(int i = 0; i < v[pos].size(); i++){
    if(dfs(v[pos][i], dest)){
      return true;
    }
  }
  return false;
}

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

  cin >> n >> m;
  v = vector< vector<int> >(n, vector<int>(0));
  used = vector<bool>(n, false);
  for(int i = 0; i < m; i++){
    int a, b; cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int q; cin >> q;
  for(int i = 0; i < q; i++){
    used = vector<bool>(n, false);
    int a, b; cin >> a >> b;
    if(dfs(a, b)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  return 0;
}

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

const int MAX = 100000;
vector<int> G[MAX];

class DisjointSet{
public:
    vector<int> rank, parent;
    DisjointSet();
    DisjointSet(int size){
        rank.resize(size,0);
        parent.resize(size,0);
        for (int i=0; i<size; i++) {
            makeSet(i);
        }
    }

    bool same(int u,int v){
        if (parent[u] == parent[v]) {
            return true;
        }else{
            return false;
        }
    }
    
    void unite(int u,int v){
        int x = findSet(u);int y= findSet(v);
        if(rank[x]<rank[y]){
            parent[x] = y;
        }else{
            parent[y] = x;
            if(rank[x]==rank[y]){
                rank[x]++;
            }
        }
    }
    // ????????????????????????
    int findSet(int u){
        
        if(u!=parent[u])
            parent[u] = findSet(parent[u]);
        
        return parent[u];
    }
private:
    void makeSet(int i){
        rank[i] = 0;
        parent[i] = i;
    }
};
    
int main(int argc, const char * argv[]) {
    
    int n,m;
    cin >> n >> m;
    DisjointSet ds(n);
    
    for(int i=0;i<m;i++){
        int s,t;
        cin >> s >> t;
        ds.unite(s, t);
    }
    int q;
    cin >> q;
    for(int i=0;i<q;i++){
        int s,t;
        if(ds.same(s, t)) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}