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 <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int WHITE = 0, GREY = 1, BLACK = 2;

struct CC {
    int n;
    vector<vector<int>> g;
    vector<int> color;
    vector<int> component;
    int cId = 0; //component id
    
    CC (int n) : n(n) {
        g.assign(n, vector<int>(0));
        color.assign(n, WHITE);
        component.assign(n, -1);
    }
    
    void addEdge (int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    
    void solve () {
        for (int i = 0; i < n; i++) {
            if (color[i] == WHITE) {
                cId++;
                bfs(i);
            }
        }
    }
    
    void bfs (int s) {
        queue<int> q;
        q.push(s);
        color[s] = GREY;
        component[s] = cId;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int v : g[u]) {
                if (color[v] == WHITE) {
                    q.push(v);
                    color[v] = GREY;
                    component[v] = cId;
                }
            }
        }
    }
};

int main() {
    
    int n, m;
    cin >> n >> m;
    CC cc(n);
    
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        cc.addEdge(u, v);
    }
    
    cc.solve();
    
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        if (cc.component[u] == cc.component[v]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 100000
#define 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>

using namespace std;
//int graph[100005][100005]; <-CompileError????????????????????????
vector<int> graph[100005];
int d[100005];
int color[100005];	
void bfs(int s, int n, int gpc){
	queue<int> Q;
	for(int i=0;i<n;i++){
		d[i] = 2e9;
	}
	color[s] = -1;
	d[s] = 0;
	Q.push(s);
	while(Q.size()!=0){
		int u = Q.front();
		Q.pop();
		for(int i=0;i<(int)graph[u].size();i++){
			int v = graph[u][i];
			if(color[v] == -2){
				color[v] = -1;
				d[v] = d[u] +1;
				Q.push(v);
			}
		}
		color[u] = gpc;
	}
}
void check(int n){
	int group = 0;
	for(int i=0;i<n;i++) color[i] = -2;
	for(int u=0;u<n;u++){
		if(color[u] == -2)bfs(u, n, group++);
	}
}

int main(){
	int n, m;
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int num;
		int tmp;
		cin >> num >> tmp;
		graph[num].push_back(tmp);
		graph[tmp].push_back(num);
	}
	check(n);
	int q;
	cin >> q;
	for(int i=0;i<q;i++){
		int s, t;
		cin >> s >> t;
		if(color[s] == color[t]) cout << "yes"<<endl;
		else cout <<"no" << endl;
	}
}

C++: #include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//_____
#define pb push_back
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
//_______

vector <int> G[100005];
int group[100005];
int color = 1;

void dfs(int u)
{
   group[u] = color;

   rep(i, G[u].size())
      if( group[ G[u][i] ] == 0 )dfs( G[u][i] );
}

signed main()
{
   FAST();
   int N, M;
   cin >> N >> M;

   rep(i, M){
      int a,b;
      cin >> a >> b;
      G[a].pb(b);
      G[b].pb(a);
   }

   rep(i, N){
      if(group[i] == 0 )dfs(i);
      ++color;
   }

   int Q;
   cin >> Q;
   while(Q--){
      int a,b;
      cin >> a >> b;
      outl( group[a] == group[b] ? "yes" : "no" );
   }
}

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