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 <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;

using Graph = vector<int>;
int main() {
    int n, m;
    cin >> n >> m;
    vector<Graph>g(n);
    vector<int> color(n, -1);
    for (int i=0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        g[s].emplace_back(t);
        g[t].emplace_back(s);
    }

    int num = 0;
    for (int i=0; i<n; i++) {
        num++;
        queue<int> que;
        if (color[i] != -1) {
            continue;
        }

        que.push(i);
        while(!que.empty()) {
            int s = que.front();
            que.pop();
            Graph G = g[s];
            for (int j=0; j<G.size(); j++) {
                int n = G[j];
                if (color[n] != -1) {
                    continue;
                }

                color[n] = num;
                que.push(n);
            }
        }
    }

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


        if (color[s] == color[t] && color[s] != -1) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>
#include <cstring>

bool isFriend(std::vector<int> *a, int s, int t, int *checked, int c){
    int tmp;
    checked[s] = c;
    if(s==t)return true;
    for(int i=0;i<a[s].size();i++){
        tmp = a[s][i];
        if(checked[tmp]>=c)continue;
        if(isFriend(a,tmp,t,checked,c))return true;
    }
    return false;
}

int main(){
    int i, n, m, q, s, t;
    int *checked;
    scanf("%d %d",&n,&m);
    checked = (int *)malloc(sizeof(int)*n);
    memset(checked, 0, sizeof(int)*n);
    std::vector<int> a[n];
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(a,s,t,checked,i+1))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		V = new vector<int>[n];
		group = new int[n];
		for(int i=0;i<n;i++){
			group[i] = 0;
		}
		group_number = 1;
	}

	void push(int s,int t){
		V[s].push_back(t);
		V[t].push_back(s);
	}

	void makeGroup(){
		int node,i;
		queue<int> Q;
		Q.push(0);
		group[0] = 1;
		while(true){
			while(!Q.empty()){
				node = Q.front();
				Q.pop();
				if(V[node].size() > 0){
					for(int k=0; k < V[node].size();k++){
						if(group[V[node][k]] == 0){
							group[V[node][k]] = group[node];
							Q.push(V[node][k]);
						}
					}
				}
			}
			group_number++;
			for(i=0;i<n;i++){
				if(group[i] == 0){
					group[i] = group_number;
					Q.push(i);
					break;
				}
			}
			if(i == n) break;
		}
	}

	bool canAccsess(int s,int t){
		return group[s] == group[t];
	}

private:
	vector<int>* V;
	int* group;
	int group_number;
	int n,m;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.makeGroup();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}

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;
}