Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
#define MAX_N 1000
int g[MAX_N][MAX_N];
int n;

bool can_connect(int s,int t) {
  bool visited[n];
  queue<int> que;
  visited[s] = true;
  que.push(s);
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(int i=0;i<n;i++) {
      if(g[v][i] != 0 && !visited[i]) {
        if(i == t) return true;
        visited[i] = true;
        que.push(i);
      }
    }
  }
  return false;
}

int main() {
  int m,s,t,q;
  scanf("%d %d\n",&n,&m);
  for(int i=0;i<m;i++) {
    scanf("%d %d",&s,&t);
    g[s][t] = 1;
    g[t][s] = 1;
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++) {
    scanf("%d %d",&s,&t);
    if(can_connect(s,t)){
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

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++) { // id別に連結リスト内を辿る
			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; // 質問用の友人s,t
	int m, q; // 友人の関係がある数ｍ、質問の個数ｑ

	cin >> n >> m; // 頂点の個数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<algorithm>
#define MAX 100
using namespace std;

int main() {
	int i, n, m, q, a, b, gnum = 0, ptog[MAX] = {};

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		if (ptog[a] != 0)ptog[b] = ptog[a];
		else if (ptog[b] != 0)ptog[a] = ptog[b];
		else ptog[a] = ptog[b] = ++gnum;
	}

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

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 1e6;
constexpr int NIL = -1;
int n;
vi G[MAX];
vi color(MAX, NIL);
inline void dfs(int r, int c) {
	deque<int> S;
	S.eb(r);
	color[r] = c;
	int u, v;
	while(!S.empty()) {
		u = S.back(); S.pop_back();
		rep(i, 0, len(G[u])) {
			v = G[u][i];
			if(color[v] == NIL) {
				color[v] = c;
				S.eb(v);
			}
		}
	}
}
inline void assignColor() {
	int id = 0;
	rep(u, 0, n) {
		if(color[u] == NIL) dfs(u, ++id);
	}
}
int main() {
	int m;
	cin >> n >> m;
	int s, t;
	rep(i, 0, m) {
		cin >> s >> t;
		G[s].eb(t);
		G[t].eb(s);
	}
	assignColor();
	int q; cin >> q;
	rep(i, 0, q) {
		cin >> s >> t;
		P(color[s] == color[t] ? "yes" : "no");
	}
	return 0;
}