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 <bits/stdc++.h>
using namespace std;

int n, m;

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

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

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id][f] = 1;
  }
  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];
    if (q[i][0] > q[i][1]) { swap(q[i][0], q[i][1]); }
  }

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

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

const int N = 100000;
const int NIL = -1;

int n;
vector<int> G[N];
int color[N];

void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    color[r] = c;

    while(!S.empty()){
        int x = S.top(); S.pop();
        for(int i = 0; i < G[x].size(); i++){
            int v = G[x][i];
            if(color[v] == NIL){
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void solve(){
    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 q, s, t;
    cin >> n >> q;

    while(q-- > 0){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    solve();

    cin >> q;
    while(q-- > 0){
        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;
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.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 i,u,id=1;
  for(i=0;i<n;i++) color[i] = NIL;
  for(u=0;u<n;u++){
    if(color[u]==NIL) bfs(u,id++);
  }
}

int main(){
  int i,s,t,m,q;
 cin >> n >> m;
  for(i=0;i<m;i++){
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

cin >> q;
  for(i=0;i<q;i++){
    cin >> s >> t;
    if(color[s]==color[t]){
 cout << ?¨yes?¨ <<endl;
    }
    else cout << ?¨no?¨ <<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_set>
#include <vector>

int main(){
    int n;
    int m;
    std::scanf("%d", &n);
    std::scanf("%d", &m);
    std::vector<std::unordered_set<int> > relations;
    for(int i = 0; i < m; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        int finds = -1;
        int findt = -1;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end()){
                finds = j;
                break;
            }
        }
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(t) != relations[j].end()){
                findt = j;
                break;
            }
        }
        if(finds == -1 && findt == -1){
            std::unordered_set<int> set;
            set.insert(s);
            set.insert(t);
            relations.push_back(set);
        } else if(findt == -1){
            relations[finds].insert(t);
        } else if(finds == -1){
            relations[findt].insert(s);
        } else if(finds != findt){
            int smaller = std::min(finds, findt);
            int larger = std::max(finds, findt);
            for(auto &x: relations[larger]){
                relations[smaller].insert(x);
            }
            relations[larger] = relations.back();
            relations.pop_back();
        }
    }
    int q;
    std::scanf("%d", &q);
    for(int i = 0; i < q; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        bool find = false;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end() && relations[j].find(t) != relations[j].end()){
                find = true;
		break;
            }
        }
        if(find){
            std::printf("yes\n");
        } else {
            std::printf("no\n");
        }
    }

    return 0;
}

C++: #include<stdio.h>
#include<queue>

#define MAX 10000

int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;

bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;

    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }

    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }

    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        bfs(s, d) ? printf("yes\n") : printf("no\n");
    }

    return 0;
}