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 <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;++i)

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

vector<set<int>> node;
vector<int> searched;

void solve(int index){
    searched[index]=index;
    queue<int> que; que.push(index);
    while(!que.empty()){
        int order = que.front(); que.pop();
        for(auto itr=node[order].begin();itr!=node[order].end();++itr){
          if(-999==searched[*itr])que.push(*itr);
            searched[*itr]=index;
        }
    }
}

int main(){
   int n,m;cin>>n>>m;
   node.resize(n);
   searched.resize(n,-999); 
   REP(i,m){
    int x,y;cin>>x>>y;
    node[x].insert(y);
    node[y].insert(x);
   }
   REP(i,n)
    if(-999==searched[i])
      solve(i);
   int hm;cin>>hm;
    REP(i,hm){
      int x,y;
      cin>>x>>y;
      if(searched[x]==searched[y]&&searched[y]!=-999&&searched[x]!=-999)
        cout << "yes" << endl;
      else 
        cout << "no" << endl;
    }
}

C++: #define _LIBCPP_DEBUG 0
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1e9 + 7;

vector<int> G[100000+2];
vector<int> visit(100000+2, -1);
int color = 0;

void dfs(int x){

    if(visit[x] > -1)
        return;
    visit[x] = color;

    for (int i = 0; i < G[x].size(); ++i) {
        dfs(G[x][i]);
    }
}


int main(void) {

    int n,m; cin>>n>>m;
    for (int i = 0; i < m; ++i) {
        int s,t; cin>>s>>t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    for (int i = 1; i < n; ++i) {
        if(visit[i]<0){
            color++;
            dfs(i);
        }
    }

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

    }

    return 0;
}

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

int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
	d[i]=i;
}
for(int l=0;l<m;l++){
	int a,b;
	cin>>a>>b;
	int j ,h;
	j=a,h=b;
while(d[j]!=j || d[h]!=h){
	j=d[j];
	h=d[h];
}
if(j!=h){
	d[j]=h;
}
}
cin>>q;
for(int y=0;y<q;y++){
	int a,b;
	cin>>a>>b;
		int j ,h;
	j=a,h=b;
while(d[j]!=j || d[h]!=h){
	j=d[j];
	h=d[h];
}
	if(j==h){
		cout<<"yes"<<endl;
	}
	else{
		cout<<"no"<<endl;
	}
}
	return 0;
}

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

class UnionFind {
public:
    vector<int> par, rank;
    UnionFind(int size) {
        par.resize(size);
        rank.resize(size);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        if (rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

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

int main() {
    int n, m;
    cin >> n >> m;
    UnionFind uf = UnionFind(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        uf.unite(s, t);
    }

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

C++: #include <cstdio>
#include <vector>

using namespace std;

struct uf_find_tree {
    vector<int> par, sizes;
    uf_find_tree(int n) : par(n), sizes(n, 1) {
        for (int i = 0; i < n; i++) { par[i] = i; }
    }
    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }
    void unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return;
        if (sizes[x] < sizes[y]) { swap(x, y); }
        par[y] = x;
        sizes[x] += sizes[y];
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return sizes[find(x)]; }
};

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

    scanf("%d %d", &n, &m);

    uf_find_tree uf(n);

    while (m--) {
        scanf("%d %d", &s, &t);
        uf.unite(s, t);
    }

    scanf("%d", &q);

    while (q--) {
        scanf("%d %d", &s, &t);
        printf((uf.same(s, t)) ? "yes\n" : "no\n");
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define MAX 100000
#define NIL -1

int n;
vector<int> G[MAX];
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;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100000
#define NIL -1

/*

G':1-2-4 3-5
color:1 2

*/

vector<int> G[MAX];
int color[MAX];

void dfs(int u, int c) {
	stack<int> S;
	S.push(u);
	color[u] = 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);
			}
		}
	}
}

int main() {
	int n, m; cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int s, t; cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	fill(color, color + MAX, NIL);
	int id = 1;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)dfs(u, id++);
	}

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

	return 0;
}