Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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

}