Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define int long long
#define vec(a,n) vector<int> (a)((n));
#define Vec(a,n) vector<string> (a)((n));
#define twovec(a,n,m) vector<vector<int>> a(n,vector<int>(m,0));
#define Twovec(a,n,m) vector<vector<double>> a(n,vector<double>(m,0));
#define P pair<int,int>
#define All(a) (a).begin(),(a).end()
#define Sort(a) sort(All(a));
#define Reverse(a) reverse(All(a));
#define PQ(n) priority_queue<P,vector<P>,greater<P>> (n)
#define pq(n) priority_queue<int> (n)
#define print(a) cout << (a) << endl
#define printD(a) cout << setprecision(15) << (a) << endl;
using namespace std;
int max_int = 1000000007;
void Debug(auto a);
int nibul(auto a,auto b);
int nibuu(auto a,auto b);
void input(vector<auto>& a,int n);
double PI = 3.14159265358979;
int n;

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    vector<int> siz;

    UnionFind(int N) : par(N), siz(N, 1LL) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
        if (siz[rx] < siz[ry]) swap(rx, ry);
        siz[rx] += siz[ry];
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) { // 素集合のサイズ
        return siz[root(x)];
    }
};
//重み付きUF
struct UnionFind_w {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    vector<int> diff_weight;
    UnionFind_w(int n){ //最初は全てが根であるとして初期化
        init(n);
    }
    void init(int n) {
        par.resize(n);
        diff_weight.resize(n);
        REP(i,n){
            par[i] = i;
            diff_weight[i] = 0;
        }
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        int r = root(par[x]);
        diff_weight[x] += diff_weight[par[x]];
        return par[x] = r;
    }

    int weight(int x){
        root(x);
        return diff_weight[x];
    }

    void unite(int x, int y,int w) { // xとyの木を併合
        w -= weight(x);
        w += weight(y);
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
        diff_weight[rx] = w;
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    int diff(int x,int y){
        return weight(x) - weight(y);
    }
};


signed main(){
    cin >> n;
    UnionFind uf(n);
    int m;
    cin >> m;
    REP(i,m){
        int s,t;
        cin >> s >> t;
        uf.unite(s,t);
    }
    int q;
    cin >> q;
    REP(i,q){
        int s,t;
        cin >> s >> t;
        if(uf.same(s,t)){
            print("yes");
        }
        else{
            print("no");
        }
    }


    return 0;
}

void Debug(auto a){
    cout << "{ ";
    for(auto b: a){
        cout << b << " ";
    }
    cout << "}" << endl;
}

//key以上の初めてのitr
int nibul(auto a,auto b){return lower_bound(All(a),b) - a.begin();}
//key以下の最後のitr
int nibuu(auto a,auto b){return upper_bound(All(a),b) - a.begin()-1;}

void input(vector<auto>& a,int n){
    REP(i,n){
        cin >> a[i];
    }
}

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
//#define Kongxiangzhouye
//int n,m;
//int e;
//int begin[301010];
//int next[301010];
//int to[301010];

int par[101010];

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

int unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)
		par[y]=x;
}

void init(int n){
	for(int i=0;i<=n;i++){
		par[i]=i;
	}
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	int n,m,q;
	while(cin>>n>>m){
		init(n);
		for(int i=0;i<m;i++){
			int a,b;
			cin>>a>>b;
			unite(a,b);
		}
		cin>>q;
		for(int i=0;i<q;i++){
			int a,b;
			cin>>a>>b;
			if(find(a)==find(b))
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	return 0;
}

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

const int MAX = 100000;
vector<int> friends[MAX + 1];
int groups[MAX + 1];

void dfs(int id, int gid) {
  if (groups[id] != -1) return;
  groups[id] = gid;

  int l = friends[id].size();
  for (int i = 0; i < l; i++) {
    dfs(friends[id][i], gid);
  }
}

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

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

  int a, b, q;
  int gid = 0;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    friends[a].push_back(b);
    friends[b].push_back(a);
  }

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

  for (int i = 0; i < n; i++) {
    if (groups[i] == -1) dfs(i, gid++);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (groups[a] == groups[b]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

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

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = INT_MAX;
const int N = 100000;
const int M = 100000;
int n, m, q;
vector<edge> es[N];
int d[N];

void dijkstra(int s) {
  int i;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  q.push(PII(d[s], s));

  while (q.size()) {
    PII p = q.top();
    q.pop();
    int v = p.second;

    for (i=0; i<(int)es[v].size(); i++) {
      edge e = es[v][i];
      if (d[e.to]==-1) {
        d[e.to] = d[v];
        q.push(PII(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int i;
  for (i=0; i<N; i++) es[i].clear();
  memset(d, 0xff, sizeof(d));

  scanf("%d%d ", &n, &m);
  for (i=0; i<m; i++) {
    int u, v;
    scanf("%d%d ", &u, &v);
    es[u].psb(edge(u, v, 0));
    es[v].psb(edge(v, u, 0));
  }

  int c = 0;
  for (i=0; i<n; i++)
    if (d[i]==-1)  {
      d[i] = c++; 
      dijkstra(i);
    }

  scanf("%d ", &q);
  for (i=0; i<q; i++) {
    int l, r;
    scanf("%d%d ", &l, &r);
    if (d[l]==d[r])
      printf("yes\n");
    else
      printf("no\n");
  }

	return 0;
}