Go: package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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>
#define rep(i,n) for(int i=0;i< (n);i++)
using namespace std;
typedef long long ll;

struct BFS{
    vector<int> color;
    vector<vector<int>> adj;
    int n_,m_;
    BFS(int n,int m):color(n,-1),adj(n){
        n_ = n;
        m_ = m;
    }
    void init(){
        rep(i,m_){
            int a,b;cin>>a>>b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
    }
    void visit(int u,int clr){
        queue<int> Q;
        Q.push(u);
        color[u] = clr;
        while(!Q.empty()){
            int v = Q.front();Q.pop();
            color[v] = clr;
            for(int h:adj[v]){
                if(color[h] == -1){
                    Q.push(h);
                }
            }
        }
    }
    void go(){
        int clr_ = 0;
        rep(i,n_){
            if(color[i] == -1){
                visit(i,clr_);
                clr_++;
            }
        }
    }
    void solve(){
        init();
        go();
        int q;cin>>q;
        rep(i,q){
            int a,b;cin>>a>>b;
            if(color[a] == color[b]){
                cout<<"yes"<<endl;
            }else{
                cout<<"no"<<endl;
            }
        }
    }
};

int main(){
    int n,m;cin>>n>>m;
    BFS bfs(n,m);
    bfs.solve();
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define MAX_NODE 100000
#define MAX_EDGE 100000

using namespace std;

int n;//num of nodes
int e;//num of edges
int group[MAX_NODE+1];//0オリジン
int q;

bool neighbor_a[MAX_NODE+1];
vector<int> neighbor_v[MAX_NODE+1];//連接リスト

void addNeighbor(int num,int ori){//oriを含む連結グラフをつくる
	int ngb;
	for(int i = 0;i < neighbor_v[num].size();i++){//深さ優先探索
		if(neighbor_a[neighbor_v[num][i]] == false){
			neighbor_a[neighbor_v[num][i]] = true;
			neighbor_v[ori].push_back(neighbor_v[num][i]);
			ngb = neighbor_v[num][i];
			addNeighbor(ngb,ori);
		}
	}
}


int main(){
	int v_1,v_2;
	scanf("%d %d",&n,&e);

	for(int i = 0;i < e;i++){
		scanf("%d %d",&v_1,&v_2);
		neighbor_v[v_1].push_back(v_2);
		neighbor_v[v_2].push_back(v_1);
	}

	for(int i  = 0;i < n;i++) group[i] = -1;

	for(int i = 0;i < n;i++){
		if(group[i] != -1) continue;

		for(int j = 0;j < n;j++) neighbor_a[j] = false;//iの連接配列

		addNeighbor(i,i);

		for(int j = 0;j < neighbor_v[i].size();j++) group[neighbor_v[i][j]] = i;

	}
	//for(int i = 0;i < n;i++) cout <<group[i] <<" ";
	//for(int i = 0;i < neighbor_v[5].size();i++) cout <<neighbor_v[5][i] <<" ";

	scanf("%d",&q);

	for(int i = 0;i < q;i++){
		scanf("%d %d",&v_1,&v_2);
		if(group[v_1] == group[v_2]) cout <<"yes" <<endl;
		else cout <<"no" <<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 100000

struct Node{
	int group;
	queue<int> fr;
};

int N, M, Q, group_num = 0;
Node person[MAX];

void isFriend(int a, int b){
	if(person[a].group < 0 && person[b].group < 0){
		int p = a, next, m;
		queue<int> move;
		group_num++;
		person[p].group = group_num;
		//cout << a << " is group " << person[a].group << endl;
		move.push(p);

		while(!move.empty()){
			p = move.front();
			if(person[p].fr.empty()){
				move.pop();
			} else {
				m = person[p].fr.size();
				while(m--){
					next = person[p].fr.front();
					person[p].fr.pop();
					if(person[next].group < 0){
						person[next].group = group_num;
						//cout << next << " is group " << person[next].group << endl;
						move.push(next);
					}
				}
				move.pop();
			}
		}

		if(person[a].group == person[b].group){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	} else if(person[a].group == person[b].group){
		cout << "yes" << endl;
	} else {
		cout << "no" << endl;
	}
}

int main(){
	cin >> N >> M;
	for(int i = 0; i < N; i++)
		person[i].group = -1;
	while(M--){
		int a, b;
		cin >> a >> b;
		person[a].fr.push(b);
		person[b].fr.push(a);
	}
	cin >> Q;
	while(Q--){
		int a, b;
		cin >> a >> b;
		isFriend(a, b);
	}
	return 0;
}

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

bool DFS(const vector<vector<int> >& G, int a, int b, int i) {
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    return DFS(G, G[a][i], b, 0) || DFS(G, a, b, i + 1);
}

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

    vector<vector<int> > G(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        vector<vector<bool> > visited(false);
        if (DFS(G, a, b, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

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

#define MAX 100000


using namespace std;


int gcol[MAX];

int main(void)
{
	int n;
	int m;
	int s;
	int t;
	int q;
	int type = 1;



	cin >> n >> m;

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


		if (gcol[s] != 0 && gcol[t] != 0) {
			for (int j = 0; j < n; i++) {
				if (gcol[s] == gcol[j]) {
					gcol[j] = gcol[s];
				}
			}
		}
		if (gcol[s] != 0) {
			gcol[t] = gcol[s];
		}
		else if (gcol[t] != 0) {
			gcol[s] = gcol[t];
		}
		else {
			gcol[s] = gcol[t] = type;
			type++;
		}


	}

	cin >> q;

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

		if (gcol[s] == gcol[t] && gcol[s] != 0) {
			cout << "yes";
		}
		else {
			cout << "no";
		}

		cout << endl;

	}

	return 0;
}

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

typedef vector<int> v;
struct graph {
	vector<v> nodes;

	graph(int n) {
		nodes.resize(n);
	}
	void read(int n) {
		while (n--) {
			int s = in(), t = in();
			nodes[s].push_back(t);
			nodes[t].push_back(s);
		}
	}
	bool find(int s, int t, v &f) {
		if (s == t)
			return true;
		f[s] = 1;
		for (int x: nodes[s])
			if (!f[x] && find(x, t, f))
				return true;
		return false;
	}
	bool find(int s, int t) {
		v foot_stamp(nodes.size());
		return find(s, t, foot_stamp);
	}
};

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