Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 1005

using namespace std;
int n, m, q;
queue<int> Q;
vector<int> A[Lens];
char c[Lens], d[Lens];
int way[Lens][Lens];

void bns(int k)
{
	Q.push(k);
	c[k] = 'g';
	while (!Q.empty())
	{
		k = Q.front();
		Q.pop();
		c[k] = 'g';
		for (int i = 0; i < A[k].size(); i++)
		{
			int tmp = A[k][i];
			if (c[tmp] == 'w')
			{
				Q.push(tmp);
			}
		}
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		                                    c[s] = c[t] = 'w';
		A[s].push_back(t);
		A[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int s, t;
		cin >> s >> t;
		bns(s);
		if (c[s]==c[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

void dfs(int s, int id, vector<vector<int>> a, vector<int> &comp) {
	stack<int> st;
	st.push(s);
	comp[s] = id;
	while (!st.empty()) {
		int i = st.top();
		int k = a[i].size();
		REP(j, k + 1) {
			if (j < k) {
				int t = a[i][j];
				if (comp[t] != id) {
					comp[t] = id;
					st.push(t);
					break;
				}
			}
			else st.pop();
		}
	}
}

void divide(int size, vector<vector<int>> a, vector<int> &comp) {
	int s = 0;
	int id = 1;
	while (1) {
		dfs(s, id, a, comp);
		while (s < size && comp[s]>0) s++;
		if (s == size) break;
		id++;
	}
	return;
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	int q;
	cin >> q;
	vector<int> comp(n, 0);
	divide(n, a, comp);
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		char* res = "no\n";
		if (comp[s] == comp[t]) res = "yes\n";
		printf("%s", res);
	}

	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <list>
using namespace std;

class Node
{
private:
public:
	Node();
	int id;
	list<int> next_node_list;
	int next_idx;
	void AddNextNode(int id);
	void SetGroup(int prev_id, int num);
	int group;
	void Init(int id);
};

Node::Node()
{
	group = 0;
	next_idx = 0;
}

void Node::AddNextNode(int id)
{
	next_node_list.push_back(id);
}

Node *Node_list;

void Node::SetGroup(int prev_id, int num)
{
	group = num;

	list<int>::iterator itr;
	itr = next_node_list.begin();
	for (int i = 0; i < next_idx; i++) {
		itr++;
	}
	
	for(; itr != next_node_list.end(); itr++){
		next_idx++;
		if (prev_id != *itr) {
			Node_list[*itr].SetGroup(id, num);
		}
	}
}

void Node::Init(int id)
{
	this->id = id;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n]();
	for (int i = 0; i < n; i++) {
		Node_list[i].Init(i);
	}
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}

	//group
	int group = 1;
	for (int i = 0; i < m; i++) {
		if (Node_list[i].group) continue;
		Node_list[i].SetGroup(-1, group);
		group++;
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (Node_list[s].group == Node_list[t].group) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int MAX = 100000;

vector<vector<int>> user(MAX, vector<int>(0));
vector<int> group(MAX, -1);

// 模範回答
// void dfs(int start, int group_i) {
//     stack<int> s;
//     group[start] = group_i;
//     s.push(start);
//     while (!s.empty()) {
//         int current = s.top();
//         s.pop();
//         for (int i = 0; i < user[current].size(); i++) {
//             int adj = user[current][i];
//             if (group[adj] == NIL) {
//                 s.push(adj);
//                 group[adj] = group_i;
//             }
//         }
//     }
// }

// 再帰によるdfs
void dfs(int start, int group_i) {
    group[start] = group_i;
    rep(i, user[start].size()) {
        int adj = user[start][i];
        if (group[adj] == -1) {
            dfs(adj, group_i);
        }
    }
}

// スタックによるdfsパターン1
// void dfs(int start, int group_i) {
//     stack<int> s;
//     group[start] = group_i;
//     s.push(start);
//     while (!s.empty()) {
//         int current = s.top();
//         group[current] = group_i;
//         bool stop = true;
//         for (int i = 0; i < user[current].size(); i++) {
//             int adj = user[current][i];
//             if (group[adj] == -1) {
//                 s.push(adj);
//                 stop = false;
//                 break;
//             }
//         }
//         if (stop) s.pop();
//     }
// }

int main() {
    int n, m, s, t, q;
    scanf("%d %d", &n, &m);
    rep(i, m) {
        scanf("%d %d", &s, &t);
        user[s].push_back(t);
        user[t].push_back(s);
    }

    int group_i = 0;
    rep(i, n) {
        if (group[i] == -1) {
            dfs(i, group_i++);
        }
    }

    scanf("%d", &q);
    rep(i, q) {
        scanf("%d %d", &s, &t);
        if (group[s] == group[t]) printf("yes\n");
        else printf("no\n");
    }
}

C++: #include <iostream>
#include <stack>
#include <vector>

using namespace std;

void dfs(vector<vector<int>>& g, vector<int>& color, int id, int c) {
    stack<int> s;
    vector<bool> visited(g.size(), false);
    s.push(id);
    color[id] = c;
    while (!s.empty()) {
        int id = s.top();
        s.pop();
        visited[id] = true;

        for (int i = 0; i < g[id].size(); ++i) {
            int adj = g[id][i];
            if (!visited[adj]) {
                color[adj] = c;
                s.push(adj);
            }
        }
    }
}

void assign_colors(vector<vector<int>>& g, vector<int>& color) {
    int color_id = 1;
    for (int i = 0; i < g.size(); ++i) {
        if (color[i] == -1) {
            dfs(g, color, i, color_id);
            ++color_id;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> g(n);
    for (int i = 0; i < m; ++i) {
        int p1, p2;
        cin >> p1 >> p2;
        g[p1].push_back(p2);
        g[p2].push_back(p1);
    }

    vector<int> color(n, -1);
    assign_colors(g, color);

    int p;
    cin >> p;
    for (int i = 0; i < p; ++i) {
        int p1, p2;
        cin >> p1 >> p2;
        if (color[p1] == color[p2])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}