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>
using namespace std;
#include<vector>
#include<stack>
#define MAX 100000
int n,m;
vector<int>G[MAX];
stack<int>mp;
int color[MAX];

void dfs(int r,int c){
	mp.push(r);
	color[r] = c;
	while (!mp.empty()){
		int u = mp.top();
		mp.pop();
		for (int i = 0;i < G[u].size();i++){
			int v = G[u][i];
			if (color[v] == -1){
				color[v] = c;
				mp.push(v);
			}
		}
	}
}

void assignColor(){
	int id = 1;
	for (int i = 0;i < n;i++){
		color[i] = -1;
	}
	for (int j = 0;j < n;j++){
		if (color[j] == -1){
			dfs(j,id++);
		}
	}
}

int main()
{
	cin >> n >> m;
	int x,y; 
	for (int i = 0; i < m ;i++){
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	assignColor();
	cin >> m;
	for (int i = 0 ;i < m;i++){
		cin >> x >> y;
		if (color[x] == color[y]){
			cout << "yes" << endl;
		}
		else{
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct node {
	int id;
	vector<int> rel;
};

node* nodes;

void dfs(int start, int cur, int* h){
	if(h[cur] == -1){
		h[cur] = start;
		for(int i = 0; i < nodes[cur].rel.size(); i++){
			dfs(start, nodes[cur].rel[i], h);
		}
	}
}

int main() {
	int n, m, q;
	scanf("%d %d", &n, &m);
	 
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	int *h = new int[n];
	for(int i = 0; i < n; i++){
		h[i] = -1;
	}
	for(int i = 0; i < n; i++){
		dfs(i, i, h);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		printf("%s\n", h[l]==h[r] ? "yes" : "no");
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define MAX_NODE 100000
#define MAX_EDGE 100000

using namespace std;

int n;//num of nodes
int e;//num of edges
int group[MAX_NODE+1];//0オリジン
int q;

bool neighbor_a[MAX_NODE+1];
vector<int> neighbor_v[MAX_NODE+1];//連接リスト

void addNeighbor(int num,int ori){//oriを含む連結グラフをつくる
	int ngb;
	
	for(int i = 0;i < neighbor_v[num].size();i++){//深さ優先探索
		if(neighbor_a[neighbor_v[num][i]] == false){
			neighbor_a[neighbor_v[num][i]] = true;
			neighbor_v[ori].push_back(neighbor_v[num][i]);
			ngb = neighbor_v[num][i];
			addNeighbor(ngb,ori);
		}
	}
}


int main(){
	int v_1,v_2;
	scanf("%d %d",&n,&e);

	for(int i = 0;i < e;i++){
		scanf("%d %d",&v_1,&v_2);
		neighbor_v[v_1].push_back(v_2);
		neighbor_v[v_2].push_back(v_1);
	}

	for(int i  = 0;i < n;i++) group[i] = -1;

	for(int i = 0;i < n;i++){
		if(group[i] != -1) continue;

		for(int j = 0;j < n;j++) neighbor_a[j] = false;//iの連接配列
		neighbor_a[i] = true;
		neighbor_v[i].push_back(i);

		addNeighbor(i,i);

		for(int j = 0;j < neighbor_v[i].size();j++) group[neighbor_v[i][j]] = i;

	}
	//for(int i = 0;i < n;i++) cout <<group[i] <<" ";
	//for(int i = 0;i < neighbor_v[5].size();i++) cout <<neighbor_v[5][i] <<" ";

	scanf("%d",&q);

	for(int i = 0;i < q;i++){
		scanf("%d %d",&v_1,&v_2);
		if(group[v_1] == group[v_2]) cout <<"yes" <<endl;
		else cout <<"no" <<endl;
	}
	return 0;
}

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

int n;
int parent[100000];
int r[100000]; //rank

//Union-find tree
void init(int n){
  for(int i = 0;i < n;i++){
    parent[i] = i;
    r[i] = 0;
  }
}

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

void unite(int x, int y){
  x = find(x);
  y = find(y);
  
  if(x == y) return ;
  
  if(r[x] < r[y]) parent[x] = y;
  else{
    parent[y] = x;
    if(r[x] == r[y]) r[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

int main(){
  
  int m, q, s, t;

  //input
  cin >> n >> m;
  init(n);
  for(int i = 0;i < m;i++){
    cin >> s >> t;
    unite(s, t);
  }

  //ouptut
  cin >> q;
  for(int i = 0;i < q;i++){
    cin >> s >> t;
    if(same(s, t)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  
  return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n) {
    parent_index_.reserve(n);
    for (int i = 0; i < n; ++i) {
      parent_index_.push_back(i);
    }
  }
  void register_friend(int s, int t) {
    auto r1 = get_root(s);
    auto r2 = get_root(t);
    if (r1 != r2) {
      parent_index_[r2] = r1;
    }
  }
  bool can_reach(int s, int t) { return get_root(s) == get_root(t); }

 private:
  int get_root(int i) {
    for (auto cur = i;;) {
      auto next = parent_index_[cur];
      if (next == cur) {
        return cur;
      }
      parent_index_[cur] = parent_index_[next];
      cur = next;
    }
  }

  vector<int> parent_index_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}

C++: #include<stdio.h>
#include<vector>
#include<string>
using namespace std;

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        visited.resize(n,false);
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        if(s1!=s2&&s1=="yes")return 1;
        cout<<s1<<endl;
    }
    return 0;
}