Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}

vi par;
vi ran;

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

void unite(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) return;
	if (ran[x] < ran[y]) {
		par[x] = y;
	}else {
		par[y] = x;
		if (ran[x] == ran[y]) ran[x]++;
	}
}

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


int main() {
	int n, m,a,b;
	cin >> n >> m;
	par.resize(n);
	rep(i, n) {
		par[i] = i;
	}
	ran.resize(n,0);
	rep(i, m) {
		cin >> a >> b;
		unite(a, b);
	}
	cin >> m;
	rep(i, m) {
		cin >> a >> b;
		cout << (same(a, b) ? "yes" : "no") << endl;
	}
}

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

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[100000][100];
    int *dist;

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);

	dist = new int [n];

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

	delete [] dist;

    return 0;
}

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

vector<int> visit;

bool bfs(int s, int t, vector< vector<int> > list) {
  if (s == t) {
    return true;
  }
  
  // visit flag
  visit[s] = 1;
  
  for (int i = 0; i < list[s].size(); i++) {
    int next = list[s][i];
    if (visit[next] == 0) {
      if (bfs(next, t, list)) {
        return true;
      }
    }
  }
  return false;
}

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>());
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    visit = vector<int>(n,0);
    if (bfs(s,t,list)) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}