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 <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <cstring>
#include <queue>
#include <map>
#include <utility>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


const int kInvalid = -1;
const int kMaxUsers = 100 * 1000;

int g_group[kMaxUsers]; // 400 KiB

// 100 K * sizeof(vector<int>) + 100 K * sizeof(int)
// = 2.4 MiB + 100 K * sizeof(int)
vector<int> g_list[kMaxUsers];

void setRelations(int start, int end)
{
  g_list[start].push_back(end);
  g_list[end].push_back(start);
}


void doGrouptize()
{
  // debug info
  DPRINTF("size of g_group[] %lu Byte\n", sizeof(g_group));
  DPRINTF("size of g_list[]  %lu Byte\n", sizeof(g_list));

  // init
  int groupId = 0;
  memset(g_group, kInvalid, kMaxUsers);

  for (int i = 0; i < kMaxUsers; i++)
  {
    if (g_group[i] != kInvalid)
      continue;

    g_group[i] = groupId;

    // grouptize connected users in BFS order
    queue<int> que;
    que.push(i);

    while (!que.empty())
    {
      const int current = que.front();
      que.pop();

      for (int j = 0; j < g_list[current].size(); j++)
      {
        const int next = g_list[current][j];

        if (g_group[next] != kInvalid)
          continue;

        g_group[next] = groupId;
        que.push(next);
      }
    }

    groupId++;
  }
}


void printAllRelations()
{
#if DEBUG
  for (int i = 0; i < kMaxUsers; i++)
  {
    for (int j = 0; j < g_list[i].size(); j++)
    {
      if (j == 0)
        DPRINTF("%d", i);

      printf(" %d", g_list[i][j]);

      if (j == g_list[i].size() - 1)
        cout << endl;
    }
  }
#endif // DEBUG
}


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

  DPRINTF("number of users %d\n", n);
  DPRINTF("number of relations %d\n", m);

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

    setRelations(s, t);
  }

  printAllRelations();

  doGrouptize();

  int q;
  cin >> q; cin.ignore();

  for (int i = 0; i < q; i++)
  {
    int s, t;
    cin >> s >> t; cin.ignore();

    DPRINTF("src %d  dst %d\n", s, t);

    if (g_group[s] == g_group[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;
}

C++: // C++ 14
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)

using namespace std;

void dfs_color(vector<Int> vs[], Int colors[], Int n) {
    bool visited[n];
    loop(i,0,n)  {
        visited[i] = false;
        colors[i] = 0;
    }
    
    stack<Int> S;
    
    loop(i,0,n) {
        Int color = i + 1;
        S.push(i);
        visited[i] = true;
        if (colors[i] == 0) colors[i] = color;
        
out_of_while:
        while (!S.empty()) {
            Int i = S.top();
            for (auto c: vs[i]) {
                if (visited[c]) continue;
                S.push(c);
                visited[c] = true;
                if (colors[c] == 0) colors[c] = color;
                goto out_of_while;
            }
            
            S.pop();
        }
    }
}


int main(void) {
    Int n, m, q, v, x;
    cin >> n >> m;
    vector<Int> vs[n];
    Int colors[n];
    
    loop(i,0,m) {
        cin >> v >> x;
        vs[v].push_back(x);
        vs[x].push_back(v);
    } 
    
    dfs_color(vs, colors, n);
    
    cin >> q;
    loop(i,0,q) {
        cin >> v >> x;
        if (colors[v] == colors[x]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define maxn 10005
#define NTL -1
int color[maxn];
vector<int> g[maxn];
stack<int> s;
int n,m,q;
void tarjan(int r,int c)
{
    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]==NTL)
            {
                color[v]=c;
                s.push(v);
            }
        }
    }
}
void assigncolor()
{
    int id=0;
    for(int i=0;i<n;i++)
        color[i]=NTL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NTL)
            tarjan(u,id++);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    assigncolor();
    scanf("%d",&q);
    while(q--)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        if(color[u]==color[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}