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++: #include <bits/stdc++.h>
using namespace std;
int n, m;
vector< list<int> > v;
vector<bool> used;
int p[100000];
int r[100000];

int find(int x){
  if(p[x] == x) return x;
  else return p[x] = find(p[x]);
}
void unite(int x, int y){
  x = find(x);
  y = find(y);
  if(x == y) return;

  if(r[x] < r[y])
    p[x] = y;
  else{
    p[y] = x;
    if(r[x] == r[y]) r[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

void dfs(int pos, int bgn){
  if(used[pos]) return;
  used[pos] = true;
  unite(pos, bgn);
  for(list<int>::iterator it = v[pos].begin(); it != v[pos].end(); it++){
    dfs(*it, bgn);
  }
}

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

  cin >> n >> m;
  v = vector< list<int> >(n, list<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);
  }

  for(int i = 0; i < n; i++){
    p[i] = i;
    r[i] = 0;
  }

  for(int i = 0; i < n; i++){
    dfs(i, i);
  }

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

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
 
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  int uf[n];
  rep(i, n) uf[i] = i;
  rep(i, m) {
    int s, t, nows, nowt;
    scanf("%d%d", &s, &t);
    nows = s, nowt = t;
    while(uf[nows] != nows) {
      nows = uf[nows];
    }
    while(uf[nowt] != nowt) {
      nowt = uf[nowt];
    }
    uf[nowt] = nows;
  }
  rep(i, n) {
    int nows = i;
    while(uf[nows] != nows) {
      nows = uf[nows];
    }
    uf[i] = nows;
  }
  /*rep(i, n) printf("%d ", uf[i]);
  printf("\n");*/
  int q;
  scanf("%d", &q);
  rep(i, q) {
    int s, t;
    scanf("%d%d", &s, &t);
    if(uf[s] != uf[t]) printf("no\n");
    else printf("yes\n");
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 100010;
int n, m;
int u, num, v;
int flag[maxx];
int color[maxx];
vector<int> G[maxx];

int c = 1;
void dfs(int t) {
    stack<int>s;
    s.push(t);
    color[t] = c;
    while (!s.empty()) {
        int u = s.top(); s.pop();
        int len = G[u].size();
        for (int i = 0; i < len; i++) {
            int v = G[u][i];
            if (flag[v] == 0) {
                color[v] = c;
                flag[v] = 1;
                s.push(v);
            }
        }
    }
}
int main()
{
    int q;
    memset(flag, 0, sizeof(flag));
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &u, &v);
        //将顶点v添加到G[u]数组中
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for (int i = 0; i < n; i++) {
        if (flag[i] == 0) {
            dfs(i);
            c++;
        }
    }
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &u, &v);
        if (color[u] == color[v]) {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
    return 0;
}

C++: #include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9;
int idx[MAX]={0};
void init(){
	int i;
	for(i=0;i<MAX;i++)
		idx[i]=i;
}
int findroot(int i){
	while(i!=idx[i])
		i=idx[i];
	return i;
}
void munion(int u ,int v){
	u=findroot(u);
	v=findroot(v);
	if(u!=v)
		idx[v]=u;
}
int main(){
	int i ,j ,n ,m;
	cin>>n>>m;
	init();
	while(m--){
		int x ,y;
		cin>>x>>y;
		munion(x ,y);
	}
	int q;
	cin>>q;
	while(q--){
		int  u ,v;
		cin>>u>>v;
		u=findroot(u);
		v=findroot(v);
		if(u==v)
			cout<<"yes"<<"\n";
		else
			cout<<"no"<<"\n";
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5
#define NIL -1
int flag = 0;
vector<int> G[MAX];
queue<int> qu;
int vis[MAX];
int n;
int ind, tmp;

void bfs(int u,int k) {
	qu.push(u);
	vis[u] = k;
	while (not qu.empty()) {
		int u = qu.front(); qu.pop();
		for (int j = 0; j < G[u].size(); j++) {
			int v = G[u][j];
			if (vis[v] == NIL) {
				vis[v] = k;
				qu.push(v);
			}
		}
	}
}

void calc() {
	int id = 1;
	for (int i = 0; i < n; i++) vis[i] = NIL;
	for (int i = 0; i < n; i++) {
		if (vis[i] == NIL) bfs(i, id++);
	}
}

int main() {
	int m, q;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
		G[tmp].push_back(ind);
	}
	calc();
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		if (vis[ind] == vis[tmp])
		cout << "yes" << endl;
		else cout << "no" << endl;
	}
}