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>
using namespace std;

class UnionFind {
	vector<int> data;
	int root(int a) {
		return data[a] < 0 ? a : data[a] = root(data[a]);
	}
public:
	UnionFind(int n) : data(n, -1) {}
	bool Find(int a, int b) {
		return root(a) == root(b);
	}
	void Union(int a, int b) {
		int ra = root(a), rb = root(b);
		if (ra == rb) return;
		if (data[ra] < data[rb]) {
			data[ra] += data[rb];
			data[rb] = a;
		}
		else {
			data[rb] += data[ra];
			data[ra] = b;
		}
	}
};

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

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

ll n, m;
vvll edges(100000, vll());
vll group(100000, 0);

void dfs(ll i, ll g) {
  if (group.at(i) != 0) return;
  group.at(i) = g;
  REP(j, edges.at(i).size()) {
    if (group.at(edges.at(i).at(j)) == 0) dfs(edges.at(i).at(j), g);
  }
  return;
}

int main() {
  cin >> n >> m;
  REP(i, m) {
    ll a, b; cin >> a >> b;
    edges.at(a).push_back(b); edges.at(b).push_back(a);
  }
  ll g = 1;
  REP(i, n) {
    if (group.at(i) == 0) dfs(i, g++);
  }
  ll q; cin >> q;
  REP(i, q) {
    ll a, b; cin >> a >> b;
    if (group.at(a) == group.at(b)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int from[100000];
int to[100000];
bool checked[100000];

bool judge(int f, int t,int n)
{
	if (checked[f])
	{
		return false;
	}

	checked[f] = true;
	bool isFriend = false;
	for (int i = 0; i < n; i++)
	{
		if (from[i] == f)
		{
			if (to[i] == t)
			{
				isFriend = true;
			}
			else
			{
				isFriend = judge(to[i], t, n);
			}
			if (isFriend)
			{
				break;
			}
		}
	}
	return isFriend;
}

int main()
{
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		int f, t;
		cin >> f >> t;

		from[i] = f;
		to[i] = t;
	}

	int q;
	cin >> q;

	for (int i = 0; i < q; i++)
	{
		int f, t;
		cin >> f >> t;

		for (int i = 0; i < n; i++)
		{
			checked[i] = false;
		}

		printf(judge(f, t, n) ? "yes\n" : "no\n");
	}
	return 0;
}

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

class vertex{
public:
  vector<vertex*> to;
  bool visited;
  int num;
  int group;

  vertex(){
    visited=false;
  }

  void dfs(int group){
    visited=true;
    this->group = group;
    for(int i=0; i<to.size(); i++){
      if(!to[i]->visited) to[i]->dfs(group);
    }
  }
};

int main(){
  int n, m; cin >> n >> m;
  vertex graph[n];

  //隣接リストにグラフを格納
  int v, k, to;
  for(int i=0; i<m; i++){
    cin >> v >> to;
    graph[v].num = v;
    graph[v].to.push_back(&graph[to]);
    graph[to].num = to;
    graph[to].to.push_back(&graph[v]);
  }

  //頂点0からdfsを行い、グループ分け(各連結成分ごとに1から昇順に番号をつける)
  for(int v=0, group=1; v<n; v++){
    if(!graph[v].visited){
      graph[v].dfs(group);
      group++;
    }
  }

  //回答を得る
  int q, v1, v2; cin >> q;
  bool ans[q];
  for(int i=0; i<q; i++){
    cin >> v1 >> v2;
    if(graph[v1].group==graph[v2].group) ans[i]=true;
    else ans[i]=false;
  }

  //回答出力
  for(int i=0; i<q; i++){
    if(ans[i]) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define N_MAX 100005
#define NIL -1

long n;
vector<long> G[N_MAX];
long color[N_MAX];

void bfs(long i, long id) {
    queue<long> S;
    S.push(i);
    color[i] = id;
    while (S.empty() == false) {
        long u = S.front();
        S.pop();
        for (long j = 0; j < G[u].size(); j++) {
            long v = G[u][j];
            if (color[v] == NIL) {
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void assign_color(long i, long id) {
    bfs(i, id);
}

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

    // for (long i = 0; i < n; i++) {
    //     cout << "Node " << i << ": ";
    //     for (long j = 0; j < G[i].size(); j++) {
    //         cout << " " << G[i][j];
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    for (long i = 0; i < n; i++) {
        color[i] = NIL;
    }

    long id = 0;
    for (long i = 0; i < n; i++) {
        if (color[i] == NIL) {
            assign_color(i, id);
            id++;
        }
    }

    // for (long i = 0; i < n; i++) {
    //     cout << " " << color[i];
    // }
    // cout << endl;

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