Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

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);
		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");
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX1 = 1e5;
const int MAX2 = 1e4;
struct node {
    int id;
    node* next;
};
node* g[MAX1];
int c;
int color[MAX1];
bool visit[MAX1];
int n,m;
void dfs(int root, int c) {
    stack<int> s;
    s.push(root);
    visit[root] = true;
    color[root] = c;
    while(!s.empty()) {
        int x = s.top();
        node* p = g[x]->next;
        int flag = 0;
        while(p != NULL) {
            if(visit[p->id]==false) {
                flag = 1;
                s.push(p->id);
                visit[p->id] = true;
                color[p->id] = c;
                break;
            }
            p = p->next;
        }
        if(flag == 0) s.pop();

    }

}

bool reach(int s, int t) {
    return color[s] == color[t];
}
int main() {
    c = 0;// 代表颜色
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        visit[i] = false;
    }
    for(int i = 0; i < n; i++) {
        g[i] = (node*)malloc(sizeof(node));
        g[i]->next = NULL;
    }
    for(int i = 0; i < m; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        node* l = (node*)malloc(sizeof(node));
        l->id = t;
        l->next = NULL;
        node* p = g[s];
        while(p->next!=NULL) p = p->next;
        p->next = l;

        node* l2 = (node*)malloc(sizeof(node));
        l2->id = s;
        l2->next = NULL;
        node* p2 = g[t];
        while(p2->next!=NULL) p2 = p2->next;
        p2->next = l2;
    }
    for(int i = 0; i < n; i++) {
        if(visit[i] == false) {
            dfs(i, c);
            c++;
        }
    }
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        if(reach(s, t)) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define WHITE 0 //未到達
#define GLAY 1 //訪問中
#define BLACK 2 //訪問完了
#define INF 1000000001


void dfs(const vector<vector<int> > &adj_list,
     const int id, vector<int> &color,const int color_id){
    color[id] = color_id;
    for(auto itr = adj_list[id].begin();itr != adj_list[id].end(); itr++){
        int v = *itr;
        if(color[v] == -1){
            color[v] = color_id;
            dfs(adj_list,v,color,color_id);
        }
    }
    return;
}

void assignColor(const vector<vector<int> > &adj_list, vector<int> &color,const int n){
    int color_id = 0;
    for(int u = 0; u < n; u++){
        if(color[u] == -1){
            dfs(adj_list,u,color,color_id);
            color_id++;
        }
    }
}


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

    cin >> n >> m;
    vector<vector<int> > adj_list(n);
    vector<int> color(n,-1);


    for(int i = 0; i < m; i++){
        cin >> s >> t;
        adj_list[s].push_back(t);
        adj_list[t].push_back(s);
    }

    assignColor(adj_list,color,n);

    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 <cstdio>
using namespace std;
int *a;
int find_top(int i)
{
	while(a[i] != i) i = a[i];
	return i;
}
void union_find(int x,int y)
{
	a[x] = find_top(x);
	a[y] = find_top(y);
	a[a[x]] = a[y];
}
int main(void)
{
	int n,m,u,v;
	scanf("%d %d",&n,&m);
	a = new int [n];
	for(int i = 0;i < n;i++) a[i] = i;
	while(m--)
	{
		scanf("%d %d",&u,&v);
		union_find(u,v);
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d %d",&u,&v);
		a[u] = find_top(u); a[v] = find_top(v);
		printf("%s\n",a[u] == a[v]?"yes":"no");
	}
	return 0;
}