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 <iostream>
#include <vector>
#include <set>

using namespace std;

vector<set<int>> A;

bool dfs(int s, int t) {
    auto& S = A[s];
    if (S.find(t) != S.end()) {
        return true;
    }
    for (auto it = S.begin(); it != S.end(); ++it) {
        if (*it != t) {
            auto i = *it;
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int n;
    int m;
    cin >> n >> m;
    A.resize(n);
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s].insert(t);
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y) || dfs(y, x)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 10000;
int n, m, q;
int M[N][N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, n) {
      if (M[s][i] == 1 && !V[i]) {
        q.push(i);
        V[i] = true;
      }
    }
  }

  return V[v];
}

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

  REP(i, n) {
    REP(j, n) {
      M[i][j] = 0;
    }
  }

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u][v] = M[v][u] = 1;
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

C++: 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;

int n,m,q;
static const int N=100005;
vector<int> G[N];
void branch(int a,int b){
    G[a].push_back(b);
    G[b].push_back(a);
}
int color[N];
void dfs(int r,int c){
    stack<int>s;
    s.push(r);
    while(!s.empty()){
        int u=s.top();s.pop();color[u]=c;
        for(int i=0;i<G[u].size();i++){
            if(color[G[u][i]]==0)s.push(G[u][i]);
        }
    }
}
void assigncolor(){
    int id=1;
    for(int u=0;u<n;u++){
        if(color[u]==0){
            dfs(u,id);
            id++;
        }
    }
}
void check(int a,int b){
    if(color[a]==color[b])printf("yes");
    else printf("no");
    printf("\n");
}
int main() {
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b;cin>>a>>b;
        branch(a,b);
    }
    assigncolor();
    cin>>q;
    for(int i=1;i<=q;i++){
        int a,b;cin>>a>>b;check(a,b);
    }
    return 0;
}

C++: #include<iostream>
#define MAX 1000001
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 <string>
#include <vector>
#include <stack>
#include <iostream>
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++){
      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,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);
  }

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

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 100010;
int n, m;
int u, num, v;
int flag[maxx];
int color[maxx];
vector<int> G[maxx];

int c = 1;
void dfs(int i) {
	stack<int>s;
	s.push(i);
	color[i] = c;
	while (!s.empty()) {
		int u = s.top(); s.pop();
		int len = G[u].size();
		for (int i = 0; i < len; i++) {
			int v = G[u][i];
			if (flag[v] == 0) {
				color[v] = c;
				flag[v] = 1;
				s.push(v);
			}
		}
	}
}
int main()
{
	int q;
	memset(flag, 0, sizeof(flag));
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		scanf("%d %d", &u, &v);
		//?°???¶???v????????°G[u]??°??????
		G[u].push_back(v);
	}
	for (int i = 0; i < n; i++) {
		if (flag[i] == 0) {
			dfs(i);
			c++;
		}
	}
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		scanf("%d %d", &u, &v);
		if (color[u] == color[v]) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}
	return 0;
}

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