Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<list>

typedef long long ll;

using namespace std;

static const int MAX = 100010;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];
int id;

void dfs(int r , int c){
	stack<int> s;
	s.push(r);
	color[r] = c;
	while (!s.empty()){
		int t = s.top() ; s.pop();
		for(int i = 0 ; i<G[t].size() ; i++){
			int k = G[t][i];
			if(color[k] == NIL){
				color[k] = c;
				s.push(k);
			}
		}
	}
}

void assign(){
	id =1;
	memset(color ,NIL , sizeof(color));
	for(int i = 0 ; i<n ; i++){
		if(color[i] == NIL)
			dfs(i , id++);
	}
}

int main(){
	ios::sync_with_stdio(false);
	int s,t,m,q;
	cin>>n>>m;
	
	for(int i = 0 ; i<m ; i++){
		cin>>s>>t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	
	assign();
	

	cin>>q;
	for(int i = 0 ; i<q ; i++){
		cin>>s>>t;
		if(color[s] == color[t]){
			cout<<"yes"<<endl;
		}
		else{
			cout<<"no"<<endl;
		}
	}	
	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#define N 100010
int father[N], root[N];
int findfather(int x){
    if(root[x]) return father[x];
    else return findfather(father[x]);
}
int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        father[i] = i; root[i] = 1;
    }
    for(int k = 0; k < m; k++){
        int a, b;
        scanf("%d %d", &a, &b);
        root[b] = 0;
        father[b] = findfather(a);
    }
    int q;
    scanf("%d", &q);
    while(q--){
        int x, y;
        scanf("%d %d", &x, &y);
        if(father[x] == father[y]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}