Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

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 <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
using Graph = vector<vector<ll>>;

ll n;
vector<ll> fri[100100];
int color[100100];

void dfs(int r, int c){
  stack<ll> s;
  s.push(r);
  color[r] = c;
  while(!s.empty()){
    int u = s.top(); s.pop();
    rep(i, 0, fri[u].size()){
      int v = fri[u][i];
      if(color[v] == -1){
        color[v] = c;
        s.push(v);
      }
    }
  }
}

void assignColor(){
  int id = 1;
  rep(i, 0, n) color[i] = -1;
  rep(u, 0, n){
    if(color[u] == -1) dfs(u, id++);
  }
}

int main() {
  ll m; cin >> n >> m;
  
  rep(i, 0, m){
    ll a, b; cin >> a >> b;
    fri[a].push_back(b);
    fri[b].push_back(a);
  }

  assignColor();

  ll q; cin >> q;
  rep(i, 0, q){
    ll a, b; cin >> a >> b;
    if(color[a] == color[b]) puts("yes");
    else puts("no");
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#define P pair<int, int>

using namespace std;

int main(){
  int n, m, q, c[100009];
  vector<int> G[100009];
  queue<int> Q;
  memset(c, -1, sizeof(c));
  cin >> n >> m;
  for(int i=0;i<m;i++){
    int s, e;
    cin >> s >> e;
    G[s].push_back(e);
    G[e].push_back(s);
  }
  for(int i=0;i<n;i++){
    if(c[i]!=-1)continue;
    c[i] = i;
    Q.push(i);
    while(!Q.empty()){
      int node = Q.front();Q.pop();
      for(int j=0;j<G[node].size();j++){
        if(c[G[node][j]]==-1){
          c[G[node][j]] = i;
          Q.push(G[node][j]);
        }
      }
    }
  }
  cin >> q;
  for(int i=0;i<q;i++){
    int s, e;cin >> s >> e;
    cout << (c[s] == c[e]? "yes": "no") << endl;
  }
}

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<uint> root_;
  
public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

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

  void setup()
  {
    std::fill(root_.begin(), root_.end(), UINT_MAX);
    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] != UINT_MAX) 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(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 <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG true
// sort(a.begin(), a.end(), std::greater<int>());

/*
std::vector<std::string> split(const std::string &input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter))
    {
        result.push_back(field);
    }
    return result;
}
*/
pair<int, int> data[100000];
bool finds[100000];
int n, m;
/*
int dfs(int foo, int timer)
{
    //foo is node number
    if (finds[foo].first != -1)
        return timer;
    finds[foo].first = ++timer;
    int result = timer;
    if (data[foo].size() != 0)
    {
        rep(i, data[foo].size())
        {
            result = dfs(data[foo][i], result);
        }
    }
    finds[foo].second = ++result;
    return result;
}
*/

bool dfs(int foo, int goal)
{
    bool ans = false;
    rep(i, m)
    {
        if (data[i].first == foo && data[i].second == goal)
            return true;
        if (data[i].first == foo)
        {
            if (finds[i] == true)
                continue;
            finds[i] = true;
            ans = dfs(data[i].second, goal);
        }
    }
    return ans;
}
int main()
{
    cin >> n >> m;
    rep(i, m)
    {
        cin >> data[i].first >> data[i].second;
    }
    int q;
    cin >> q;
    rep(i, q)
    {
        int left, right;
        cin >> left >> right;
        if (dfs(left, right) || dfs(right, left))
            cout << "yes";
        else
            cout << "no";
        cout << endl;
        rep(j, m) finds[j] = false;
    }
    return 0;
}