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 <cstdio>
#include <cctype>
#include <vector>
using namespace std;

#define si static inline
#define gcu getchar_unlocked
si int in() {
	int c, n = 0;
	//bool minus = false;
	//if ((c = gcu()) == '-') minus = true; else ungetc(c, stdin);
	while ((c = gcu()) >= '0' && c <= '9') n = 10 * n + (c - '0');
	return n; }
	//return minus ? -n : n; }
si void scan(char *s) {while (!isspace(*s++ = gcu()));}
#define pcu putchar_unlocked
#define svo si void out
template <typename T>
svo(T n) {
	static char buf[20];
	char *p = buf;
	//if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
svo(const char *s){while(*s)pcu(*s++);}
svo(char *s){while(*s)pcu(*s++);}
svo(char c){pcu(c);}
template <typename T>
//svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
template <typename head, typename... tail>
svo(head&& h, tail&&... t){out(h);out(move(t)...);}

struct union_find {
	struct node {int p, r = 0;};
	vector<node> nodes;

	union_find(int n) {
		nodes.resize(n);
		int i = 0;
		for (node &n: nodes)
			n.p = i++;
	}
	int find(int x) {
		int &p = nodes[x].p;
		if (p == x)
			return x;
		else {
			p = find(p);
			return p;
		}
	}
	void _union(int a, int b) {
		int ar = find(a), br = find(b);
		node &an = nodes[ar], &bn = nodes[br];
		if (an.r > bn.r)
			bn.p = ar;
		else if (an.r > bn.r)
			an.p = br;
		else if (ar != br) {
			bn.p = ar;
			an.r++;
		}
	}
	void read(int n) {
		int a;
		while(n--) {
			a = in();
			_union(a, in());
		}
	}
};

int main() {
	union_find u(in());
	u.read(in());
	int q = in();
	while (q--)
		out(u.find(in()) == u.find(in()) ? "yes" : "no", '\n');
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_set>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< vector<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< vector<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).push_back(t);
		friendGraphList.at(t).push_back(s);
	}
	for (auto& v : friendGraphList)v.shrink_to_fit();
}

void accesibleNodeSearch(const vector<vector<int>>& graphList, vector<vector<int>>& visitMatrix, const int s, vector<bool>& visitList) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	fill(visitList.begin(), visitList.end(), false);
	nodeQueue.push(s);
	visitList.at(s).flip();
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			if (visitList.at(adjID))continue;
			const vector<int>& idList = graphList.at(adjID);
			if (find(idList.begin(), idList.end(), id) == idList.end())continue;
			nodeQueue.push(adjID);
			visitList.at(adjID).flip();
		}
		nodeQueue.pop();
	}
	const int num = count(visitList.begin(), visitList.end(), true);
	visitMatrix.at(s).reserve(num);
	for (int i = 0; i != n; ++i) {
		if (!visitList.at(i))continue;
		visitMatrix.at(s).push_back(i);
	}
}

void buildFriendAccesibility(const vector<vector<int>>& graphList, vector<vector<int>>& visitMatrix) {
	const int n = graphList.size();
	visitMatrix.resize(n, vector<int>());
	vector<bool> visitList(n);
	for (int i = 0; i != n; ++i) {
		accesibleNodeSearch(graphList, visitMatrix, i, visitList);
	}
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	vector<vector<int>> visitMatrix;
	buildFriendAccesibility(graphList, visitMatrix);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << ( find(visitMatrix.at(s).begin(), visitMatrix.at(s).end(),t) != visitMatrix.at(s).end() ? "yes" : "no") << endl;
	}
}

int main()
{


	execute(cin);



	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define NMAX 100000
using namespace std;

struct Node {
  vector<int> friends;
};
int group[NMAX];

void bfs(vector<Node> &nodes, int id) {
  static int groupno = 1;
  queue<int> q;
  q.push(id);
  set<int> st;

  group[id] = groupno;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < nodes[u].friends.size(); i++) {
      int fid = nodes[u].friends[i];
      if (group[fid] == -1) {
        q.push(fid);
        group[fid] = groupno;
      }
    }
  }
  groupno++;
}

int main() {
  vector<Node> nodes;
  int nrel, nuser;

  cin >> nuser;
  cin >> nrel;
  nodes.resize(nuser);
  int tmp1, tmp2;
  for (int i = 0; i < nrel; i++) {
    cin >> tmp1 >> tmp2;
    nodes[tmp1].friends.push_back(tmp2);
    nodes[tmp2].friends.push_back(tmp1);
  }
  for (int i = 0; i < nodes.size(); i++) {
    group[i] = -1;
  }

  int q, s, t;
  cin >> q;

  for (int i = 0; i < q; i++) {
    scanf("%d%d", &s, &t);
    if (group[s] == -1)
      bfs(nodes, s);
    printf("%s\n", group[s] == group[t] ? "yes" : "no");
  }
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int n,m;
vector <int> rel[100000];
int id[100001];
int visit[100001] = {0};
int s,t;


void check(int x, int u) { 
 visit[x] = 1;
  id[x] = u;

  int v ;
  for(int i=0;i<rel[x].size();i++) {
    v = rel[x][i];
    if(visit[v] == 0) {
      check(v,u);
    }
  }
}
    
  

int main() {

 

  cin >> n >> m;

 
  int a,b;


  for(int i=0;i<m;i++) {


    cin >> a >> b;

    rel[a].push_back(b);
    rel[b].push_back(a);

  }
  
  for(int i =0;i<n;i++) {
    if(visit[i] == 0) {
      check(i,i);
    }
  }
  

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

  return 0;

}