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 <stack>

using namespace std;

#define N_MAX 100000

int search_connection(vector<int> v, int t, stack<int> st);
vector<int> G[N_MAX];

int main()
{
    int n, m, i, s, t, q;
    stack<int> st;

    cin >> n >> m;

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

    //??????????????????
    cin >> q;
    for (i = 0; i < q; i++) {
        cin >> s >> t;
        while (st.size() != 0) st.pop();

        st.push(s);
        if ( search_connection(G[s], t, st) )
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

int search_connection(vector<int> v, int t, stack<int> st) {
    for (auto itr = v.begin(); itr != v.end(); itr++) {
        if (*itr == t)
            return 1;
        else {
            st.push(*itr);
            return (0 || search_connection(G[*itr], t, st));
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

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(c);
      }
    }
  }
}

void assignColor(){
  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(void){
  int s, t, m, q;

  cin >> n >> m;

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

  assignColor();

  cin >> q;

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

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define USER_SIZE 100000
#define NIL -1

using namespace std;

int group[USER_SIZE];

int update(int num){
    if(num == group[num]){ return num; }
    else{ return group[num] = update(group[num]); }
}

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

    cin >> n >> m;
    for(int i = 0; i < n; i++){
        group[i] = i;
    }
    for(int i = 0; i < m; i++){
        cin >> s >> t;
        int tmp1 = update(s);
        int tmp2 = update(t);
        group[max(tmp1, tmp2)] = min(tmp1, tmp2);
    }

    cin >> q;
    while(q--){
        cin >> s >> t;
        if(update(s) == update(t)){ cout << "yes" << endl; }
        else{ cout << "no" << endl; }
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

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

bool DFS(const vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);

    int idx = 0;
    do {
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


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

vector<vector<bool> > D(100000, vector<bool>(100000));

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (int j = 0; j < G[i].size(); j++){
//         cout << G[i][j] << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

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

using namespace std;

struct Node {
    int color = 0;
    vector<int> next;
} node[100000];

int main() {
    int n, m, q, t1, t2;

    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> t1 >> t2;
        node[t1].next.push_back(t2);
        node[t2].next.push_back(t1);
    }

    int color = 1;
    queue<int> que;
    while (1) {
        int start = -1;
        for (int i = 0; i < n; i++) {
            if (node[i].color == 0) {
                start = i;
                break;
            }
        }
        if (start == -1) {
            break;
        }
        
        node[start].color = color;
        que.push(start);
        while (!que.empty()) {
            int temp = que.front();
            int size = node[temp].next.size();
            for (int i = 0; i < size; i++) {
                if (node[node[temp].next[i]].color == 0) {
                    node[node[temp].next[i]].color = color;
                    que.push(node[temp].next[i]);
                }
            }
            que.pop();
        }
        color++;
    }

    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> t1 >> t2;
        if (node[t1].color == node[t2].color && node[t1].color > 0 && node[t2].color > 0) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }

    return 0;
}