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