Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int N;
vector<int> G[100000];
int color[100000];

void dfs(int s, int c){
	stack<int> S;
	S.push(s);
	color[s] = c;
	while(!S.empty()){
		int u = S.top();
		S.pop();
		rep(i, G[u].size()){
			int p = G[u][i];
			if(color[p] == 0){
				color[p] = c;
				S.push(p);
			}
		}
	}
}

void colorAssign(){
	rep(i, N){
		color[i] = 0;
	}
	int id = 1;
	rep(i, N){
		if(color[i] == 0){
			dfs(i, id);
			id++;
		}
	}

}

int main(){
	int m, s, t;
	cin >> N >> m;
	rep(i, m){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	colorAssign();
	int q;
	cin >> q;
	rep(i, q){
		cin >> s >> t;
		if(color[s] == color[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

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

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<int> d(n.size()), dd(n.size());
	for (size_t i = 0; i < n.size(); i++) {
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		if (dd[i] == 1)continue;
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
					std::vector<int>::iterator itt= std::find(n[*it].relation.begin(), n[*it].relation.end(), num);
					if (itt != n[*it].relation.end()) {
						n[*it].relation.erase(itt);
					}
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
		if (from == to) return true;
	}
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);
	makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
	std::cout << i << " " << n[i].parent << std::endl;
	}
	*/
	/*
	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	//*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

C++: //
// Created by 李青坪 on 2018/11/4.
//

#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>

#define MAXN 100001
#define NIL -1
using namespace std;

vector<int > compo[MAXN];
int group[MAXN];
queue<int> queue1;

void init(int n){
    fill(group, group+n, NIL);
    queue1 = queue<int>();
}

void query(int node, int color){
    queue1.push(node);
    group[node] = color;
    int ex;
    while (!queue1.empty()){
        ex = queue1.front();
        queue1.pop();
        for (auto comp: compo[ex]){
            if (group[comp] != NIL) continue;
            group[comp] = color;
            queue1.push(comp);
        }
    }
}

void partitionGroup(int n){
    int c = 0;
    for (int i = 0; i < n; ++i) {
        queue1 = queue<int>();
        if (group[i] == NIL) query(i, c++);
    }
}

int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    int s, t;
    for (int i = 0; i < m; ++i) {
        scanf("%d %d", &s, &t);
        compo[s].push_back(t);
        compo[t].push_back(s);
    }
    init(n);
    partitionGroup(n);
    int q;
    scanf("%d", &q);
    for (int j = 0; j < q; ++j) {
        scanf("%d %d", &s, &t);
//        printf("%d", group[s]);
        if (group[s] == group[t]) printf("yes\n");
        else printf("no\n");
    }
}
/*
10 5
0 1
1 2
2 9
3 4
6 7
12
0 1
0 2
0 9
3 4
6 7
7 6
0 5
0 6
3 7
3 5
5 8
8 5
 */