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 <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

class Graph
{
public:
    Graph() {}
    Graph(int n)
    : con(vs32(n, -1))
    {}
    ~Graph() {}

    bool path(int u, int v)
    {
        bool ret;

        int repu = getRep(u);
        int repv = getRep(v);
        if (repu != repv) ret = false;
        else /*if (repu == repv)*/ ret = true;

        return ret;
    }

    void setPath(int u, int v)
    {
        int repu = getRep(u);
        int repv = getRep(v);


        if (repu == repv) return;
        if (con[repu] > con[repv])
        {
            swap(repu, repv);
        }
        con[repu] += con[repv];
        con[repv] = repu;
    }

    int getRep(int u)
    {
        int ret;
        if (con[u] >= 0)
        {
            con[u] = getRep(con[u]);
            ret = con[u];
        }
        else
        {
            ret = u;
        }
        return ret;
    }

    vs32 con;
};

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

    int n;
    cin >> n;

    Graph g(n);
    int m;
    cin >> m;
    int u, v;
    rep (i, m)
    {
        cin >> u >> v;
        g.setPath(u, v);
    }

    int q;
    cin >> q;
    rep (i, q)
    {
        cin >> u >> v;
        if (g.path(u, v))
        {
            cout << "yes\n";
        }
        else
        {
            cout << "no\n";
        }
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q; int COLOR[MAX_N], N, M, Q1, A, B;
pair<int, int> R[MAX_N]; vector<pair<int, int>>S[MAX_N]; int T[MAX_N];
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); }
	for (int i = 0; i < Q1; i++) {
		cin >> R[i].first >> R[i].second; S[R[i].first].push_back(make_pair(R[i].second, i));
		S[R[i].second].push_back(make_pair(R[i].first, i));
	}
	for (int i = 0; i < N; i++) {
		Q.push(i); for (int j = 0; j < N; j++) { COLOR[j] = 0; }
		while (!Q.empty()) {
			int B = Q.front(); Q.pop();
			for (int j = 0; j < X[B].size(); j++) {
				if (COLOR[X[B][j]] == 0) { COLOR[X[B][j]] = 1; Q.push(X[B][j]); }
			}
		}
		for (int j = 0; j < S[i].size(); j++) {
			int a1 = S[i][j].first, a2 = S[i][j].second;
			if (COLOR[a1] == 1) { T[a2]++; }
		}
	}
	for (int i = 0; i < Q1; i++) {
		if (T[i] == 2) { cout << '1' << endl; }
		else { cout << '0' << endl; }
	}
	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;
  int i, v, u;
  S.push(r);
  color[r]=c;
  while (! S.empty()) {
    u=S.top();
    S.pop();
    for (i=0; i<G[u].size(); i++) {
        v=G[u][i];
      if (color[v]==NIL) {
        color[v]=c;
        S.push(v);
      }
    }
  }
}



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

int main() {

  int  m, i, j, v, s, t, q;

  cin >> n >> m;

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

  assignColor();

  cin >> q;

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

  return 0;

}

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

#define MAX 100000
#define WHITE -1

int n, m, q;
vector<int> G[MAX];
int col[MAX];

void dfs(int s, int c) {
	col[s] = c;
	for (int v = 0; v < G[s].size(); v++) {
		if (col[G[s][v]] == WHITE)
			dfs(G[s][v], c);
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		col[i] = WHITE;
	}
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	int id = 0;
	for (int i = 0; i < n; i++) {
		if (col[i] == WHITE) {
			dfs(i, ++id);
		}
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		cout << (col[s] == col[t] ? "yes" : "no") << endl;
	}
	return 0;
}

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

bool isConnected(int id_from, int id_to, std::vector<std::set<int>> firend_relationships)
{
  std::queue<int> next_queue;
  std::set<int> searched_set;
  next_queue.push(id_from);

  while (!next_queue.empty())
  {
    int current_id = next_queue.front();
    next_queue.pop();
    searched_set.insert(current_id);

    std::set<int>::const_iterator itr;
    for (itr = firend_relationships[current_id].begin();
         itr != firend_relationships[current_id].end();
         ++itr)
    {
      // Ignore searched ones
      if (searched_set.count(*itr) != 0)
        continue;

      // Found
      if (*itr == id_to)
        return true;

      next_queue.push(*itr);
    }
  }

  // Not found
  return false;
}

int main()
{
  int n, m;
  scanf("%d%d", &n, &m);

  std::vector<std::set<int>> firend_relationships;
  firend_relationships.resize(n);
  for (int i = 0; i < m; ++i)
  {
    int id1, id2;
    scanf("%d%d", &id1, &id2);
    firend_relationships[id1].insert(id2);
    firend_relationships[id2].insert(id1);
  }

  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i)
  {
    int id1, id2;
    scanf("%d%d", &id1, &id2);
    if (isConnected(id1, id2, firend_relationships))
      printf("yes\n");
    else
      printf("no\n");
  }

  return 0;
}

C++: #include<cstdio>

int par[100500];

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

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

void init() {
    for(int i=0; i<100500; i++)
        par[i] = i;
}

int main(void) {
    int n,m,q;
    scanf("%d%d",&n,&m);
    init();
    for(int i=0; i<m; i++) {
        int s,t;
        scanf("%d%d",&s,&t);
        unite(s,t);
    }
    scanf("%d",&q);
    for(int i=0; i<q; i++) {
        int s,t;
        scanf("%d%d",&s,&t);
        puts(find(s) == find(t) ? "yes" : "no");
    }
    return 0;
}