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<vector>
#include<stack>
using namespace std;
const int MAXN = 100005;
const int NIL = -1;

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

void dfs(int s, int c)
{
	stack<int> S;
	S.push(s);
	color[s] = 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 AssighColor()
{
	int id = 1;
	for(int i = 0; i < n; i++)
		color[i] = NIL;
	for(int i = 0; i < n; i++)
	{
		if(color[i] == NIL)
			dfs(i, 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);
	}
	
	AssighColor();
	
	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 <bits/stdc++.h>
using namespace std;

struct UnionFind{
  int n;
  vector<int> r, p;
  UnionFind() {}
  UnionFind(int sz): n(sz), r(sz, 1), p(sz, 0){ iota(p.begin(), p.end(), 0); }
  int find(int x) {
    return (x == p[x] ? x : p[x] = find(p[x]));
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  void unite(int x, int y){
    x = find(x); y = find(y);
    if ( x == y ) return;
    if ( r[x] < r[y] ) swap(x, y);
    r[x] += r[y];
    p[y] = x;
  }
};

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

  UnionFind uf(n);
  for ( int i = 0; i < m; i++ ) {
    int s, t;
    cin >> s >> t;
    uf.unite(s, t);
  }

  int q;
  cin >> q;
  for ( int i = 0; i < q; i++ ) {
    int s, t;
    cin >> s >> t;
    if ( uf.same(s, t) ) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  
  return 0;
}

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

int main() {
	// 入力
	int n, m;
	std::cin >> n >> m;
	std::vector<std::vector<int>> vec(n);
	std::vector<int> d(n, -1);
	std::stack<int>  s;

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

	// 連結成分分解
	for (int i = 0; i < n; i++) {
		if (d[i] != -1) continue;
		if (vec[i].size() == 0) continue;
		s.push(i);
		while(!s.empty()){
			int x = s.top();
			s.pop();
			for (int j = 0; j < vec[x].size(); j++) {
				if (d[vec[x][j]] == -1) {
					d[vec[x][j]] = i;
					s.push(vec[x][j]);
				}
			}
		}
	}

	int q;
	std::cin >> q;
	while (q--) {
		int a, b;
		std::cin >> a >> b;
		if (d[a] != -1 && d[b] != -1 && d[a] == d[b]) {
			std::cout << "yes" << std::endl;
		}
		else {
			std::cout << "no" << std::endl;
		}
	}
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

using namespace std;

static const int MAX=1000000;
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;
    rep(i,n)color[i]=nil;
    rep(i,n){
        if(color[i]==nil)dfs(i,id++);
    }
    
    
}

int main(){
    int s,t,m,q;
    cin>>n>>m;
    rep(i,m){
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    assignColor();
    
    cin>>q;
    
    rep(i,q){
        cin>>s>>t;
        if(color[s]==color[t]){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
    }
    

return 0;
}

C++: //#define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		printf("%d,", g->id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}