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;
typedef long long ll;
typedef vector<vector<int> > vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<string, int> psi;
typedef pair<int, int> pii;
#define rep(i,s,n) for(int i = (s); i < (n); i++)
#define loop(i) for(int i = 0;true; i++)
#define all(x) (x).begin(), (x).end()
#define yn(f) (f?"yes":"no")
#define YN(f) (f?"YES":"NO")
#define Yn(f) (f?"Yes":"No")
#define mn 1000000007
int main() {
	int N, M;
	cin >> N;
	cin >> M;
	vvi A(N);
	for (int i = 0; i < M; i++)
	{
		int l, r;
		scanf("%d", &l);
		scanf("%d", &r);
		A[l].push_back(r);
		A[r].push_back(l);
	}
	vi C(N, -1);
	vi v(N, 0);
	int num = 0;
	for (int i = 0; i < N; i++)
	{
		if (v[i]) {
			continue;
		}
		else {
			v[i] = 1;
			num++;
			C[i] = num;
		}
		stack<int> S;
		S.push(i);
		while (!S.empty()) {
			int temp = S.top();
			S.pop();
			for (int j = 0; j < A[temp].size(); j++)
			{
				if (v[A[temp][j]] == 0) {
					S.push(A[temp][j]);
					v[A[temp][j]] = 1;
					C[A[temp][j]] = num;
				}
			}

		}
	}
	for (int i = 0; i < N; i++)
	{
		
	}
	int k;
	cin >> k;
	for (int i = 0; i < k; i++)
	{
		int l, r;
		scanf("%d", &l);
		scanf("%d", &r);

		printf("%s\n", yn(C[l] == C[r]));
	}
};

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct node {
    int id;
    vector<int> rel;
};

struct memo {
	int a, b;
	bool val;
	bool operator==(memo m){
		return (this->a == m.a && this->b == m.b) || (this->a == m.b && this->b == m.a);
	}
};
 
int n, m, q;
node* nodes;
vector<memo> ms;

bool canReach(int s, int d, bool* h){
    h[s] = true;
	if(s==d) return true;
	
	memo nm;
	nm.a = s;
	nm.b = d;
	
	vector<memo>::iterator it = find(ms.begin(), ms.end(), nm);
	if(it != ms.end()) return (*it).val;
	
    for(int i = 0; i < nodes[s].rel.size(); i++){
		if(!h[nodes[s].rel[i]] && canReach(nodes[s].rel[i], d, h)){
			nm.val = true;
			ms.push_back(nm);
			return true;
        }
    }
	
	nm.val = false;
	ms.push_back(nm);
    return false;
}
 
int main() {
    scanf("%d %d", &n, &m);
     
    nodes = new node[n];
    for(int i = 0; i < n; i++){
        nodes[i].id = i;
    }
    while(m--){
        int l, r;
        scanf("%d %d", &l, &r);
        nodes[l].rel.push_back(r);
        nodes[r].rel.push_back(l);
    }
     
    scanf("%d", &q);
    while(q--){
        int l, r;
        scanf("%d %d", &l, &r);
         
        bool *h = new bool[n];
        for(int i = 0; i < n; i++){
            h[i] = false;
        }
        printf("%s\n", canReach(l, r, h) ? "yes" : "no");
        delete[] h;
    }
    return 0;
}

C++: def dfs(s):
    global t
    if s == t:
        return True
    DP[s]=1
    for i in F[s]:
        if not DP[i]:
            if dfs(i):
                return True
    return False
    
n,m = [int(i) for i in input().split()]
F = [{}]*n
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
for i in range(q):
    DP = [0]*n
    s,t = [int(i) for i in input().split()]
    if dfs(s):
        print("yes")
    else:
        print("no")

C++: #include <iostream>
#include <vector>
using namespace std;

struct node {
    int to;
    node *next;
};

node *adj[100000], fm[200000];
int g[100000];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, i;
    cin>>n>>m;
    m+=m;
    int s, t;
    i=0;
    while(i<m) {
        cin>>s>>t;
        fm[i].to=t;
        fm[i].next=adj[s];
        adj[s]=&fm[i];
        i++;
        fm[i].to=s;
        fm[i].next=adj[t];
        adj[t]=&fm[i];
        i++;
    }
    int group=1;
    vector<int> st;
    st.reserve(n);
    for(i=0; i<n; i++) {
        if(g[i]) continue;
        g[i]=group;
        st.push_back(i);
        while(!st.empty()) {
            node *p=adj[st.back()];
            g[st.back()]=group;
            st.pop_back();
            while(p) {
                if(g[p->to]==0) st.push_back(p->to);
                p=p->next;
            }
        }
        group++;
    }
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>s>>t;
        if(g[s]==g[t]) cout<<"yes\n";
        else cout<<"no\n";
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

#define MAX_N 200000

int par[MAX_N];
int rnk[MAX_N];

// 初期化
void init(int n){
	for(int i = 0; i < n; i++){
		par[i] = i;
		rnk[i] = 0;
	}
}

// x の親を返す
int find(int x){
	if(par[x] == x){
		return x;
	}
	return par[x] = find(par[x]);
}

// x と y を併合する
void unite(int x, int y){
	int x_par = find(x);
	int y_par = find(y);
	if(x_par == y_par){
		return;
	}
	
	if(rnk[x_par] < rnk[y_par]){
		par[x_par] = y_par;
	}else{
		par[y_par] = x_par;
		if(rnk[x_par] == rnk[y_par]){
			rnk[x_par]++;
		}
	}
}

// x, y が同じグループか判定
bool same(int x, int y){
	return (find(x) == find(y));
}

int main(){
	
	int n, m;
	cin >> n >> m;
	
	init(n);
	
	for(int i = 0; i < m; i++){
		int x, y;
		cin >> x >> y;
		unite(x, y);
	}
	
	int q;
	cin >> q;
	
	for(int i = 0; i < q; i++){
		int x, y;
		cin >> x >> y;
		if(same(x, y)){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}
	
	return 0;
}