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 <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define MAX_N 100000

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;
bool used[MAX_N];
int cmp[MAX_N];
int V;

void printv(string name, int v[], int len) {
	cout << name << " :";
	for (int i = 0; i < len; i++)
		cout << " " << v[i];
	cout << endl;
}

void printv(string name, vector<int> v) {
	cout << name << " :";
	for (int i = 0; i < V; i++)
		cout << " " << v[i];
	cout << endl;
}

void add_edge(int from, int to) {
	G[from].push_back(to);
	rG[to].push_back(from);
}

void dfs(int v) {
	used[v] = true;
	for (int i = 0; i < G[v].size(); ++i)
		if (!used[G[v][i]])
			dfs(G[v][i]);
	vs.push_back(v);
}

void rdfs(int v, int k) {
	used[v] = true;
	cmp[v] = k;
	for (int i = 0; i < rG[v].size(); ++i)
		if (!used[rG[v][i]])
			rdfs(rG[v][i], k);
}

int scc() {
	fill_n(used, MAX_N, false);
	vs.clear();
	for (int v = 0; v < V; ++v)
		if (!used[v])
			dfs(v);
	fill_n(used, MAX_N, false);
	int k = 0;
	for (int i = vs.size() - 1; i >= 0; --i)
		if (!used[vs[i]])
			rdfs(vs[i], k++);
	return k;
}

int scc(int v) {
	fill_n(used, MAX_N, false);
	vs.clear();
//	for (int v = 0; v < V; ++v)
		if (!used[v])
			dfs(v);
	fill_n(used, MAX_N, false);
	int k = 0;
	for (int i = vs.size() - 1; i >= 0; --i)
		if (!used[vs[i]])
			rdfs(vs[i], k++);
	return k;
}

int main() {
	int n, m;
	cin >> n >> m;
	V = n;
	for (int i = 0; i < m; ++i) {
		int s, t;
		cin >> s >> t;
		add_edge(s, t);
	}
	int k = scc();
//	cout << "k = " << k << endl;
//	printv("cmp", cmp, V);
//	printv("vs", vs);
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int s, t;
		cin >> s >> t;
		scc(s);
		if (used[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

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

#define MAX 100000

using namespace std;

int visited[MAX];
//adjacent list
vector<int> G[MAX];

//can go from s to t or not
int color[MAX];

void dfs(int s,int c){
  color[s]=c;
  visited[s]=1;
  for(int i=0;i<G[s].size();i++){
    if(visited[G[s][i]]==0){
      dfs(G[s][i],c);
    }
  }
}

int main(){
  int n,m; cin >> n >> m;
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<m;i++){
    int x,y; cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  int q; cin >> q;
  for(int i=0;i<q;i++){
    for(int j=0;j<n;j++) visited[j]=0;
    int s,t; cin >> s >> t;
    dfs(s,i);
    if(color[s]==color[t]) cout << "yes" << endl;
    else cout << "no" << endl;
  }
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <stack>
#include <string.h>
#define rep(i, n) for(int i=0;i<n;i++)
#define NOT_FOUND 0
#define ENQUEUED 1
using namespace std;

vector<int> graph[100001];
int memo[100001];

int dfs(int start, int end) {
    if (start == end) {
        return 1;
    }
    memset(memo, NOT_FOUND, sizeof(memo));
    stack<int> stack;
    stack.push(start);
    memo[start] = ENQUEUED;

    while(!stack.empty()) {
        int v = stack.top();
        stack.pop();
        vector<int> edges = graph[v];

        rep(i, edges.size()) {
            int next = edges[i];
            if (next == end) {
                return 1;
            }
            if (memo[next] == NOT_FOUND) {
                memo[next] = ENQUEUED;
                stack.push(next);
            }
        }
    }
    return 0;
}

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

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

    int q;
    cin >> q;

    for (int i = 0; i < q; i++) {
        int s, d;
        cin >> s >> d;
        if (dfs(s, d)) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

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

int n;
vector<int> g[N];
int color[N];

void dfs(int r,int c){
    stack<int> s;
    s.push(r);
    color[r] = c;
    while(!s.empty()){
        int u = s.top();
        s.pop();
        for(int i=0;i<g[u].size();i++){
            int v = g[u][i];
            if(color[v] == NIL){
                color[v] = c;
                s.push(v);
            }
        }
    }
}

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

int main(){
    int m,s,t,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_color();

    cin >> q;
    for(int i=0;i<q;i++){
        cin >> s >> t;
        if(color[s] == color[t])
            cout << "yes" << endl;
        else
            cout << "no"  << endl;
    }

}