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<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 100005

using namespace std;
int n, m, q;
vector<int> A[Lens];
queue<int>	Q;
char c[Lens], d[Lens];

int Find(int s, int t)
{
	Q.push(s);
	c[s] = 'g';
	while (!Q.empty())
	{
		s = Q.front();
		Q.pop();
		c[s] = 'g';
		for (int j = 0; j < A[s].size(); j++)
		{
			if (c[A[s][j]] == 'w')
				Q.push(A[s][j]);
			if (A[s][j] == t)
			{
				return 1;
			}
		}
	}
return 0;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		d[s] = d[t] = 'w';
		A[s].push_back(t);
		A[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		for (int j = 0; j < n; j++)
			c[j] = d[j];
		int s, t;
		cin >> s >> t;
		if (Find(s, t))
			cout << "yes" << endl;

		else
			cout << "no" << endl;
	}
	return 0;
}

C++: #include<vector>

class UnionFind {
public:
    UnionFind(int N) :parent(N), rank(N, 0), size(N, 1) {
        for (int i = 0; i < N; ++i) parent[i] = i;
    }
    int find(int x) {
        return x == parent[x] ? x : parent[x] = find(parent[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 (rank[x] < rank[y]) parent[x] = y;
        else {
            parent[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
            std::swap(x, y);
        }
        size[y] += size[x];
        size[x] = 0;
    }
    int getParentRank(int x) {
        return rank[find(x)];
    }
    int getParentSize(int x) {
        return size[find(x)];
    }
    int getSize(int x) {
        return size[x];
    }
private:
    std::vector<int> parent, rank, size;
};
#include<iostream>
using namespace std;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    UnionFind UF(n);
    for(int i=0;i<m;++i){
        int a,b;
        cin>>a>>b;
        UF.unite(a,b);
    }
    int q;
    cin>>q;
    for(int i=0;i<q;++i){
        int a,b;
        cin>>a>>b;
        cout<<(UF.same(a,b)?"yes":"no")<<endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(i=a;i<b;i++)
#define rloop(i,a,b) for(i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
const int inf=1000000001;
const ll INF=1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int N=100000;

int n;
vii v(N,vi());
vi color(N),nt(N);

bool dfs(int,int);
int next(int);

int main(){
	int m,i,j;
	cin>>n>>m;
	int s,t;
	loop(i,0,m){
		cin>>s>>t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	int q;
	cin>>q;
	loop(i,0,q){
		cin>>s>>t;
		bool flag=dfs(s,t);
		if(flag==true){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
	}
}

bool dfs(int s,int t){
	if(s==t){
		return true;
	}
	int i;
	loop(i,0,n+1){
		color[i]=WHITE;
	}
	stack<int> st;
	st.push(s);
	color[s]=GRAY;
	while(!st.empty()){
		int u=st.top();
		int u2=next(u);
		if(u2==t){
			return true;
		}else if(u2!=-1){
			color[u2]=GRAY;
			st.push(u2);
		}else{
			color[u]=BLACK;
			st.pop();
		}
	}
	return false;
}

int next(int u){
	int i;
	loop(i,0,v[u].size()){
		if(color[v[u][i]]==WHITE){
			return v[u][i];
		}
	}
	return -1;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
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;
  for (int i = 0; i < n; i++ ) color[i] = NIL;
  for (int u = 0; u < n; u++ ) {
    if (color[u] == NIL ) dfs(u, 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);
  }

  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>
#include<vector>
#include<stack>
using namespace std;
#define max 100000
#define 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;
	for (int i = 0; i < n; i++) color[i] = nil;
	for (int u = 0; u < n; u++) {
		if (color[u] == nil) dfs(u, 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);
	}
    
	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;
}