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 <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
   if (a > b) {
      a = b;
      return true;
   }
   return false;
}
template <class T>
inline bool chmax(T& a, T b) {
   if (a < b) {
      a = b;
      return true;
   }
   return false;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

vector<int> to[100000];
int color[100000];
void dfs(int u, int c) {
   color[u] = c;
   for (int v : to[u]) {
      if (color[v] == INF) dfs(v, c);
   }
}

int main() {
   cin.tie(0);
   ios_base::sync_with_stdio(0);

   int n, m;
   cin >> n >> m;
   rep(i, 0, m) {
      int s, t;
      cin >> s >> t;
      to[s].push_back(t);
      to[t].push_back(s);
   }
   rep(i, 0, n) color[i] = INF;
   int c = 0;
   rep(i, 0, n) {
      if (color[i] == INF) {
         dfs(i, c++);
      }
   }
   int q;
   cin >> q;
   rep(i, 0, q) {
      int s, t;
      cin >> s >> t;
      if (color[s] == color[t])
         cout << "yes" << endl;
      else
         cout << "no" << endl;
   }
   return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
    queue<int> Q;
vector<int> u[500000];
int cnt=0,color[500000];
// white 0 gray 1 black 2
// void BFS(int s,int n){
//     if(color[s]==0){
//         cnt++;
//         color[s]=cnt;
//         Q.push(s);
//         while(Q.size()!=0){
//             int tmp=Q.front();
//             Q.pop();
//             for(int j=0;j<u[tmp].size();j++){
//                 int to = u[tmp][j];
//                 if(color[to]==0){
//                     color[to]=cnt;
//                     Q.push(to);
//                 }
//             }
//         }
//     }
// }
int main(){
    int n,m,s,t,q;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>s>>t;
        u[s].push_back(t); 
        u[t].push_back(s);
    }
    cin>>q;
    for(int s=0;s<n;s++){
        if(color[s]==0){
            cnt++;
            color[s]=cnt;
            Q.push(s);
            while(Q.size()!=0){
                int tmp=Q.front();
                Q.pop();
                for(int j=0;j<u[tmp].size();j++){
                    int to = u[tmp][j];
                    if(color[to]==0){
                        color[to]=cnt;
                        Q.push(to);
                    }
                }
            }
    }
    }
    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>
#include <cstring>
#include <cstdio>
using namespace std;

int fri[100010];

int find(int x){
  if( fri[x] == x ) return x;
  else return fri[x] = find(fri[x]);
}

void uf_union(int x, int y){
  x = find(x);
  y = find(y);
  fri[x] = y;
}

int main(){

  int n,m,q;
  cin >> n >> m;

  // init
  for(int i=0; i<n; i++) fri[i] = i;

  //
  for(int i=0; i<m; i++){
    int u,v; scanf("%d %d",&u,&v);
    uf_union(u,v);
  }

  cin >> q;
  for(int i=0; i<q; i++){
    int u,v; scanf("%d %d",&u,&v);
    if( find(u) == find(v) ) printf("yes\n");
    else printf("no\n");
  }


}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int id;
    vector<int> children{};
    int visited = 0;
    
    void resetVisited(){
        this->visited = 0;
    }
};
 
deque<int> schedule; 
vector<status> graph;

bool bfs(int target_v){
    if (schedule.empty()){
        return false;
    }
    
    int now_v = schedule.front();
    schedule.pop_front();
    
    for(auto& next_v:graph[now_v].children){
        if(graph[next_v].visited) continue;

        if(next_v==target_v) return true;
        graph[next_v].visited = 1;
        schedule.push_back(next_v);
    }
    return bfs(target_v);
}
 

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //setup
    int nb_v; cin >> nb_v;
    int nb_e; cin >> nb_e;
    graph.resize(nb_v);
    for(int i=0; i<nb_e; ++i){
        int v1, v2; cin >> v1 >> v2;
        graph[v1].children.push_back(v2);
        graph[v2].children.push_back(v1);
    }
    for(auto& a:graph){
        sort(a.children.begin(),a.children.end());
    }
    
    //bfs
    int times; cin>>times;
    for(int i=0; i<times; ++i){
        int from_v; cin>>from_v;
        int to_v; cin>>to_v;
        for(auto& a:graph) a.resetVisited();
        schedule = {};
        schedule.push_back(from_v);
        cout<<(bfs(to_v)? "yes\n":"no\n");
    }
}

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

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent = -1;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
        }
        else
            person[t].parent = person[s].parent;
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}