Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<bits/stdc++.h>

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define floot10 cout<<fixed<<setprecision(10);
#define int long long

struct UnionFind{
  int n;
  vector<int> r,p;
  UnionFind(){}
  UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
  int find(int x){
	return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
	return find(x)==find(y);
  }
  void unite(int x,int y){
	x=find(x);y=find(y);
	if(x==y) return;
	if(r[x]<r[y]) swap(x,y);
	r[x]+=r[y];
	p[y]=x;
  }
};


signed main(){
	int n,m;
	cin>>n>>m;
	UnionFind hoge(n);
	lp(i,m){
		int a,b;
		cin>>a>>b;
		hoge.unite(a,b);
	}
	int q;
	cin>>q;
	lp(i,q){
		int a,b;
		cin>>a>>b;
		if(hoge.same(a,b)==true) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#include <cstdlib>

const int N = 100000;
int color[N];
std::vector<int> adj_list[N];

void DFS(int node_id, int c)
{
    std::stack<int> S;
    S.push(node_id);
    color[node_id] = c;

    while(!S.empty()) {
        int target = S.top();
        S.pop();
        for (int i = 0; i < adj_list[target].size(); i++) {
            int friend_id = adj_list[target][i];
            if (color[friend_id] == -1) {
                color[friend_id] = c;
                S.push(friend_id);
            }
        }
    }
}

void AssignColor(int n)
{
    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(int argc, char *argv[])
{
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);

    // make adj matrix
    for (int i = 0; i < m; i++) {
        int node_id, friend_id;
        scanf("%d", &node_id);
        scanf("%d", &friend_id);
        adj_list[node_id].push_back(friend_id);
        adj_list[friend_id].push_back(node_id);
    }

    AssignColor(n);

    int q_num;
    scanf("%d", &q_num);
    for (int i = 0; i < q_num; i++) {
        int src, dst;
        scanf("%d", &src);
        scanf("%d", &dst);
        if (color[src] == color[dst]) fprintf(stdout, "yes\n");
        else fprintf(stdout, "no\n");
    }

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxN=100000;
int link[maxN+10];
struct bian{
    int next;
    int zhongdian;
    bian():next(-1),zhongdian(-1){}
}a[maxN+10];
int k=0;
int flag;
void dfs(int x,int y)
{

        int g=link[x];
        while(g!=-1)
        {
            if(a[g].zhongdian==y)
            {
               flag=1;
               return;
            }
            dfs(a[g].zhongdian,y);
            g=a[g].next;
        }
        return;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<maxN+10;i++)
    {
        link[i]=-1;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        a[k].zhongdian=y;
        a[k].next=link[x];
        link[x]=k;
        k++;
    }
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        flag=0;
        dfs(x,y);
       /* int g=link[x];
        while(g!=-1)
        {
            if(a[g].zhongdian==y)
            {
                flag=1;break;
            }
            g=a[g].next;
        }*/
        if(flag==1)
        {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

bool canReach(int s, int d, vector<int> &h){
	h.push_back(s);
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(find(h.begin(), h.end(), nodes[s].rel[i]) == h.end()){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		vector<int> h;
		h.clear();
		printf("%s\n", canReach(l, r, h) ? "yes" : "no");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define NIL -1

vector<int> G[100003];
int color[100003];
int n;

int nextPoint(int curPoint){
    int next;
    for(int i=0;i<G[curPoint].size();++i){
        next = G[curPoint][i];
        if(color[next] == NIL){
            return next;
        }
    }
    return -1;
}

void dfs(int point, int value){
    color[point] = value;
    while(1){
        int next = nextPoint(point);
        if(next != -1){
            dfs(next,value);
        }
        else{
            color[point] = value;
            return;
        }
    }
}

int main(){
    int m, 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);
    }
    for(int i=0;i<n;++i){
        color[i] = NIL;
    }
    int id = 0;
    for(int i=0;i<n;++i){
        if(color[i]==NIL){
            id += 1;
            dfs(i,id);
        }
    }
    int q;
    scanf("%d",&q);
    for(int i=0;i<q;++i){
        scanf("%d %d",&s, &t);
        if(color[s]==color[t]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}