Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <queue>
using namespace std;

const int Max = 100005;
int color[Max], nt[Max];
vector<int> G[Max];
int n;
/*
int next(int u)
{
    nt[u]++;
    if(nt[u]<G[u].size())
        return G[u][nt[u]];
    else
        return -1;
}
*/
void dfs(int s, int t)
{

     //memset(nt, -1, sizeof(nt));
     stack<int> S;
     S.push(s);
     color[s] = t;
     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]==-1)
             {
                 color[v] = t;
                 S.push(v);
             }
         }
     }
     /*
     while(!S.empty())
     {
         int u = S.top();
         int v = next(u);
         if(v!=-1)
         {
             if(v==t) return 1;
             if(color[v]==-1)
             {
                 color[v] = 0;
                 S.push(v);
             }
         }
         else
         {
             S.pop();
         }
     }
     */
     //return 0;
}

void assignColor()
{
    int id = 1;
    memset(color, -1, sizeof(color));
    for(int i=0; i<n; i++)
    {
        if(color[i]==-1)
            dfs(i, id++);
    }
}

int main()
{
    int m, q, s, t;
    scanf("%d %d", &n, &m);
    for(int i=0; i<m; i++)
    {
        scanf("%d %d", &s, &t);
        G[s].push_back(t);//邻接表
        G[t].push_back(s);
    }
    scanf("%d", &q);
    assignColor();
    for(int i=0; i<q; i++)
    {
        scanf("%d %d", &s, &t);
        if(color[s]==color[t])
            printf("yes\n");
        else
            printf("no\n");
        /*
        if(dfs(s, t))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
            */
    }

    return 0;
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <cstring>
#include <queue>
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] = { [0 ... kMaxUsers-1] = kInvalid }; // 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++)
    g_group[i] = kInvalid;

  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 <bits/stdc++.h>
using namespace std;
int main(){
  int n, m;
  cin >> n >> m;
  vector<set<int>> E(n);
  for (int i = 0; i < m; i++){
    int s, t;
    cin >> s >> t;
    E[s].insert(t);
    E[t].insert(s);
  }
  vector<int> G(n, -1);
  for (int i = 0; i < n; i++){
    if (G[i] == -1){
      queue<int> Q;
      Q.push(i);
      G[i] = i;
      while (!Q.empty()){
        int v = Q.front();
        Q.pop();
        for (int w : E[v]){
          if (G[w] == -1){
            G[w] = G[v];
            Q.push(w);
          }
        }
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++){
    int s, t;
    cin >> s >> t;
    if (G[s] == G[t]){
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}