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 <stack>
using namespace std;

const long N = 100;
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;
const long NIL = -1;

long n, M[N][N];
long color[N], cc[N], cp = 0;
long nt[N];

long next(long u){
	for (long v = nt[u]; v <= n; v++){
		nt[u] = v + 1;
		if (M[u][v]) return v;
	}
	return -1;
}

void dfs_visit(long r){
	for (long i = 0; i < n; i++) nt[i] = 0;

	stack<long> S;
	S.push(r);
	color[r] = GRAY;
	if (cc[r] == NIL) cc[r] = cp;

	while (!S.empty()){
		long u = S.top();
		long v = next(u);
		if (v != -1){
			if (color[v] == WHITE){
				color[v] = GRAY;
				if (cc[v] == NIL) cc[v] = cp;
				S.push(v);
			}
		}
		else {
			S.pop();
			color[u] = BLACK;
		}
	}
	cp++;
}

void dfs(){
	for (long i = 0; i < n; i++){
		color[i] = WHITE;
		nt[i] = 1;
	}

	for (long u = 0; u < n; u++){
		if (color[u] == WHITE) dfs_visit(u);
	}

}

int main(){
	long m, q;
	cin >> n >> m;
	for (long i = 0; i < n; i++){
		cc[i] = NIL;
		for (long j = 0; j < n; j++){
			M[i][j] = 0;
		}
	}
	for (long i = 0; i < m; i++){
		long s, t;
		cin >> s >> t;
		M[s][t] = M[t][s] = 1;
	}
	dfs();
	cin >> q;
	for (long i = 0; i < q; i++){
		long s, t;
		cin >> s >> t;
		if (cc[s] == cc[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

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

typedef struct{
	bool isVisited;
	std::vector<int> neighbors;
} node;

void dfs(node* nodes, int id, int t){

	for (int i=0; i<nodes[id].neighbors.size(); i++){
		if (nodes[t].isVisited) break;
		if (!nodes[nodes[id].neighbors[i]].isVisited){
			nodes[nodes[id].neighbors[i]].isVisited=true;
			dfs(nodes, nodes[id].neighbors[i], t);
		}
	}
}

int main(){
	int n, d, num_relation, n_query, s, t;
	node *nodes;

	scanf("%d %d", &n, &num_relation);
	nodes = new node[n];
	for(int i=0;i<num_relation;i++){
		scanf("%d %d", &s, &t);
		nodes[s].neighbors.push_back(t);
		nodes[t].neighbors.push_back(s);
	}

	scanf("%d", &n_query);
	for(int i=0;i<n_query;i++){
		for (int j=0; j<n;j++){
			nodes[j].isVisited=false;
		}
		scanf("%d %d", &s, &t);
		dfs(nodes, s, t);
		if (nodes[t].isVisited) printf("yes\n");
		else printf("no\n");

	}

	delete []nodes;
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int from[100000];
int to[100000];

bool checked[100000];

bool judge(int f, int t,int m)
{
	if (checked[f])
	{
		return false;
	}

	checked[f] = true;

	bool isFriend = false;
	for (int i = 0; i < m; i++)
	{
		if (from[i] == f)
		{
			if (to[i] == t)
			{
				isFriend = true;
			}
			else
			{
				isFriend = judge(to[i], t, m);
			}
			if (isFriend)
			{
				break;
			}
		}
		if (to[i] == f)
		{
			if (from[i] == t)
			{
				isFriend = true;
			}
			else
			{
				isFriend = judge(from[i], t, m);
			}
			if(isFriend)
			{
				break;
			}
		}
	}
	return isFriend;
}

int main()
{
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		int f, t;
		cin >> f >> t;

		from[i] = f;
		to[i] = t;
	}

	int q;
	cin >> q;

	for (int i = 0; i < q; i++)
	{
		int f, t;
		cin >> f >> t;

		for (int i = 0; i < n; i++)
		{
			checked[i] = false;
		}

		printf(judge(f, t, m) ? "yes\n" : "no\n");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
#define TO(x,t,f) ((x)?(t):(f))
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define FILV(x,a) fill(x.begin(),x.end(),a)
#define FILA(ar,N,a) fill(ar,ar+N,a)
#define NEXTP(x) next_permutation(x.begin(),x.end())
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define pb push_back
#define pri(aa) cout<<(aa)<<endl
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
const ll INF=1e18+7;
const ll MOD=1e9+7;
const ll N = 1007;
class UnionFind{
public:
	vector<int> par;
	//0-indexed
	void init(int n = 1) {
		par.resize(n);
		for (int i = 0; i < n; ++i) par[i] = i;
	}
	int root(int x = 1){
		if(par[x]==x)return x;
		else{
			return par[x] = root(par[x]);
		}
	}
	void unite(int x = 1,int y = 1){
		x = root(x);
		y = root(y);
		if(x==y)return;
		else par[x] = y;
	}
};
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,m;
	cin>>n>>m;
	UnionFind uf;
	uf.init(n);
	FOR(i,0,m){
		ll x,y;
		cin>>x>>y;
		uf.unite(x,y);
	}
	ll q;
	cin>>q;
	FOR(i,0,q){
		ll x,y;
		cin>>x>>y;
		if(uf.root(x)==uf.root(y)){
			pri("yes");
		}else{
			pri("no");
		}
	}
}

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

#define int long long
 	
const int N = 1e5 + 11;
int p[N], sz[N];

int get(int x){
  if (p[x] == x) return x;
  return p[x] = get(p[x]);	
}

void unite(int x, int y){
   x = get(x), y = get(y);
   if(sz[x] < sz[y]) swap(x, y);
   sz[x] += sz[y];
   p[y] = x;
}

int32_t main(){
	int n, m, q;
	cin >> n >> m;
	for(int i = 0; i < n; i++){
	   p[i] =  i;
	   sz[i]  = 0;	
	}
	for(int i = 0; i < m; i++){
	   int x, y;
	   cin >> x >> y;
	   unite(x, y);
	}
   cin >> q;
   for(int i = 0; i < q; i++){
	  int start, end;
	  cin >> start >> end;
	  if (get(start) == get(end)) cout << "yes" << endl;
	  else  cout << "no" << endl;   
	}
}