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++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5
#define NIL -1
int flag = 0;
vector<int> G[MAX];
queue<int> qu;
int vis[MAX];
int n;
int ind, tmp;

void bfs(int u,int k) {
	qu.push(u);
	vis[u] = k;
	while (not qu.empty()) {
		int u = qu.front(); qu.pop();
		for (int j = 0; j < G[u].size(); j++) {
			int v = G[u][j];
			if (vis[v] == NIL) {
				vis[v] = k;
				qu.push(v);
			}
		}
	}
}

void calc() {
	int id = 1;
	for (int i = 0; i < n; i++) vis[i] = NIL;
	for (int i = 0; i < n; i++) {
		if (vis[i] == NIL) bfs(i, id++);
	}
}

int main() {
	int m, q;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
		G[tmp].push_back(ind);
	}
	calc();
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		if (vis[ind] == vis[tmp])
		cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

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

using namespace std;

static const int MAX = 100000;
static const int 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 m, s, t;

  cin >> n >> m;

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

  assignColor();

  int q;
  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 <vector>
#include <map>
#include <stack>
using namespace std;

#define null -1

void DFS(vector<int> rVal[], map<int, int> uColor_m, int idOrSs) {
	stack<int> stack;
	stack.push(idOrSs);
	uColor_m[idOrSs] = idOrSs;

	while (!stack.empty()) {
		int curUVal = stack.top();
		stack.pop();
		for (int i = 0; i < (signed)rVal[idOrSs].size(); i++) {
			int curV = rVal[curUVal][i];
			if (uColor_m[curV] != null) continue;
			uColor_m[curV] = idOrSs;
			stack.push(curV);
		}
	}
}

void assignColor(int uNum, vector<int> rVal[], map<int, int> uColor_m) {
	for (int i = 0; i < uNum; i++)
		uColor_m[i] = null;
	for (int i = 0; i < uNum; i++) {
		if (uColor_m[i] == null) DFS(rVal, uColor_m, i);
	}
}

int main() {
	int userNum;
	int relationNum;
	map<int, int> uColor_map; //<?????????, ??°??????????????????>
	//??????????????¨????????????
	vector<int> relationVal[100000];
	//int userColor[100000];

	cin >> userNum >> relationNum;
	for (int i = 0; i < relationNum; i++) {
		int userVal1, userVal2;
		cin >> userVal1 >> userVal2;
		relationVal[userVal1].push_back(userVal2);
		relationVal[userVal2].push_back(userVal1);
		uColor_map[i] = null;
	}

	assignColor(userNum, relationVal, uColor_map);

	int qNum;
	cin >> qNum;
	for (int i = 0; i < qNum; i++) {
		int uV1, uV2;
		cin >> uV1 >> uV2;
		if (uColor_map[uV1] == uColor_map[uV2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>

#define N 100005
#define UNDEFINED -1

int n, m, s, t, q;
std::vector<int> adj[N];
int group[N];

void dfs(int x, int g) {
    std::stack<int> S;
    S.push(x);
    group[x] = g;
    while(!S.empty()) {
        int u = S.top(); S.pop();
        for(int i = 0; i < adj[u].size(); i++) {
            int v = adj[u][i];
            if(group[v] == UNDEFINED) {
                group[v] = g;
                S.push(v);
            }
        }
    }
}

int main() {
    for(int i = 0; i < N; i++) {
        group[i] = UNDEFINED;
    }

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

    int g = 0;
    for(int i = 0; i < n; i++) {
        if(group[i] == UNDEFINED) {
            dfs(i, g++);
        }
    }


    std::cin >> q;
    for(int i = 0; i < q; i++) {
        std::cin >> s >> t;
        if(group[s] != UNDEFINED && group[t] != UNDEFINED && group[s] == group[t]) {
            puts("yes");
        } else {
            puts("no");
        }
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_p
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf (1ll<<60)
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl

using namespace std;

const int mod=1e9+7;

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

#define N 100000
int r[N],p[N];

void init(int n){
    int i;
    rep(i,n){
        p[i]=i;
        r[i]=0;
    }
}

int find_set(int x){
    if(x!=p[x])
        p[x]=find_set(p[x]);
    return p[x];
}

void link(int x,int y){
    if(r[x]>r[y])
        p[y]=x;
    else{
        p[x]=y;
        if(r[x]==r[y])
            r[y]++;
    }
}
void unite(int x,int y){
    link(find_set(x),find_set(y));
}

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

void run(){
    int n,m;
    cin>>n>>m;
    init(n);
    int s,t;
    rep(i,m){
        cin>>s>>t;
        unite(s,t);
    }
    int q;
    cin>>q;
    rep(i,q){
        cin>>s>>t;
        yes(same(s,t));
    }
}