Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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

int n, m, s, t, u;
vector<vector <int> > a;
bool f[100000];

bool path(int s1) {
	f[s1] = 1;
	for (int i = 0; i < (int) a[s1].size(); i++) {
		if (a[s1][i] == t) {
			a[s].push_back(t);
			return true;
		}
		else if (!f[a[s1][i]])
			if (path(a[s1][i]))
				return true;
	}
	return false;
}

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

	a.resize(n);

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

	for (int i = 0; i < n; i++)
		sort(a[i].begin(), a[i].end());

	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s >> t;

	for (int i = 0; i < n; i++)
		f[i] = 0;

		if(path(s))
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}

	return 0;
}

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

using namespace std;

int main(){
	int n,m;
	cin >> n >> m;
	
	vector<int> es[n];
	int s,t;
	for(int i=0;i<m;i++){
		cin >> s >> t;
		es[s].push_back(t);
		es[t].push_back(s);
	}
	int q;
	cin >> q;
	for(int i=0;i<q;i++){
		cin >> s >> t;
		
		bool used[n];
		fill(used,used+n,0);
		bool flag = false;
		stack<int> stk;
		stk.push(s);
		used[s] = true;
		while(!stk.empty()){
			int v = stk.top(); stk.pop();
			for(int j=0;j<es[v].size();j++){
				int next = es[v][j];
				if(next == t){
					cout << "yes\n";
					flag = true;
					break;
				}
				if(!used[next]){
					stk.push(next);
					used[next] = true;
				}
			}
			if(flag){
				break;
			}
		}
		if(!flag){
			cout << "no\n";			
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[50000][20];
    int *dist;

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);

	dist = new int [n];

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

	delete [] dist;

    return 0;
}

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

#define MAX_N 100000
#define MAX_M 100000
#define WHITE 1
#define GREY 2
#define BLACK 3

vector<int> G[MAX_N];
int V[MAX_N];
int V_num[MAX_N];

void init(void){
    for(int i = 0; i < MAX_N; i++){
        V[i] = WHITE;
        V_num[i] = i;
    }
}

void labeling(int target){
    queue<int> Q;
    Q.push(target);
    while(!Q.empty()){
        int num = Q.front(); Q.pop();
        for(int i = 0; i < G[num].size(); i++){
            if(V[G[num][i]] == WHITE){
                Q.push(G[num][i]);
                V[G[num][i]] = GREY;
                V_num[G[num][i]] = target;
            }
        }
        V[num] = BLACK;
    }
}

int main(void){
    int n, m;
    scanf("%d %d\n", &n, &m);
    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d\n", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }

    for(int i = 0; i < n; i++){
        if(V[i] != WHITE){
            continue;
        }
        labeling(i);
    }

    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        if(V_num[a] == V_num[b]){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
}

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

#define MAX 100000

vector<int> G[MAX];
int cond[MAX], n;
stack<int> S;

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

void dfs(int x, int id)
{
	S.push(x);
	cond[x] = id;

	int i, v;
	while (!S.empty()) {
		int u = S.top();
		S.pop();
		for (i = 0; i < (int)G[u].size(); i++) {
			v = G[u][i];			
			if (cond[v] == 0) {				
				cond[v] = id;
				S.push(v);
			}
		}
	}
}

void searchFriends()
{
	int id = 1;
	for (int i = 0; i < n; i++)
		if (cond[i] == 0) dfs(i, id++);
}

int main()
{
	int m, q, u, v;
	cin >> n >> m;

	set(m);
	searchFriends();

	cin >> q;
	
	for (int i = 0; i < q; i++) {
		cin >> u >> v;
		if (cond[u] != 0 && cond[v] != 0 && cond[u] == cond[v])
			cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}