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 <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int NMAX = 100000;
const int MMAX = 100000;

vector<int> table[NMAX]; 

int color[NMAX];

void search(int s, int id, int n) {
	queue<int> q;
	q.push(s);
	color[s] = id;

	while(true) {
		if (q.empty()) {
			return;
		}
		int current = q.front(); q.pop();
		for (int i = 0; i < table[current].size(); i++) {
			int v = table[current][i];
			if (color[v] == -1) {
				q.push(v);
				color[v] = id;
			}
		}
	}
}

void assignColor(int n) {
	int id = 0;
	for (int i = 0; i < n; i++) {
		color[i] = -1;
	}	
	
	for (int i = 0; i < n; i++) {
		if (color[i] == -1) {
			id++;
			search(i, id, n);
		}
	}
}

int main() {
	int n, m;
	scanf("%d %d", &n, &m);

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

	assignColor(n);

	int q;
	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		scanf("%d %d", &s, &t);

		bool result = color[s] == color[t];
		if (result) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}

	return 0;
}

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

const int N=100001;

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

void dfs(int x,int y){
  stack<int> S;
  
  S.push(x);
  color[x]=y;
  
  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]==-1){
	color[v]=y;
	S.push(v);
      }
    }
  }
}

void assignColor(){
  int id=1;
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<n;i++){
    if(color[i]==-1) dfs(i,id++);
  }
}

int main(){
  int m,q,s,t;

  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;
#include<stdio.h>

typedef struct fox { int val; fox *prev, *next; } fox_t;

fox_t* init()
{
	fox_t* head = new fox_t;
	head->val = -1;
	head->next = NULL;
	head->prev = NULL;
	return head;
}

fox_t* make_tail(fox_t* head)
{
	fox_t* tail = new fox_t;
	tail->val = -1;
	head->next = tail;
	tail->next = NULL;
	tail->prev = head;
	return tail;
}

bool isEmpty(fox_t* head, fox_t* tail)
{
	return (head->next == tail ? true : false);
}

fox_t* b_insert(fox_t* tail, int data)
{
	fox_t* fox = new fox_t;
	fox->val = data;
	fox->prev = tail->prev;
	fox->next = tail;
	tail->prev->next = fox;
	tail->prev = fox;
	return fox;
}

void f_show(fox_t* head, fox_t* tail)
{
	cout << "----??????????????¨???----" << endl;
	fox_t* fox = head->next;

	while(fox != tail){
		cout << fox->val;
		fox = fox->next;
		if(fox != tail) cout << " ";
	};
	cout << endl;
	return;
}
// docking.
void unionfox(fox_t* head1, fox_t* tail1, fox_t* head2, fox_t* tail2)
{
	if(isEmpty(head1, tail1) || isEmpty(head2, tail2)) return;
	fox_t* fox = tail1->prev;
	cout << tail1->prev << endl;
	cout << head2->next << endl;
	fox->next = head2->next;
	head2->next->prev = fox;
	tail2->prev->next = tail1;
	tail1->prev = tail2->prev;
	delete tail2;
	delete head2;
}

int main()
{
	int color[10001];
	int c_num = 0;
	fox_t* foxhead[10001];
	fox_t* foxtail[10001];
	int i, n, m, q;
	int a, b;
	cin >> n >> m;
	for(i = 0; i < n; i++){
		color[i] = -1;
		foxhead[i] = init();
		foxtail[i] = make_tail(foxhead[i]);
	}
	int c_a, c_b, val;

	for(i = 0; i < m; i++){
		cin >> a >> b;
		c_a = color[a];
		c_b = color[b];
		if(c_a < c_b) swap(a, b);  // color[a] >= color[b].
		if(c_a < 0 && c_b < 0){
			color[a] = c_num; color[b] = c_num;
			b_insert(foxtail[c_num], a);
			b_insert(foxtail[c_num], b);
			c_num++;
		}else if(c_b < 0){
			color[b] = c_a;
			b_insert(foxtail[c_a], b);
		}else if(c_a == c_b){
			continue;
		}else{ // color[a] != color[b].
			// b????????¬?????????color???a???color??????????????§???union?????????
			fox_t* fox = foxhead[c_b]->next;
			while(fox != foxtail[c_b]){
				val = fox->val;
				color[val] = c_a;
				fox = fox->next;
			};
			unionfox(foxhead[c_a], foxtail[c_a], foxhead[c_b], foxtail[c_b]);
		}
	}
	cin >> q;
	for(i = 0; i < q; i++){
		cin >> a >> b;
		if(color[a] >= 0 && (color[a] == color[b])){
			printf("yes\n"); }else{ printf("no\n"); 
			}
	}
	return 0;
}

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

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
int main() {
    cin>>n>>m;
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        cin>>s>>t;
        g[s].push_back(t);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>s>>t;
        cout<<(color[s]==color[t]?"yes":"no")<<endl;
    }
    return 0;
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e5 + 10;

int m,n;
int p[MAXN];

int Find(int x){
    return x==p[x] ? x : p[x]=Find(p[x]);
}

void Merge(int u,int v){
    int x=Find(u);
    int y=Find(v);
    if(x!=y){
        p[x]=y;
    }
}

void work(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=m;i++){
        int u,v; scanf("%d%d",&u,&v);
        Merge(u,v);
    }
    int q; scanf("%d",&q);
    for(int i=1;i<=q;i++){
        int s,t; scanf("%d%d",&s,&t);
        if(Find(s)==Find(t)) printf("yes\n");
        else printf("no\n");
    }
}

int main(){
    work();
    return 0;
}