Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; 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)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: //
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  int n,m,join[100000],num[100000],s,t;
  cin >> n >> m;

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

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

    if(join[t] != join[s]){
      int js = join[s];
      int jt = join[t];

      join[t] = join[s]; 
      num[js] += 1;
      num[jt] -= 1;
      
      
      for(int a=0; 0<num[jt]; a++){
	cout << "^^ ";
	if(join[a]==jt && jt != a){
	  join[a] = js;
	  num[js] += 1;
	  num[jt] -= 1;
	}
      }
    }
  }

  int q;
  cin >> q;
  bool answer[10000];

  for(int i=0; i<q; i++){
    answer[i] = false;
    cin >> s >> t;

    if(join[s]==join[t])
      answer[i] = true;
  }

  for(int i=0; i<q; i++){
    if(answer[i])
      cout << "yes\n";

    else
      cout << "no\n";
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define N 100000

using namespace std;
vector<int> v[N];
int group[N]={};
int g=0;

bool search(int s,int t){
  if(group[s]==group[t]&&group[s]>0) return true;
  if(group[s]>0) return false;
  group[s]=++g;
  queue<int> q;
  q.push(s);
  while(!q.empty()){
    int a=q.front();
    for(auto i=v[a].begin();i!=v[a].end();i++){
      if(group[*i]==0){
        group[*i]=g;
        q.push(*i);
      }
    }
    q.pop();
  }
  if(group[s]==group[t]&&group[s]>0){ 
    return true;
  }else{
    return false;
  }
}

int main(){
  int n,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 q; cin>>q;
  for(int i=0;i<q;i++){
    cin>>s>>t;
    if(search(s,t)){
      cout<<"yes"<<endl;
    }else{
      cout<<"no"<<endl;
    }
  }

}

C++: #include <iostream>
#include <cstdio>
#define MAX 100000

using namespace std;

int main(){
  int a[MAX]={};
  int n,m; cin>>n>>m;
  int s,t;
  int group=1;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    if(a[s]==0 && a[t]==0){
      a[s]=a[t]=group;
      group++;
    }else if(a[s]==0){
      a[s]=a[t];
    }else if(a[t]==0){
      a[t]=a[s];
    }else{
      int tmp=a[t];
      a[t]=a[s];
      for(int j=0;j<i;j++){
        if(a[j]==tmp) a[j]=a[s];
      }
    }
  }

  int q; cin>>q;
  for(int i=0;i<q;i++){
    cin>>s>>t;
    if(a[s]==a[t]){
      cout<<"yes"<<endl;
    }else{
      cout<<"no"<<endl;
    }
  }

}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 100001;

vector<int> G[maxn];
int vis[maxn];

void DFS(int u, int k)
{
    vis[u] = k;
    for(vector<int>::iterator it = G[u].begin(); it != G[u].end(); it++)
        if(vis[*it] == -1)
            DFS(*it, k);
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int j = 0; j < n; j++)
            vis[j] = -1;
    for(int i = 0; i < n; i++)
        if(vis[i] == -1)
            DFS(i, i);
    int q;
    cin >> q;
    for(int i = 0; i < q; i++)
    {
        int x, y;
        cin >> x >> y;
        if(vis[y] == vis[x])
            cout << "yes" <<endl;
        else
            cout << "no" <<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<algorithm>

bool dfs(std::vector<std::vector<int> > n, int from, int to) {
	std::stack<int> s;
	std::vector<int> went(n.size(), 0);

	s.push(from);
	while (s.size() != 0){
		int num = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
			if (*it == to) return true;
			if (!went[*it]) {
				s.push(*it);
				went[*it] = 1;
			}
		}
	}
	return false;
}

void color(std::vector<std::vector<int> > n) {
	std::vector<int> color(n.size(), 0);
	std::vector<int> went(n.size(), 0);
	std::stack<int> s;

	int count = 1;
	for (size_t i = 0; i < n.size(); i++) {
		if (color[i] != 0) continue;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				if (!went[*it]) {
					s.push(*it);
					went[*it] = 1;
					color[*it] = count;
				}
			}
		}
		count++;
	}
	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (color[from] == color[to]) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	color(n);

	return 0;
}

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

#include<stdio.h>


#include<queue>
using namespace std;
int main()
{
    int person_number,relation_number;

    cin>>person_number>>relation_number;

    vector<vector<int > > list(person_number);
    for(int i=0;i<relation_number;i++)
    {
        int start,end;
        scanf("%d %d ",&start,end);
        int flag=0;
        for(auto n:list[start])
        {
            if(n==end)
                flag=1;
        }
        if(flag==0) 
        {
            list[start].push_back(end);
            list[end].push_back(start);
        }
    }

    int test_number;
    scanf("%d",&test_number);
for(int i=0;i<test_number;i++)
{
    int from,to;
    scanf("%d %d ",&from,&to);
    queue<int> q;
    q.push(from);
    vector<int> ban_list;
    for(int i=0;i<person_number;i++)
    {
        ban_list.push_back(0);
    }
    ban_list[from]=1;
    for(;!q.empty();q.pop())
    {
        int flag=0;
        for(auto n:list[q.front()])
        {
            if(n==to) 
            { 
                flag=1;
                break;
            }
            if(ban_list[n]==0)
            {
                ban_list[n]=1;
                q.push(n);
            }
        }
        if(flag==1) break;
    }
    if(!q.empty()) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
}
    return 0;
}