Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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 <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
//#include "../cxx-prettyprint/prettyprint.hpp"

std::vector<std::vector<int>> Adj;
std::unordered_set<int> nodes_unvisited;
std::unordered_map<int, int> node_component_map;

void mark_nodes(int node, int component_id) {
    node_component_map[node] = component_id;
    nodes_unvisited.erase(node);
    for (int& next : Adj[node]) {
        if (nodes_unvisited.find(next) != nodes_unvisited.end()) {
            mark_nodes(next, component_id);
        }
    }
}

int main() {
    int n, m;
    std::cin >> n >> m;

    Adj.assign(n, std::vector<int>());
    for (int i = 0; i < m; i++) {
        int s, t;
        std::cin >> s >> t;
        Adj[s].push_back(t);
        Adj[t].push_back(s);
    }

    for (int i = 0; i < n; i ++) {
        nodes_unvisited.insert(i);
    }

    int component_id = 0;
    while (!nodes_unvisited.empty()) {
        int start = *nodes_unvisited.begin();
        mark_nodes(start, component_id);
        component_id ++;
    }

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

C++: #include <cstdio>
#include <vector>
#include <queue>
using namespace std;
static const int NIL = -1;

int n, m;
int *color;
vector<int> *v;

void bfs(int r, int c) {
    queue<int> q;
    q.push(r);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (color[u] == NIL) {
            color[u] = c;
        }
        for (vector<int>::iterator it = v[u].begin(); it != v[u].end(); it++) {
            if (color[*it] == NIL) {
                q.push(*it);
            }
        }
    }
}

void assignColor() {
    color = new int[n];
    int id = 0;
    for (int i = 0; i < n; i++) {
        color[i] = NIL;
    }
    for (int u = 0; u < n; u++) {
        if (color[u] == NIL) {
            bfs(u, id++);
        }
    }
}

int main() {
    scanf("%d %d", &n, &m);
    v = new vector<int>[n];

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

    assignColor();

    int q;
    scanf("%d", &q);

    for (int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        printf("%s\n", color[s] == color[t] ? "yes" : "no");
    }

    return 0;
}

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

using namespace std;

int main() {
  vector<int> R[100000];
  queue<int> Q;
  int n,m,s,t,q;
  int visit[100000];

  cin >> n >> m;
  for(int i=0;i<m;i++){
    cin >> s >> t;
    R[s].push_back(t);
    R[t].push_back(s);
  }
  
  for(int j=0;j<n;j++) visit[j] = 0;
  for(int i=0;i<n;i++){
    if(visit[i] == 0){
      Q.push(i);
      while(Q.empty() == false){
	int u = Q.front();
	Q.pop();
	visit[u] = 1;
	for(int j=0;j<R[u].size();j++){
	  int v = R[u][j];
	  if(visit[v]==0)Q.push(v);
	}
	R[u].clear();
	R[u].push_back(i);
      }
    }
  }
    
  cin >> q;
  for(int i=0;i<q;i++){
    cin >> s >> t;
    if(R[s][0] == R[t][0]) cout << "yes" << endl;
    else cout << "no" << endl;
  }


  return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
int n,m;
int e;
int begin[101010];
int next[101010];
int to[101010];
int vis[101010];
//bool search(int st,int ed){
//	queue<int> q;
//	q.push(st);
//	q.push(ed);
//	vis[st]=1;
//	while(!q.empty()){
//		int now=q.front();
//		q.pop();
//		int te=begin[now];
//		while(te!=-1){
//			if(to[te]==ed)
//				return true;
//			if(vis[to[te]]==0){
//				q.push(to[te]);
//				vis[to[te]]=1;
//			}
//			te=next[te];
//		}
//	}
//	return false;
//}

bool doublesearch(int st,int ed){
	int size1=1;
	int size2=1;
	queue<int> q[2];
	q[0].push(st);
	q[1].push(ed);
	vis[st]=1;
	vis[ed]=2;
	while((!q[0].empty())&&(!q[1].empty())){
		if(size1<=size2){
			int now=q[0].front();
			q[0].pop();
			int te=begin[now];
			while(te!=-1){
				if(vis[to[te]]==2)
					return true;
				if(vis[to[te]]==0){
					q[0].push(to[te]);
					vis[to[te]]=1;
				}
				te=next[te];
			}
		}
		else{
			int now=q[1].front();
			q[1].pop();
			int te=begin[now];
			while(te!=-1){
				if(vis[to[te]]==1)
					return true;
				if(vis[to[te]]==0){
					q[1].push(to[te]);
					vis[to[te]]=1;
				}
				te=next[te];
			}
		}
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	int n,m,q;
	int st,ed;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=0;i<m;i++){
			scanf("%d%d",&st,&ed);
			to[++e]=ed;
			next[e]=begin[st];
			begin[st]=e;
			to[++e]=st;
			next[e]=begin[ed];
			begin[ed]=e;
		}
		scanf("%d",&q);
		for(int i=0;i<q;i++){
			memset(vis,0,sizeof(vis));
			scanf("%d%d",&st,&ed);
			if(doublesearch(st,ed))
				printf("yes\n");
			else
				printf("no\n");
		}
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t, p; vector<int>a[200000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	Q.push(A);
	while (!Q.empty()) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			p = x[a[Q.front()][k]];
			if (x[p] == 0) { 
				Q.push(a[Q.front()][k]);
			}
		}
		Q.pop();
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

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

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

const int MAX = 100000;
const int NIL = -1;
vector<int> G[MAX];
int n;
int color[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;
}