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++: #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;
}

C++: #include <iostream>
#include <cstdio>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#define MAX 100000
#define INFTY 1<<30 // 2^30
using namespace std;
typedef long long llong;
 
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 assingColor()
{
  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, m ,q;
  cin >> n >> m;
  for(int i = 0; i < m; i++)
  {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
 
  assingColor();
 
  cin >> q;
  for(int i = 0; i < q; i++)
  {
    cin >> s >> t;
    cout << ((color[s] == color[t]) ? "yes" : "no") << endl;
  }
  return 0;
}