Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		printf("%d,", g->id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}

C++: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20180319_AOJ
{
    class ALDS1_11D
    {
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int[] n_m = line.Split(' ').Select(int.Parse).ToArray();
                int n = n_m[0];
                int m = n_m[1];
                int[,] matrix = new int[n, n];

                for (int i = 0; i < m; i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    matrix[s_t[0], s_t[1]] = 1;
                    matrix[s_t[1], s_t[0]] = 1;
                }

                int[] group = new int[n];
                int gn = 0;
                for (int i = 0; i < n; i++)
                {
                    if (group[i] == 0)
                    {
                        gn++;
                        group[i] = gn;
                    }

                    gn = group[i];
                    for (int t = 0; t < n; t++)
                    {
                        if (matrix[i, t] == 1)
                        {
                            int tn = group[t];
                            if (tn > 0)
                            {
                                Replace(group, tn, gn);
                            }
                            else
                            {
                                group[t] = gn;
                            }
                        }
                    }
                }

                int q = int.Parse(Console.ReadLine());
                for (int i = 0; i < q; i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    if (group[s_t[0]] == group[s_t[1]])
                    {
                        Console.WriteLine("yes");
                    }
                    else
                    {
                        Console.WriteLine("no");
                    }
                }
            }
        }

        static void Replace<T>(T[] arr, T source, T dest)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].Equals(source))
                {
                    arr[i] = dest;
                }
            }
        }
    }
}

C++: /* ALDS1_11_D: Connected Components */

#include <iostream>
#include <vector>
using std::vector;

bool is_connectable(vector<int> sns[], int id, int goal_id) {
    vector<int> friends = sns[id];
    for (int i = 0; i < friends.size(); i++) {
        int friend_id = friends[i];
        if (friend_id == goal_id || is_connectable(sns, friend_id, goal_id))
            return true;
    }
    return false;
}

int main() {
    std::ios::sync_with_stdio(false);

    int n_users, n_relations;
    std::cin >> n_users >> n_relations;

    int id, friend_id, n_friends;
    vector<int> *sns = new vector<int>[n_users];
    for (int i = 0; i < n_relations; i++) {
        std::cin >> id >> friend_id;
        sns[id].push_back(friend_id);
    }

    int n_questions;
    std::cin >> n_questions;

    for (int i = 0; i < n_questions; i++) {
        std::cin >> id >> friend_id;
        bool connectable = is_connectable(sns, id, friend_id);
        std::cout << ( connectable ? "yes" : "no" ) << "\n";
    }
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>
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;
}