Go: // AOJ ALDS1_11_D: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D

package main

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

var nocolor = -1

type graph struct {
	graph  [][]int
	colors []int
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func newGraph(nodeNum int) *graph {
	colors := make([]int, nodeNum)
	g := make([][]int, nodeNum)
	for i := 0; i < nodeNum; i++ {
		g[i] = []int{}
		colors[i] = nocolor
	}

	return &graph{g, colors}
}

func constructGraph(sc *bufio.Scanner) *graph {
	usersNum := nextInt(sc)
	linkNum := nextInt(sc)

	g := newGraph(usersNum)
	for i := 0; i < linkNum; i++ {
		u := nextInt(sc) // node
		l := nextInt(sc) // link node
		// edge each other
		g.graph[u] = append(g.graph[u], l)
		g.graph[l] = append(g.graph[l], u)
	}
	return g
}

func (g *graph) dfs(r, color int) {
	g.colors[r] = color

	stack := list.New()
	stack.PushFront(r)

	for stack.Len() > 0 {
		v := stack.Remove(stack.Front()).(int)
		for _, u := range g.graph[v] {
			if g.colors[u] == nocolor {
				g.colors[u] = color
				stack.PushFront(u)
			}
		}
	}
}

func (g *graph) assignColor() {
	color := 1
	for u := 0; u < len(g.graph); u++ {
		if g.colors[u] == nocolor {
			g.dfs(u, color)
			color++
		}
	}
}

func (g *graph) isLink(s, t int) bool {
	return g.colors[s] == g.colors[t]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// construct graph and friendly colors
	g := constructGraph(sc)
	g.assignColor()

	// judge link
	q := nextInt(sc)
	for i := 0; i < q; i++ {
		s := nextInt(sc)
		t := nextInt(sc)

		if g.isLink(s, t) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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 + 1;

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){ // ??????r?????\?¶????????????????????????????c??§??????
	stack<int> S;
	S.push(r);
	color[r] = c; // ??????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){ // ???????????????u??¨??£??\????????????????????????????????£??????????????£???????????????u??¨??????????????????
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor(){ // ????????????????????????
	int id = 1; // ????????´??°???????????????????????\??????????????????1???????§???????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, /* ??????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<algorithm>
#include<vector>
#include<queue>
#define MAX 100001
using namespace std;
int n;
int color[MAX] = {0};
vector<int> number[MAX];
void Connected_Components(void){
    int col = 0;
    for(int i=0;i<n;i++){
        if(color[i]==0){
            queue<int> Number_of_vertices;
            Number_of_vertices.push(i);
            color[i] = ++col;
            while(!Number_of_vertices.empty()){
                int v = Number_of_vertices.front();
                Number_of_vertices.pop();
                //cout << v << endl;
                for(int j=0;j<number[v].size();j++){
                    if(color[number[v][j]]==0){
                        color[number[v][j]] = col;
                        //cout << color[j] << endl;
                        Number_of_vertices.push(number[v][j]);
                    }
                }
            }
        }
    }
}
void input(void){
    int m;
    cin >> n >> m;
    for(int i=0;i<n;i++){
        number[i].push_back(i);
    }
    for(int i=0;i<m;i++){
        int id,fre;
        cin >> id >> fre;
        number[id].push_back(fre); 
        number[fre].push_back(id);
    }
    // for(int i=0;i<n;i++){
    //     for(int j=0;j<number[i].size();j++){
    //         cout << number[i][j] << ' ';
    //     }
    //     cout << endl;
    // }
}
void question(void){
    int q;
    cin >> q;
    for(int i=0;i<q;i++){
        int id_1,id_2;
        cin >> id_1 >> id_2;
        if(color[id_1]==color[id_2]){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
}
int main(void){
    input();
    Connected_Components();
    question();
    return 0;
}