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 <iostream>
#include <string>
#include <vector>

using namespace std;

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

// 連結成分(Connected Components)

vector<int> G[N];
int color[N] = { NIL };

void depthFirstSeach(int u, int colorGroup)
{
    color[u] = colorGroup;
    //頂点vにつながっている頂点v
    for (int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if (color[v] == NIL)
            depthFirstSeach(v, colorGroup);
    }
}


int main(int argc, char *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    // 1行目は、ユーザー数n, 関係の数m
    int n, m;
    cin >> n >> m;
    // 2行目から、友達関係をm個
    for (int i = 0; i < m; i++)
    {
        int s, t; // sとtが友達
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

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

    int colorGroup = 1;
    for (int u = 0; u < n; u++)
    {
        // 色がついていないノード(color=NIL)に色を付ける。
        // 深さ優先探索で、たどり着けるところまで色を付ける。
        // これをNILがなくなるまで続けることで色分けできる。
        if (color[u] == NIL) depthFirstSeach(u, colorGroup++);
    }

    // 質問の数q
    int q;
    cin >> q;
    for (int i = 0; i< q; i++)
    {
        int qs, qt; // qsからqtにたどり着けるか?
        cin >> qs >> qt;
        cout << ((color[qs] == color[qt]) ? "yes" : "no") << endl;
    }

    return 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 <stack>
using namespace std;
typedef long long ll;
static const ll MAX = 110000;
static const ll NIL = -1;

ll n;
vector<ll> G[MAX];
ll color[MAX];

void dfs(int r, int c) {
  stack<ll> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    ll u = S.top(); S.pop();
    for (ll i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assignColor() {
  ll id = 1;
  for (ll i = 0; i < n; i++) color[i] = NIL;
  for (ll u = 0; u < n; u++) {
    if (color[u] == NIL) dfs(u, id++);
  }
}

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

  assignColor();

  cin >> q;

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

C++: #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <stack>
#define rep(i, n) for(int i=0;i<n;i++)
#define NOT_FOUND 0
#define ENQUEUED 1
using namespace std;

vector<int> graph[100001];
int memo[100001];

int dfs(int start, int end) {
    if (start == end) {
        return 1;
    }
    memset(memo, NOT_FOUND, sizeof(memo));
    stack<int> stack;
    stack.push(start);
    memo[start] = ENQUEUED;
    int counter = 0;

    while(!stack.empty() && counter != 10) {
        counter += 1;
        int v = stack.top();
        stack.pop();
        vector<int> edges = graph[v];

        for (int i = 0; i < edges.size(); i++) {
            int next = edges[i];
            if (next == end) {
                return 1;
            }
            if (memo[next] == NOT_FOUND) {
                memo[next] = ENQUEUED;
                stack.push(next);
            }
        }
    }
    return 0;
}

int main() {
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int v, e;
        cin >> v >> e;
        graph[v].push_back(e);
        graph[e].push_back(v);
    }

    int q;
    cin >> q;

    for (int i = 0; i < q; i++) {
        int s, d;
        cin >> s >> d;
        if (dfs(s, d)) {
            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.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<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;
}