Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

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

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

    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;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int main(void) {
    int n, m, a, b, q;
    cin >> n >> m;
    UnionFind uf(n);
    for (int i= 0; i < m; i++) {
        cin >> a >> b;
        uf.unite(a, b);
    }

    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> a >> b;
        cout << (uf.same(a, b) ? "yes":"no") << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	int parent[n];
	for(int i=0;i<n;i++)
	{
		parent[i] = -1;
	}
	int first,second;
	for(int i=0;i<m;i++)
	{
		cin>>first>>second;
		while(parent[first]!=-1)
		{
			first=parent[first];
		}
		while(parent[second]!=-1)
		{
			second=parent[second];
		}
		if(second!=first)
		{
			parent[second]=first;
		}
	}
	int cases,first1,second1;
	cin>>cases;
	for(int i=0;i<cases;i++)
	{
		cin>>first1>>second1;
		while(parent[first1]!=-1)
		{
			first1=parent[first1];
		}
		while(parent[second1]!=-1)
		{
			second1=parent[second1];
		}
		if(first1==second1)
			cout<<"yes";
		else
			cout<<"no";
		cout<<endl;
	}
}

C++: #include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

typedef std::vector<bool> TCheckList;
class TVertex
{
    int FIndex;
    std::vector<TVertex*> FList;

public:
    TVertex(int Index)
     : FIndex(Index)
    {}
    void AddPath(TVertex* Vertex)
    {
        FList.push_back(Vertex);
    }
    bool IsFriend(const TVertex* Target, TCheckList& CheckList)const
    {
        assert(FIndex < CheckList.size());
        if(CheckList[FIndex]) return false;
        CheckList[FIndex] = true;
        for(std::vector<TVertex*>::const_iterator It = FList.begin(); It != FList.end(); ++It){
            const TVertex* V = *It;
            if(V == Target) return true;
            if(V->IsFriend(Target, CheckList)){
                return true;
            }
        }
        return false;
    }
};
class TGraph
{
    std::vector<TVertex*> FTable;
    void Init(int n)
    {
        assert(n > 0);
        FTable.resize(n);
        for(int i = 0; i < n; ++i){
            FTable[i] = new TVertex(i);
        }
    }
public:
    TGraph(int n)
    {
        Init(n);
    }
    
    void SetupVertex(std::istream& in)
    {
        int Index;
        in >> Index;
        assert(Index >= 1);
        assert(Index <= FTable.size());
        int k;
        in >> k;
        for(int i = 0; i < k; ++i){
            int v;
            in >> v;
            FTable[Index]->AddPath(FTable[v]);
        }
    }
    void MakeFriend(int s, int t)
    {
        assert(s < FTable.size());
        assert(t < FTable.size());
        FTable[s]->AddPath(FTable[t]);
        FTable[t]->AddPath(FTable[s]);
    }
    bool IsFriend(int s, int t)const
    {
        assert(s < FTable.size());
        assert(t < FTable.size());
        TCheckList CheckList(FTable.size(), false);
        return FTable[s]->IsFriend(FTable[t], CheckList);
    }
};

int main()
{
    int n, m;
    std::cin >> n >> m;
    TGraph Graph(n);
    for(int i = 0; i < m; ++i){
        int s, t;
        std::cin >> s >> t;
        Graph.MakeFriend(s, t);
    }
    int q;
    std::cin >> q;
    for(int i = 0; i < q; ++i){
        int s, t;
        std::cin >> s >> t;
        std::cout << (Graph.IsFriend(s, t) ? "yes" : "no") << std::endl;
    }
    return 0;
    
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int main(){
    //input
    int n,m;
    std::cin>>n;
    std::cin>>m;
    int A[n][n];
    bool visited[n];
    int judge[n];

    for(int i=0;i<n;i++){
        judge[i]=-1;
        visited[i]=false;
        A[i][0]=0;
        for(int j=1;j<n;j++){
            A[i][0]=0;
            A[i][j]=-1;
        }    
    }

    int cnt_i=0,cnt_j=0;
    for(int k=0;k<m;k++){
        int i,j;
        std::cin>>i;
        std::cin>>j;
        A[i][0]++; cnt_i=A[i][0];
        A[j][0]++; cnt_j=A[j][0];
        A[i][cnt_i]=j;
        A[j][cnt_j]=i;
    }

    /*for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            std::cout<<" "<<A[i][j];
        }
        std::cout<<std::endl;
    }*/

    int n_2;
    std::cin>>n_2;
    for(int i=0;i<n_2;i++){
        int s,e;
        std::cin>>s;
        std::cin>>e;
        
        int cnt=0;
        for(int i=0;i<n;i++){
            judge[i]=-1;
            visited[i]=false;
        }
        
        std::queue<int> que;
        que.push(s);
        /*for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                //std::cout<<" "<<A[i][j];
            }
            //std::cout<<std::endl;
        }*/
        while(!que.empty()){
            int q=que.size();
            for(int k=0;k<q;k++){
                //std::cout<<"quesize"<<que.size()<<"\n";
                int i=que.front();
                que.pop();
                visited[i]=true;
                judge[i]=cnt;
                //std::cout<<i<<"cnt"<<cnt<<"\n";
                //std::cout<<A[i][0]<<"\n";
                if(A[i][0]!=0){
                    for(int j=0;j<A[i][0];j++){
                        if(visited[A[i][j+1]]!=true){
                            visited[A[i][j+1]]=true;
                            que.push(A[i][j+1]);
                        }
                    }
                }
            }
            cnt++;
        }
        if(judge[e]==-1){
            std::cout<<"no\n";
        }else std::cout<<"yes\n";
    }

    return 0;
}