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

typedef uint32_t u32;

template<u32 N_>
class UnionFindTree {
private:
  u32 par[N_];
  u32 rank[N_];
public:
  UnionFindTree(u32 n) {
    assert(n < N_);
    for(u32 i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
    return;
  }
  int find(u32 x) {
    if( par[x] == x ) return x;
    return par[x] = find(par[x]);
  }
  void unite(u32 x, u32 y) {
    x = find(x);
    y = find(y);
    if( x == y ) return;
    if( rank[x] < rank[y] ) {
      par[x] = y;
      return;
    }
    par[y] = x;
    if( rank[x] == rank[y] ) rank[x] += 1;
    return;
  }  
};

int main() {

  u32 n, m;
  std::cin >> n >> m;

  UnionFindTree<200000> uft(n);

  for(u32 i = 0; i < m; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    uft.unite(x, y);
  }

  u32 q;
  std::cin >> q;

  for(u32 i = 0; i < q; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    std::cout << (uft.find(x) == uft.find(y) ? "yes" : "no") << std::endl;
  }
  
  return 0;
}

C++: #include <vector>
#include <iostream>
using namespace std;
int n, m, q, x, y, t; int group[111111]; vector<int> f[111111];
int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) group[i] = i, f[i].push_back(i);
	for(int i = 0; i < m; i++) {
		scanf("%d%d", &x, &y);
		if(group[x] == group[y]) continue;
		x = group[x], y = group[y], t = f[y].size();
		if(f[y].size() > f[x].size()) swap(x, y);
		for(int j = 0; j < t; j++) {
			group[f[y].back()] = group[x];
			f[x].push_back(f[y].back());
			f[y].pop_back();
		}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &x, &y);
		printf("%s\n", group[x] == group[y] ? "yes" : "no");
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5

vector<int> G[MAX];
queue<int> qu;

int main() {
	int n, m, q;
	int ind,tmp;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		flag = 0;
		qu.push(ind);
		while (not qu.empty()) {
			int u = qu.front(); qu.pop();
			if (u == tmp) {
				flag = 1;
				break;
			}
			for (int j = 0; j < G[u].size(); j++) {
				qu.push(G[u][j]);
			}
		}
		if (not flag) {
			swap(ind, tmp);
			qu.push(ind);
			while (not qu.empty()) {
				int u = qu.front(); qu.pop();
				if (u == tmp) {
					flag = 1;
					break;
				}
				for (int j = 0; j < G[u].size(); j++) {
					qu.push(G[u][j]);
				}
			}
		}
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,m,i,s,t,p[1<<17],q;
int root(int u){
  return p[u]==u?u:p[u]=root(p[u]);
}
int main(void){
  for(i=0;i<1<<17;i++)p[i]=i;
  cin>>n>>m;
  while(m--){
    cin>>s>>t;
    s=root(s);
    t=root(t);
    // if(s>t)swap(s,t);
    if(s!=t)p[t]=s;
  }
  cin>>q;
  while(q--){
    cin>>s>>t;
    cout<<(root(s)==root(t)?"yes":"no")<<endl;
  }
  return 0;
}

C++: #include <stdio.h>

int m[100000] = {0};

void init() {
	int i;
	for ( i = 0; i < 100000; ++i) {
		m[i] = i;
	}
}

int find(int a) {
	if ( a == m[a]) {
		return a;
	}
	return m[a] = find(m[a]);
}

int friend(int x, int y) {
	if ( find(x) == find(y)) {
		return 1;
	} else {
		return 0;
	}
}

void unit (int x, int y) {
	x = find(x);
	y = find(y);
	
	if ( x == y ) {
		return ;
	}
	m[y] = x;
}

int main() {
	int n, s, t, u, v, i;
	init();
	scanf("%d", &n);
	scanf("%d", &s);
	
	for ( i = 0; i < s; ++i) {
		scanf("%d", &u);
		scanf("%d", &v);
		unit( u, v);
	}
	
	scanf("%d", &t);
	
	for ( i = 0; i < s; i++) {
		scanf("%d", &u);
		scanf("%d", &v);
		if ( friend( u, v)) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int flag[100000];
vector<int> G[100000];
int color = 0;

void dfs(int f)
{
    for (int i = 0; i < G[f].size(); i++)
    {
        if (flag[G[f][i]] == -1)
        {
            flag[G[f][i]] = color;
            dfs(G[f][i]);
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    int f, g;
    for (int i = 0; i < m; i++)
    {

        cin >> f >> g;
        G[f].push_back(g);
        G[g].push_back(f);
    }

    for (int i = 0; i < n; i++)
        flag[i] = -1;

    for (int i = 0; i < n; i++)
    {
        if (flag[i] == -1)
        {
            color = i;
            flag[i] = color;
            dfs(i);
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
        cin >> f >> g;
        if (flag[f] == flag[g])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}

C++: #include<iostream>
#include<list>
#define MAX 100000
using namespace std;

class person
{
public:
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		while (!lis.empty())
		{
			person* temp = lis.front();
			lis.pop_front();
			if (temp == Per)return true;
			else if (temp->Isdone == true)continue;
			else
			{
				if (temp->serch(Per))return true;
			}
		}
		return false;
	}
};

int main()
{
	person *P;
	P = new person[MAX + 1];
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	delete[] P;
	return 0;
}