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;

// grp[i]:i番目の強連結成分に含まれる頂点一覧 iはトポロジカル順
class SCC {
	public:
	static const int MV = 100000;
	vector<vector<int> > grp; int NV,cmp[MV];
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
	void init(int NV) { this->NV=NV; for(int i=0;i<MV;i++) { E[i].clear(); RE[i].clear();}}
	void add(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) {vis[cu]=1; cmp[cu]=ind; grp[ind].push_back(cu);
		for(int i=0;i<RE[cu].size();++i) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0; grp.clear(); grp.resize(MV); NUM.clear();
		memset(vis,0,sizeof(vis)); for(int i=0;i<NV;i++) if(!vis[i]) dfs(i);
		memset(vis,0,sizeof(vis)); for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]])
			{ grp[c].clear(); revdfs(NUM[i],c); sort(grp[c].begin(),grp[c].end()); c++;}
		grp.resize(c);
	}
};

int n, m, a, b, q, c[100100];
SCC scc;

/*
vector<int> scc(int n, vector<int> e[]) {
	int sz = 0, s = 0;
	vector<int> rg[n], stk, cmp(n, -1), added(n), visited(n), ord(n);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < e[i].size(); ++j) rg[e[i][j]].push_back(i);
		sz += e[i].size();
	}
	stk.resize(n + sz), sz = 0;
	for (int i = 0; i < n; i++) {
		if (visited[i]) continue;
		stk[s = 1] = i;
		while (s != 0) {
			int v = stk[s - 1];
			visited[v] = true;
			bool pushed = false;
			for (int j = 0; j < e[v].size(); ++j) {
				int dst = e[v][j];
				if (!visited[dst]) {
					stk[s++] = dst;
					pushed = true;
				}
			}
			if (pushed) continue;
			int t = stk[s - 1];
			if (!added[t]) added[t] = true, ord[n - ++sz] = t;
			--s;
		}
	}
	int k = 0;
	for (int i = 0; i < n; ++i) {
		if (cmp[ord[i]] != -1) continue;
		s = 0, stk[s++] = ord[i];
		while (s != 0) {
			int v = stk[--s];
			cmp[v] = k;
			for (int j = 0; j < rg[v].size(); ++j) {
				int d = rg[v][j];
				if (cmp[d] == -1) stk[s++] = d;
			}
		}
		++k;
	}
	return cmp;
}
*/

// paint
void rec(int v, int x) {
	c[v] = x;
	for (int i = 0; i < scc.E[v].size(); ++i) {
		if (c[scc.E[v][i]]==0) rec(scc.E[v][i],x);
	}
}

int main() {
	cin >> n >> m;
	scc.init(n);
	for (int i = 0; i < m; ++i) {
		cin >> a >> b;
		scc.add(a,b);
	}
	
	scc.scc();
	int m = 0;
	for (int i = 0; i < scc.grp.size(); ++i) {
		for (int j = 0; j < scc.grp[i].size(); ++j) {
			if (c[scc.grp[i][j]]==0) rec(scc.grp[i][j],++m);
		}
	}
	
	cin >> q;
	while (q--) {
		cin >> a >> b;
		cout << (c[a]==c[b] ? "yes" : "no") << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int id;
    vector<int> children{};
    int visited = 0;
    int depth = -1;
};
 
deque<int> schedule; 
vector<status> graph;

bool bfs(int target_v){
    if (not schedule.empty()){
        
        int now_v = schedule.front();
        schedule.pop_front();
        if(now_v==target_v) return true;
        
        for(auto& next_v:graph[now_v].children){
            if(graph[next_v].visited) continue;
            
            graph[next_v].visited = 1;
            schedule.push_back(next_v);
        }
        return bfs(target_v);
    }
    return false;
}

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //setup
    int nb_v; cin >> nb_v;
    int nb_e; cin >> nb_e;
    graph.resize(nb_v);
    for(int i=0; i<nb_e; ++i){
        int v1, v2; cin >> v1 >> v2;
        graph[v1].children.push_back(v2);
        graph[v2].children.push_back(v1);
    }
    for(auto& a:graph){
        sort(a.children.begin(),a.children.end());
    }
    
    //bfs
    int times; cin>>times;
    for(int i=0; i<times; ++i){
        int from_v; cin>>from_v;
        int to_v; cin>>to_v;
        schedule = {};
        schedule.push_back(from_v);
        cout<<(bfs(to_v)? "yes\n":"no\n");
    }
}

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

using ll = long long;

static const int MAX = 100000;
vector<vector<int>> G;
int color[MAX];

void input_graph(int m) {
    int s, t;
    rep(i, m) {
        scanf("%d %d", &s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
}

void init_graph(int n) {
    rep(i, n) {
        vector<int> vec;
        G.push_back(vec);
    }
}
void bfs(int s, int color_id) {
    queue<int> que;
    que.push(s);
    color[s] = color_id;
    while(que.size() > 0) {
        int u = que.front();
        que.pop();
        rep(i, G[u].size()) {
            int v = G[u][i];
            if(color[v] == -1) {
                que.push(v);
                color[v] = color_id;
            }
        }
    }
}

void color_assign(int n) {
    int color_id = 1;
    rep(i, n) { color[i] = -1; }
    rep(i, n) {
        if(color[i] == -1) {
            bfs(i, color_id++);
        }
    }
}

int main() {
    // 制約見て！！！デカかったらlong longにすること！
    int n, m, q, s, t;
    cin >> n >> m;
    init_graph(n);
    input_graph(m);
    color_assign(n);
    cin >> q;
    rep(i, q) {
        scanf("%d %d", &s, &t);
        if(color[s] == color[t]) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
}