Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;

vector<int>cc;
void dfs(vector<vector<int>> &g,int v,int id){
    cc[v]=id;
    for(auto e:g[v]){
        if(cc[e]==-1){
            dfs(g,e,id);
        }
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> g(n);
    for(int i=0;i<m;++i){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int q;
    cin>>q;
    vector<int> s(q),t(q);
    for(int i=0;i<q;++i){
        cin>>s.at(i)>>t.at(i);
    }
    cc.assign(n,-1);
    int id=0;
    for(int i=0;i<n;++i){
        if(cc[i]==-1){
            dfs(g,i,id);
            id++;
        }
    }
    for(int i=0;i<q;++i){
        if(cc[s[i]]==cc[t[i]])cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

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<std::vector<int> > nn(n.size());
	std::vector<int> d(n.size()), dd(n.size());
	int count = 0;
	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		nn[count].push_back(i);
		n[i].parent = -1;
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		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 {
					nn[count].push_back(*it);
					s.push(*it);
					d[*it] = 1;
					n[*it].parent = num;
					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);
					}
				}
			}
		}
		count++;
	}

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		for (int j = 0; j < count; j++) {
			if (std::find(nn[j].begin(), nn[j].end(), from) != nn[j].end() &&
				std::find(nn[j].begin(), nn[j].end(), to) != nn[j].end()) {
				std::cout << "yes" << std::endl;
				break;
			}
			if (j == count - 1) {
				std::cout << "no" << std::endl;
			}
		}
	}
}

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

	/*
	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++: #include <bits/stdc++.h>

using namespace std;

vector<vector<int>> makelist(int n, int m) {
	vector<vector<int>> Adjlist(n);
	int a, b;

	for(int i = 0; i < m; i++) {
		cin >> a >> b;
		Adjlist[a].push_back(b);
		Adjlist[b].push_back(a);
	}

	return Adjlist;
}

bool dfs_run(int a, int b, vector<vector<int>>& adj, vector<bool>& checker) {
	//cout << a << endl;
	while(!adj[a].empty()) {
		int tmp;
		tmp = adj[a].back();
		adj[a].pop_back();	
		if(checker[tmp] == false) {
			continue;
		}
		checker[tmp] = false;
		if(tmp == b) {
			return true;
		}else{
			return dfs_run(tmp, b, adj, checker);
		}
	}
	return false;
}

bool dfs(int n, int a, int b, vector<vector<int>> adj) {
	vector<bool> checker(n, true);
	
	while(!adj[a].empty()) {
		checker[a] = false;
		if(dfs_run(a, b, adj, checker)) {
			return true;
		}
	}
	return false;
}

int main(void)
{
	int n, m, q;

	cin >> n >> m;
	vector<vector<int>> Adjlist = makelist(n, m);

	cin >> q;
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;

		if(dfs(n, a, b, Adjlist)) {
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}

	return 0;
}

C++: #include <iostream> 
#include <vector> 
#include <stack>
#define MAX 100000
using namespace std;

vector<int> Graph[MAX+1];
int color[MAX+1], col(0);

void AssignColor(int node, int col) 
{
    stack<int> S;
    color[node] = col;
    S.push(node);

    while(! S.empty())
    {
        int current_node = S.top(); S.pop();
        for (int i = 0; i < Graph[current_node].size(); ++i) 
        {
            int next = Graph[current_node][i];
            if (color[next] == -1) 
            {
                color[next] = col;
                S.push(next);
            } 
        }
    }
}


int main() 
{
    int node_num, edge_num, node1, node2;
    int q;
    cin >> node_num >> edge_num;
    for (int i = 0; i < node_num+1; ++i) 
    {
        color[i] = -1;
    }

    for (int i = 0; i < edge_num; ++i) 
    {
        cin >> node1 >> node2;
        Graph[node1].push_back(node2);
        Graph[node2].push_back(node1);
    }

    for (int i = 0; i < node_num; ++i)
    {
        if (color[i] < 0) 
        {
            AssignColor(i, col++);
        }
    }
    
    cin >>  q;
    for (int i = 0; i < q; ++i) 
    {
        cin >> node1 >> node2;
        if (color[node1] == color[node2]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}