Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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

using namespace std;

#define N_MAX 100000

int search_connection(int s, int t);
vector<int> G[N_MAX], check_v;

int main()
{
    int n, m, i, s, t, q;

    cin >> n >> m;

    //??£??\??????????????????
    for (i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    //??????????????????
    cin >> q;
    for (i = 0; i < q; i++) {
//        cout << "Q. " << i << endl;

        cin >> s >> t;

        if (check_v.size() != 0) check_v.clear();

        check_v.push_back(s);
        if ( search_connection(s, t) )
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

int search_connection(int s, int t) {
    int i;
//    cout << "search " << s << ", " << t << endl;

    for (auto itr = G[s].begin(); itr != G[s].end(); itr++) {
        if (*itr == t) {
            return 1;
        }
        else if ( std::find(check_v.begin(), check_v.end(), *itr) != check_v.end() ) {
            //*itr?????¢?????¢?´¢??????node?????´??????????????¢?´¢????????????
        }
        else {
            //*itr???????????¢?´¢???????????????node?????´???
            check_v.push_back(*itr);
            if ( search_connection(*itr, t) )
                return 1;
        }
    }

    return 0;
}

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

using namespace std;

static const int N = 100;

int n, m;
int color[N], dist;

vector<int> G[N];

void dfs(int i, int c){
	queue<int> q; q.push(i);
	color[i] = c;

	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int i = 0; i < G[u].size(); ++i){
			int v = G[u][i];
			if (-1 == color[v]){
				color[v] = c;
				q.push(v);
			}
		}
	}
}


void search(){
	for (int i = 0; i < n; ++i) color[i] = -1;

	int c = 0;
	for (int i = 0; i < n; ++i)	{
		if (color[i] == -1) dfs(i, c++);
	}
}


int main()
{
	cin >> n >> m;

	for (int i = 0; i < m; ++i) {
		int u, v; cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	search();

	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, v; cin >> u >> v;
		if (color[u] == color[v]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <string>

using namespace std;

static const int N = 100000;
static const int NIL = -1;

class ListGraph {
private:
    vector<int> Graph[N];
    int Group[N];
    int n;

public:
    ListGraph()
    {
        cin >> n;
        int m;
        cin >> m;
        int u, v;
        for(int i = 0; i < m; i++)
        {
            cin >> u >> v;
            Graph[u].push_back(v);
            Graph[v].push_back(u);
        }
    }

    void dfs(int start, int group)
    {
        stack<int> stack;
        stack.push(start);
        Group[start] = group;
        while(!stack.empty())
        {
            int u = stack.top();
            stack.pop();
            for(int i = 0; i < Graph[u].size(); i++) // uの全隣接頂点に対して
            {
                int v = Graph[u][i];
                if(Group[v] == NIL) // 未訪問(グループ未定)のとき
                {
                    Group[v] = group; // 自分のグループに含める
                    stack.push(v);
                }
            }
        }
    }

    void grouping()
    {
        int id = 1;
        for(int i = 0; i < n; i++)
            Group[i] = NIL;
        for(int u = 0; u < n; u++)
        {
            if(Group[u] == NIL)
                dfs(u, id++);
        }
    }

    string answer(int start, int goal)
    {
        if(Group[start] == Group[goal])
            return "yes";
        else
            return "no";
    }
};

int main()
{
    ListGraph graph;
    graph.grouping();
    int question;
    cin >> question;
    int start, goal;
    for(int i = 0; i < question; i++)
    {
        cin >> start >> goal;
        cout << graph.answer(start, goal) << endl;
    }
}

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

using namespace std;

struct uft {
    uft (int s) : ps {new int[s]}, ds {new int[s]}, s {s} {
        for(int i = 0; i < s; ++i){
            ps[i] = i;
            ds[i] = 0;
        }
    };
    void unite(int x, int y){
        int px = parent(x);
        int py = parent(y);
        if(ds[px] < ds[py]){
            ps[px] = py;
        } else if(ds[px] > ds[py]){
            ps[py] = px;
        } else {
            ps[px] = py;
            ++ds[py];
        }
    };
    int parent(int p){
        return p == ps[p] ? p : ps[p] = parent(ps[p]);
    };
    bool same(int x, int y){
        return parent(x) == parent(y);
    };
    int* ps;
    int* ds;
    int s;
};

struct user {
    user() {};
    user(int i) : id {i}, fs {}, group {-1} {};
    int id;
    vector<int> fs;
    int group;
};

void add_friends(user *us, int x, int y){
    us[x].fs.push_back(y);
    us[y].fs.push_back(x);
}

void dfs(user* us, int id, int g){
    stack<int> ss {};
    ss.push(id);
    while(ss.size()){
        int p = ss.top();
        ss.pop();
        us[p].group = g;
        vector<int> fs = us[p].fs;
        for(auto x : fs){
            if(us[x].group == g){
                continue;
            }
            ss.push(x);
        }
    }
}

void make_group(user* us, int n){
    int g = 0;
    for(int i = 0; i < n; ++i){
        if(us[i].group < 0){
            dfs(us, i, g);
            ++g;
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    //user us[n];
    auto us = uft(n);
    /*for(int i = 0; i < n; ++i){
        us[i] = user(i);
    }*/
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        us.unite(a, b);
    //    add_friends(us, a, b);
    }
    //make_group(us, n);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++ i){
        int a, b;
        cin >> a >> b;
        //if(us[a].group == us[b].group){
        if(us.same(a, b)){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}