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++: //By Vlgd
//By Vlgd
#include<bits/stdc++.h>
using namespace std;
const int N=100000;
int n,m,d[N],t=0;
vector<int>G[N];
stack<int>S;
void dfs(int r,int cnt){
	S.push(r);
	d[r]=cnt;
	while(!S.empty()){
		int u=S.top();S.pop();
		for (int i=0;i<G[u].size();i++){
			int v=G[u][i];
			if (d[v]==-1){
				d[v]=cnt;
				S.push(v);
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	int u,v;
	for (int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u); 
	}
	memset(d,-1,sizeof(d));
	for (int i=0;i<n;i++) if (d[i]==-1) dfs(i,t++);
	int q;
	scanf("%d",&q);
	for (int i=0;i<q;i++){
		scanf("%d%d",&u,&v);
		if (d[u]==d[v]) printf("yes\n");
		else printf("no\n");
	} 
}

C++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
using namespace std;

bool isFriend(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    if (sns[x][y] == 1) return true;
    bool res = false;
    arrived[x] = 1;
    for (int i = 0; i < sns.size(); i++) {
        if (sns[x][i] == 1 && !arrived[i]) {
            bool tmp = isFriend(sns, arrived, i, y);
            if (tmp) {
                res = true;
                break;
            }
        }
    }
    return res;
}

int main()
{
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>(n, 0));
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x][y] = 1;
        sns[y][x] = 1;
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        bool res = isFriend(sns, arrived, x, y);
        if (res) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

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

int main() {
	int n,m,q,k;
	int s,t;
	int i,j;
	queue<int> nq;
	vector<vector<int> > node;
	vector<int> ngrp;

	scanf("%d%d",&n,&m);
	node.resize(n);
	ngrp.resize(n);

	for(i=0;i<m;++i) {
		scanf("%d%d",&s,&t);
		node[s].push_back(t);
		node[t].push_back(s);
	}

	for(i=0;i<n;++i) {
		ngrp[i]=-1;
	}

	for(i=0; i<n; ++i) {
		if(ngrp[i]==-1) {
			nq.push(i);
			ngrp[i]=i;
			while(!nq.empty()) {
				k=nq.front();
				for(j=0;j<(int)node[k].size();++j) {
					if(ngrp[node[k][j]]==-1) {
						ngrp[node[k][j]]=i;
						nq.push(node[k][j]);
					}
				}
				nq.pop();
			}
		}
	}
	scanf("%d",&q);
	for(i=0;i<q;++i) {
		scanf("%d%d",&s,&t);
		printf((ngrp[s]==ngrp[t])?"yes\n":"no\n");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int MAX=100000,NIL=-1;
int n,color[MAX];
vector<int> G[MAX];
void dfs(int r,int c){
    stack<int> S;
    S.push(r);
    color[r]=c;
    while(!S.empty()){
        int u=S.top(); S.pop();
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}
void assign(){
    int id=1;
    for(int i=0;i<n;i++){
        color[i]=NIL;
    }
    for(int u=0;u<n;u++){
        if(color[u]==NIL) dfs(u,id++);
    }
}
int main(void){
    int s,t,m,q;
    cin >> n >> m;
    for(int i=0;i<m;i++){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    assign();
    cin >> q;
    for(int i=0;i<q;i++){
        cin >> s >> t;
        if(color[s]==color[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

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

}