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 <cstdio>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

static const int MAX_NUM = 100000;

void DFS(int group[], vector<int> vec[], int gnum, int start)
{
    stack<int> S;
    int target;
    int i;
    int tmp;

    S.push(start);
    group[start] = gnum;

    while (S.empty() == false) {
        target = S.top();
        for (i = 0; i < vec[target].size(); i++) {
            if (group[vec[target][i]] == 0) {
                group[vec[target][i]] = gnum;
                S.push(vec[target][i]);
                break;
            }
        }
        if (i == vec[target].size()) {
            S.pop();
        }
    }

    return;
}

void SetupGroup(int group[], vector<int> vec[], int elemnum)
{
    int i;
    int gnum = 0;

    for (i = 0; i < elemnum; i++) {
        if (group[i] == 0) {
            ++gnum;
            DFS(group, vec, gnum, i);
        }
    }
}

int main(void)
{
    int i;
    int num;
    int elemnum;
    int qnum;
    int ele1, ele2;
    vector<int> vec[MAX_NUM];
    int group[MAX_NUM] = {0};

    scanf("%d %d", &elemnum, &num);
    for (i = 0; i < num; i++) {
        scanf("%d %d", &ele1, &ele2);
        vec[ele1].push_back(ele2);
        vec[ele2].push_back(ele1);
    }

    SetupGroup(group, vec, elemnum);

    scanf("%d", &qnum);
    for (i = 0; i < qnum; i++) {
        scanf("%d %d", &ele1, &ele2);
        if (group[ele1] == group[ele2]) {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
}

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

bool x[10000][10000];
bool y[10000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][b]=true;
		x[b][a]=true;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<100;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<n;k++){
						if(x[j][k]==true){
							y[k]=true;
						}
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxx=100010;

int pre[maxx];

int find(int u){
	while(pre[u]!=u) u=find(pre[u]);
	return u;
}

int main(){
	int n,m,x,y;
	cin>>n>>m;
	for(int i=0;i<n;i++) pre[i]=i;
//	for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
//	for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
	while(m--){
		cin>>x>>y;
		pre[find(x)]=find(y);
//		for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
//		for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
	}
	cin>>m;
	while(m--){
		cin>>x>>y;
		if(find(x)==find(y)) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int N = 100000;
int n;
int color[N];
vector<int> v[N];

bool isFriend(int s, int t){
  for(int i=0;i<n;i++) color[i] = 0;
  queue<int> q;
  int x;
  q.push(s);
  while(!q.empty()){
    x = q.front(); q.pop();
    for(int i=0;i<v[x].size();i++){
      int y = v[x][i];
      if(y == t) return true;
      if(color[y] == 0){
        q.push(y);
        color[y] = 1;
      }
    }
  }
  return false;
}

int main(){
  int m;
  cin >> n >> m;
  int s, t;
  for(int i=0;i<m;i++){
    cin >> s >> t;
      v[s].push_back(t);
      v[t].push_back(s);
  }
  int qn; cin >> qn;
  for(int i=0;i<qn;i++){
    cin >> s >> t;
    if(isFriend(s, t)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
vector<int> neighbors[100005];
//why choose vector, not matrix
int root[100005];

void dfs(int i, int start){
	root[i] = start;
	//visut all i's neighbors
	//c++ 11
	for(int j : neighbors[i]){
		if(root[j]==-1){
			dfs(j,start);
		}
	}
}

int main(){
	int n,m,x,y;
	memset(root, -1, sizeof(root));
	scanf("%d %d",&n,&m);
	for(int i = 0; i<m; i++){
		scanf("%d %d",&x,&y)
		neighbors[x].push_back(y);
		neighbors[y].push_back(x);
	}
	for(int i = 0; i<n; i++){
		if(root[x]= -1){
			dfs(i,i);
		}
	}
	scanf("%d", &q);
	for(int i = 0; i<q; i++){
		scanf("%d %d",&x, &y);
		if(root[x] = root[y]){
			printf("Yes\n");
		}else printf("no\n");
	}
	return 0;
}