Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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