Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

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

#define MAX 100000

//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;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif

void bfs(std::vector<std::vector<int> > &G, std::vector<int> &component_number, int i, int c){
	std::queue<int> que;

	que.push(i);
	component_number[i] = c;
	while(!que.empty()){
		int n = que.front(); que.pop();
		
		for(int j = 0; j < G[n].size(); j++){
			int nn = G[n][j];
			if(component_number[nn] < 0){
				component_number[nn] = c;
				que.push(nn);
			}
		}
	}
	return;
}


int main(int argc, char **argv){
	int n, m, u, v, q, s, t;
	std::vector<std::vector<int> > G;
	std::vector<int> component_number;
	int c = 0;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	ifs >> n >> m;
#else
	std::cin >> n >> m;
#endif

	G.resize(n);
	component_number.resize(n, -1);
	for(int i = 0; i < m; i++){
#if INPUT_FROM_FILE
		ifs >> u >> v;
#else
		std::cin >> u >> v;
#endif
		G[u].push_back(v);
		G[v].push_back(u);
	}

	for(int i = 0; i < n; i++){
		if(component_number[i] < 0){
			bfs(G, component_number, i, c);
			c++;
		}
	}

#if INPUT_FROM_FILE
	ifs >> q;
#else
	std::cin >> q;
#endif
	for(int i = 0; i < q; i++){
#if INPUT_FROM_FILE
		ifs >> s >> t;
#else
		std::cin >> s >> t;
#endif
		if(component_number[s] == component_number[t]){
			std::cout << "yes" << std::endl;
		}
		else{
			std::cout << "no" << std::endl;
		}
	}
	return 0;
}

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();
	for (int i = 0; i < n; i++)
		cout << vis[i] << endl;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		if (vis[ind] == vis[tmp])
		cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

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

int fri[100010];

int find(int x){
  if( fri[x] == x ) return x;
  else return fri[x] = find(fri[x]);
}

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

void uf_union(int x, int y){
  x = find(x);
  y = find(y);
  
  fri[x] = y;

}

int main(){

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

  // init
  for(int i=0; i<n; i++) fri[i] = i;

  //
  for(int i=0; i<m; i++){
    int u,v; scanf("%d %d",&u,&v);
    uf_union(u,v);
  }

  cin >> q;
  for(int i=0; i<q; i++){
    int u,v; scanf("%d %d",&u,&v);
    if( find(u) == find(v) ) cout << "yes\n";
    else cout << "no\n";
  }


}

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_D: Connected Components

#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>

using uint = unsigned int;

class Graph {
  std::vector<std::vector<uint>> g_;
  std::vector<int> root_;
  
public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

  void add_edge(uint from, uint to)
  {
    if (from < to)
      g_[from].push_back(to);
    else
      g_[to].push_back(from);
  }

  void setup()
  {
    std::fill(root_.begin(), root_.end(), -1);
    for (auto i = 0u; i < g_.size(); ++i ) {
      dfs(i, i);
    }
  }

  bool reachable(uint from, uint to) const
  {
    return root_[from] == root_[to];
  }

private:
  void dfs(uint i, uint root)
  {
    if (root_[i] >= 0) return;
    root_[i] = root;
    for (auto j = 0u; j < g_[i].size(); ++j) {
      dfs(g_[i][j], root);
    }
  }
  
};

int main()
{
  uint n, m;
  std::scanf("%u %u", &n, &m);

  Graph g(n);

  uint s, t;
  for (auto i = 0u; i < m; ++i) {
    std::scanf("%u %u", &s, &t);
    g.add_edge(s, t);
  }
  g.setup();

  uint q;
  std::scanf("%u", &q);
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%u %u", &s, &t);
    std::puts(g.reachable(s, t) ? "yes" : "no");
  }
}

// eof

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

int x[100000][100];
bool y[100000];
int z[100000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][z[a]]=b;
		x[b][z[b]]=a;
		z[a]++;
		z[b]++;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<40;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<z[j];k++){
						y[x[j][k]]=true;
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}