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++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> Cluster;
	int n;

	Graph(int n);

	bool isConnected(int s, int t);

	void MakeCluster();

	void Link(int s, int t);

};

Graph::Graph(int n){
	A.resize(n);
	Cluster.resize(n);
	for (int i = 0; i < n; i++) Cluster[i] = 0;
	this->n = n;
}

void Graph::Link(int s, int t){
	A[s].push_back(t);
	A[t].push_back(s);
	return;
}

void Graph::MakeCluster(){
	int clusternum = 0;
	std::list<int> Q;
	for (int i = 0; i < n; i++){
		if (Cluster[i] == 0){
			Q.clear();
			clusternum++;
			Cluster[i] = clusternum;
			Q.push_back(i);
			bool res = false;
			while (!Q.empty()){
				int f = Q.front();
				Q.pop_front();
				for (std::vector<int>::iterator itr = A[f].begin(); itr != A[f].end(); itr++){
					if (Cluster[*itr] == false){
						Cluster[*itr] = clusternum;
						Q.push_back(*itr);
					}
				}
			}
		}
	}
	return;
}

bool Graph::isConnected(int s, int t){
	return Cluster[s] == Cluster[t];
}

int main(){
	int n, m, s, t, q;
	scanf("%d%d", &n, &m);
	Graph G(n);
	for (int i = 0; i < m; i++){
		scanf("%d%d", &s, &t);
		G.Link(s, t);
	}
	scanf("%d", &q);
	G.MakeCluster();
	for (int i = 0; i < q; i++){
		scanf("%d%d", &s, &t);
		if (G.isConnected(s, t)) printf("yes\n"); else printf("no\n");
	}
	
	return 0;
}

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

int a[100000];
int search(int k){
  if(a[k]==k){
      return k;
  }
  int p=search(a[k]);
  a[k]=p;
  return p;
}

int main(){
  for(int i=0;i<100000;i++){
    a[i]=i;
  }
  int n,m,p,q,r,s,t;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>p>>q;
    a[search(p)]=search(q);
  }
  cin>>r;
  for(int i=0;i<r;i++){
    cin>>s>>t;
    if(search(s)==search(t)){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty()){
        que.pop();
    }
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

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

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

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

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(x==t){
                answer[i]=true;
                break;
            }

            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);
            }
         }

          que.pop();
        }

        que=refresh(que,node,n);
    }

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

C++: #include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

int n, m;
vector<int> G[100000];
int id[100001];
int visit[100001] = {0};
int s, t;

void check(int x, int u) {
    visit[x] = 1;
    id[x] = u;

    int v;
    REP(i, G[x].size()) {
        v = G[x][i];
        if (visit[v] == 0) check(v, u);
    }
}

int main() {
    cin >> n >> m;
    int a, b;
    REP(i, m) {
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    REP(i, n) {
        if (visit[i] == 0) check(i, i);
    }

    int q;
    cin >> q;
    REP(i, q) {
        cin >> s >> t;
        if (id[s] == id[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

static const int MAX = 100000;
static const int NIL = -1;
int n;
int 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 assignColor(){
    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(){
    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);
    }
    assignColor();
    cin>>q;
    for (int  i = 0; i < q; i++)
    {
        cin >> s >>  t;
        if(color[s] == color[t]){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;
}

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

int uni[100000];

int oya(int a){
if (uni[a]<0){
	return a;
}
else{
	uni[a]=oya(uni[a]);
	return oya(uni[a]);
}
}

bool ren(int a,int b){
	a=oya(a);
	b=oya(b);
	if(a==b){
	return	false;
	}
	else{
		
	return true;	
	}
}

int main() {

int n,m;
cin>>n>>m;
for(int y=0;y<n;y++){
	uni[y]=-1;
}

for(int r=0;r<m;r++){
int s,g;
cin>>s>>g;
if(ren(s,g)){
	s=oya(s);
	uni[s]=g;
	}
}
int q;
cin>>q;
for(int rr=0;rr<q;rr++){
	int s,g;
	cin>>s>>g;
	if(ren(s,g)==true){
		cout<<"no"<<endl;
	}
	else{
		cout<<"yes"<<endl;
	}
}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;


int n;

string *color;

queue<int> q;

int *ans;

vector<int> g[10000];


void bfs(int x){
  color[x] = "gray";
  q.push(x);


  while(q.size() != 0){
    int u = q.front();
    q.pop();

    for(int i = 0; i < g[u].size(); i++){
      int v = g[u][i];
      if(color[v] == "white"){
        color[v] = "gray";
        q.push(v);
      }
    }

    color[u] = "black";
    ans[u] = x;


  }
  return;
}


int main(){

  cin >> n;

  color = new string[n];

  ans = new int[n];

  int m;

  cin >> m;


  int a, b;


  for(int i = 0; i < m; i++){
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }


  for(int i = 0; i < n; i++){
    color[i] = "white";
  }


  for(int j = 0; j < n; j++){
    if(color[j] == "white"){
      bfs(j);
    }
  }

  int q;

  cin >> q;

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

  return 0;
}