Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include<cstdio>
#include<queue>
#include<vector>
#define N_MAX 100005
using namespace std;
struct Node{
    vector<int> vct;
    short type;
    char state;//0:undiscovered
};
vector<Node> nodes;
int n;
void bfs(){
    queue<int> Q;
    short type=1;
    for(int i=0;i<n;i++){
        if(nodes[i].state==0){
            Q.push(i);
            nodes[i].type = type;
            nodes[i].state = 1;
            while(!Q.empty()){
                int active = Q.front();
                int total = nodes[active].vct.size();
                for(int i=0;i<total;i++){
                    int newIndex=nodes[active].vct[i];
                    if(nodes[newIndex].state==0){
                        Q.push(newIndex);
                        //printf("== %d -> %d ==\n", active, newIndex);
                        nodes[newIndex].type = type;
                        nodes[newIndex].state = 1;
                    }
                }
                Q.pop();
            }
            type++;
        }
    }
}
int main(){
    int rel;
    scanf("%d%d", &n, &rel);
    for(int i=0;i<n;i++){//initial
        Node init;
        init.type=0;
        init.state=0;
        nodes.push_back(init);
        //printf("initial info: %d %d\n", init.type, init.state);
    }
    for(int i=0;i<rel;i++){
        int maj, sub;
        scanf("%d%d", &maj, &sub);
        nodes[maj].vct.push_back(sub);
        nodes[sub].vct.push_back(maj);//Undirected
    }
    bfs();
    short ques;
    scanf("%d", &ques);
    for(short i=0;i<ques;i++){
        int maj, sub;
        scanf("%d%d", &maj, &sub);
        if(nodes[maj].type==nodes[sub].type){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }

    /*printf("\n============ now testing ================\n");
    for(int i=0;i<n;i++){
        printf("\t-- %d: type:%d state:%d --\n", i, nodes[i].type, nodes[i].state);
    }*/
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstdio>
using namespace std;
const int maxn=100000;
const int NIL=-1;
int n;
vector<int> G[maxn];
int color[maxn];

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 setColor(){
    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);
    }

    setColor();

    cin>>q;
    for(int i=0;i<q;++i){
        cin>>s>>t;
        if(color[s]==color[t]){
            puts("yes");
        }else{
            puts("no");
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n, m;

bool frend(vector<vector<int>> &M, vector<int> &memo, int i, int t) {
  if (memo[i] == 1) return false;
  memo[i] = 1;
  int cnt = M[i].size();
  for (int z = 0; z < cnt; z++) {
    if (M[i][z] == t) return true;
    if (memo[M[i][z]] == 0) {
      if (frend(M, memo, M[i][z], t)) { return true; }
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n);

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  for (int i = 0; i < qn; i++) {
    vector<int> memo(n);
    bool res = frend(M, memo, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

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

int n, m, q;
int dp[100000];
vector<int> v[100000];

void f(int p, int idx) {
	if (dp[p] < 0) {
		dp[p] = idx;
		for (int i = 0; i < v[p].size(); i++) {
			f(v[p][i], idx);
		}
	}
}

void f2() {
	int idx = 0;
	for (int i = 0; i < n; i++) {
		if (dp[i] < 0) {
			f(i, idx++);
		}
	}
}


int main(){
	int s, t;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	cin >> q;
	fill(dp, dp + n, -1);
	f2();
	for (int I = 0; I < q; I++) {
		cin >> s >> t;
		if (dp[s] == dp[t]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector<int> group;

void bfs(int v, int g, vector< vector<int> > list) {
  if (group[v] != 0) {
    return;
  }
  group[v] = g;
  for (int i = 0; i < list[v].size(); i++) {
    bfs(list[v][i], g, list);
  }
}

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

  int n,m,s,t,q;
  cin >> n >> m;

  vector< vector<int> > list(n, vector<int>());
  group = vector<int>(n,0);
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  int g = 1;
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      bfs(i, g, list);
      g++;
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (group[s] == group[t]) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}