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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define  all(x) (x).begin(),(x).end()
typedef long long ll;

vector<int> frnd[100005];
vector<int> group(100005, 0);

void dfs(int node, int parentNode, int groupId){
    group[node] = groupId;
    for(int x: frnd[node]){
        if (x == parentNode) continue;
        if (group[x] != 0) continue;
        dfs(x, node, groupId);
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    rep(i,m){
        int s, t;
        cin >> s >> t;
        frnd[s].push_back(t);
        frnd[t].push_back(s);
    }

    int gropuId = 1;
    rep(i,n){
        if (group[i] != 0) continue;
        dfs(i, -1, gropuId);
        gropuId++;
    }

    int q;
    cin >> q;
    vector<string> ans(q);
    rep(i,q){
        int s, t;
        cin >> s >> t;
        if (group[s] == group[t]) ans[i] = "yes";
        else ans[i] = "no";
    }
    rep(i,q){
        cout << ans[i] << endl;
    }
}

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

#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>

using uint = unsigned int;

class Graph {
  std::vector<std::vector<uint>> g_;
  std::vector<int> root_;
  enum { NIL = -1, IN_STACK= -2 };

public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

  void add(uint from, uint to)
  {
    g_[from].push_back(to);
    g_[to].push_back(from);
  }

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

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

private:
  void dfs(uint root)
  {
    std::stack<uint> s;
    s.push(root);
    while (!s.empty()) {
      auto i = s.top();
      s.pop();
      root_[i] = root;
      for (auto j : g_[i]) {
        if (root_[j] == NIL) {
          root_[j] = IN_STACK;
          s.push(j);
        }
      }
    }
  }
};

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(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<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 100000

int d[MAX];
vector<int> V[MAX];
int tt=0,n,m;
queue<int> Q;

void gogo(int x){
  Q.push(x);
  d[x]=tt;
  int v;

  while(!Q.empty()){
    v=Q.front(); Q.pop();
    for(int i=0;i<V[v].size();i++){
      if(d[V[v][i]]==-1) {
        Q.push(V[v][i]);
        d[V[v][i]]=d[v];

      }
    }
  }

}

void gostart(){
  for(int i=0;i<n;i++){
    if(d[i]==-1) {
      gogo(i);
      tt++;
    }
  }
}

int main()  {
  cin>>n>>m;
  for(int i=0;i<n;i++){
    d[i]=-1;
  }

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

  gostart();

  int q;
  cin>>q;
  int k,l;
  for(int i=0;i<q;i++){
    cin>>k>>l;
    if(d[k]==d[l]) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> Cluster;
	int n;

	Graph(int n);

	bool isConnected(int s, int t);

	void MakeCluster();

	void Link(int s, int t);

};

Graph::Graph(int n){
	A.resize(n);
	Cluster.resize(n);
	for (int i = 0; i < n; i++) Cluster[i] = 0;
	this->n = n;
}

void Graph::Link(int s, int t){
	A[s].push_back(t);
	A[t].push_back(s);
	return;
}

void Graph::MakeCluster(){
	int clusternum = 0;
	std::list<int> Q;
	for (int i = 0; i < n; i++){
		if (Cluster[i] == 0){
			Q.clear();
			clusternum++;
			Cluster[i] = clusternum;
			Q.push_back(i);
			bool res = false;
			while (!Q.empty()){
				int f = Q.front();
				Q.pop_front();
				for (std::vector<int>::iterator itr = A[f].begin(); itr != A[f].end(); itr++){
					if (Cluster[*itr] == false){
						Cluster[*itr] = clusternum;
						Q.push_back(*itr);
					}
				}
			}
		}
	}
	return;
}

bool Graph::isConnected(int s, int t){
	return Cluster[s] == Cluster[t];
}

int main(){
	int n, m, s, t, q;
	scanf("%d%d", &n, &m);
	Graph G(n);
	for (int i = 0; i < m; i++){
		scanf("%d%d", &s, &t);
		G.Link(s, t);
	}
	scanf("%d", &q);
	G.MakeCluster();
	for (int i = 0; i < q; i++){
		scanf("%d%d", &s, &t);
		if (G.isConnected(s, t)) printf("yes\n"); else printf("no\n");
	}
	
	return 0;
}

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

int a[100000];
int search(int k){
  if(a[k]==k){
      return k;
  }
  int p=search(a[k]);
  a[k]=p;
  return p;
}

int main(){
  for(int i=0;i<100000;i++){
    a[i]=i;
  }
  int n,m,p,q,r,s,t;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>p>>q;
    a[search(p)]=search(q);
  }
  cin>>r;
  for(int i=0;i<r;i++){
    cin>>s>>t;
    if(search(s)==search(t)){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty()){
        que.pop();
    }
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

    for(int i=0; i<m; i++){
        cin >> s >> t;

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(x==t){
                answer[i]=true;
                break;
            }

            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);
            }
         }

          que.pop();
        }

        que=refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}