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++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
vector<vector<int>> G;
vector<int> C;
int color, n, m;

void dfs(int r) {
  C.at(r) = ++color;
  stack<int> S;
  S.push(r);
  while (S.size() > 0) {
    int u = S.top(); S.pop();
    rep(i, G.at(u).size()) {
      if (C.at(G.at(u).at(i)) == -1) {
        C.at(G.at(u).at(i)) = color;
        S.push(G.at(u).at(i));
      } 
    }
  }
}

void assing_color() {
  rep(i, n) {
    if (C.at(i) == -1){
      dfs(i);
    }
  }
}

int main() {
  cin >> n >> m;
  rep(i, n) G.push_back({});
  rep(i, n) C.push_back(-1);
  rep(i, m) {
    int a, b; cin >> a >> b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }
  assing_color();
  int q; cin >> q;
  rep(i, q) {
    int a, b; cin >> a >> b;
    if(C.at(a) == C.at(b)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Node{
private:
	int id;
	int friend_num;
	Node **friends;
	int check_id[2];
public:
	Node(int id, int n);
	~Node(){ delete[] friends; }
	void addFriend(Node *friend_node);
	bool searchFriend(int s_id, int t_id);
};

Node::Node(int id, int n)
{
	this->id = id;
	friend_num = 0;
	friends = new Node*[n];
	check_id[0] = check_id[1] = -1;
}

void Node::addFriend(Node *friend_node)
{
	friends[friend_num] = friend_node;
	friend_num++;
}

bool Node::searchFriend(int s_id, int t_id)
{
	if (check_id[0] == s_id && check_id[1] == t_id) return false;
	check_id[0] = s_id;
	check_id[1] = t_id;

	if (this->id == t_id) return true;

	for (int i = 0; i < friend_num; i++) {
		if (friends[i]->searchFriend(s_id, t_id)) return true;
	}

	return false;
}

int main(void)
{
	int n;
	cin >> n;	
	Node **node = new Node*[n];
	for (int i = 0; i < n; i++) node[i] = new Node(i, n);

	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		node[s]->addFriend(node[t]);
		node[t]->addFriend(node[s]);
	}
		
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		bool result = node[s]->searchFriend(s, t);
		if (result) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	delete[] node;
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>


using namespace std;

int *passed;

bool do_check(int s, int t, vector< vector<int> > a){
  queue<int> q;
  
  bool flag = false;
  if(s==t){flag=true;}
  q.push(s);
  while(q.size()>0 && flag==false){
    s = q.front();
    q.pop();
    for(int i=0;i<a[s].size();i++){
      int tmp = a[s].at(i);
      if(tmp == t){
	flag = true;
      }
      if(passed[tmp] == 0){
	passed[tmp] = 1;
	q.push(tmp);
      }
    }
  }
  return flag;
}

int main(){
  int n, m;
  cin >> n >> m;
  passed = new int[n];
  vector< vector<int> > a;

  for(int i=0;i<n;i++){
    vector<int> tmp;
    a.push_back(tmp);
  }

  for(int i=0;i<m;i++){
    int tmpa, tmp;
    cin >> tmpa >> tmp;
    a[tmpa].push_back(tmp);
    a[tmp].push_back(tmpa);
  }

  // Questions
  int q_num;
  cin >> q_num;
  // cout << "q_num: " << q_num << endl;
  for(int i=0;i<q_num;i++){
    for(int j=0;j<n;j++){
      passed[j]=0;
    }
    int s, t;
    cin >> s >> t;

    // cout << s << ": " << do_check(s, t, a) << endl;
    if(do_check(s, t, a)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------


int n, m, q;
vector<int> g[100005];
int col[100005];

void dfs(int r, int c)
{
	stack<int> stk;
	stk.push(r);
	col[r] = c;

	while (!stk.empty()) {
		int u = stk.top(); stk.pop();
		for (int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if (col[v] == -1) {
				stk.push(v);
				col[v] = c;
			}
		}

	}
}

void solve()
{
	int c = 0;

	for (int i = 0; i < n; i++) {
		col[i] = -1;
	}

	for (int i = 0; i < n; i++) {
		if (col[i] == -1) {
			dfs(i, ++c);
		}
	}
}


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

	return 0;
}

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

int n, m;
stack<int> S;
vector<int> V[100000];
int color[100000];

void dfs(int i, int id){
    S.push(i);
    color[i] = id;

    while(!S.empty()){
        int u = S.top();
        S.pop();
        for(int i=0; i<V[u].size(); i++){
            int v = V[u][i];
            if(color[v]==-1){
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void dfs_init(){

    int id = 1; //グラフの識別
    for(int i=0; i<n; i++){
        color[i] = -1;
    }
    for(int i=0; i<n; i++){
        if(color[i]==-1) dfs(i, id++);
    }
}

int main(){

    cin >> n >> m;

    for(int i=0; i<m; i++){
        int a, b;
        cin >> a >> b;
        V[a].push_back(b);
        V[b].push_back(a);
    }

    dfs_init();

    int q;
    cin >> q;
    for(int i=0; i<q; i++){
    int x, y;
    cin >> x >> y;
    if(color[x]==color[y]) cout << "yes" << endl;
    else cout << "no" << endl;
    }

    return 0;
}