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

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

struct Node {
	int ID;
	int rep;
	bool visited;
	bool operator<(const Node& rhs) {return this->ID < rhs.ID;}
	bool operator>(const Node& rhs) {return this->ID > rhs.ID;}
	bool operator<=(const Node& rhs) {return this->ID <= rhs.ID;}
	bool operator>=(const Node& rhs) {return this->ID >= rhs.ID;}
	bool operator==(const Node& rhs) {return this->ID == rhs.ID;}
	bool operator!=(const Node& rhs) {return this->ID != rhs.ID;}
};

void BFS(std::vector<int>* adjlist, Node* nodes, std::queue<int> que)
{	
	while (!que.empty()) {
		int id = que.front(); que.pop();
		for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
			if (!nodes[*it].visited) { 
				que.push(*it);
				nodes[*it].rep = nodes[id].rep;
				nodes[*it].visited = true;
			}
		}
	}
}

void DFS(std::vector<int>* adjlist, Node* nodes, int id, int rep)
{
	nodes[id].rep = rep;
	nodes[id].visited = true;
	
	for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
		if (!nodes[*it].visited) DFS(adjlist, nodes, *it, rep);
	}
}

int main()
{
	int n, m; std::cin >> n >> m;
	
	std::vector<int>* adjlist = new std::vector<int>[n];
	Node* nodes = new Node[n];
	
	std::queue<int> que;
	
	for (int i=0; i<n; i++) {
		nodes[i].visited = false;
	}
	
	for (int i=0; i<m; i++) {
		int x, y;
		std::cin >> x >> y;
		adjlist[x].push_back(y);
		adjlist[y].push_back(x);
	}
	
	//std::sort(nodes, nodes + n);
	
	for (int i=0; i<n; i++) {
		if (!nodes[i].visited) {
			//que.push(i); nodes[i].visited = true; nodes[i].rep = i;
			//BFS(adjlist, nodes, que);
			DFS(adjlist, nodes, i, i);
		}
	}
	
	int q; std::cin >> q;
	
	for (int i=0; i<q; i++) {
		int x, y;
		std::cin >> x >> y;
		if (nodes[x].rep == nodes[y].rep) std::cout << "yes" << std::endl;
		else std::cout << "no" << std::endl;
	}
	
	delete[] adjlist;
	delete[] nodes;
	
	return 0;
}