Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

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

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include <bits/stdc++.h>
using namespace std;
// #define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define ITR(v,c) for(auto v=begin(c);v!=end(c);v++)
#define FORE(x,c) for(auto &x:c)
#define FOR(v,a,n) for(int v=a;v<(int)(n);v++)
#define REP(v,n) FOR(v,0,n)
#define RREP(v,n) for(int v=((int)(n)-1);v>=0;v--)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define SZ(c) ((int)c.size())
const int DX[9]={0,1,0,-1,1,1,-1,-1,0}, DY[9]={-1,0,1,0,-1,1,1,-1,0};
const int INF=1e9; const long long INFLL=1e18;
template<class T> ostream& operator << (ostream &os, const vector<T> &v) {
    ITR(i,v) os<<*i<<(i==end(v)-1?"":" "); return os; }
template<class T> istream& operator >> (istream &is, vector<T> &v) {
    ITR(i,v) is>>*i; return is; }
//------------------------------------------------------------------------------

//-------- Union Find
struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
    bool unionSet(int x, int y) {
	    x = root(x); y = root(y);
        if(x != y) {
            if(data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
	}
	bool findSet(int x, int y) {
	    return root(x) == root(y);
    }
	int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

signed main() {
    int n,m;
    cin>>n>>m;
    UnionFind uf(n);
    REP(i,m) {
        int a,b;
        cin>>a>>b;
        uf.unionSet(a,b);
    }

    int q;
    cin>>q;
    REP(i,q) {
        int a,b;
        cin>>a>>b;
        if(uf.root(a)==uf.root(b)) {
            cout<<"yes"<<endl;
        }
        else {
            cout<<"no"<<endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
int i, j, v, num, color[100000];
vector<int> list[100000];

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(i=0; i<list[u].size(); i++){
     v = list[u][i];
     if(color[v] == -1){
       color[v] = c;
       S.push(v);
     }
   }
 }
}
 
void assigncolor(){
 int id = 1;
 for(i=0; i<num; i++){
   color[i] = -1;
 }
 for(i=0; i<num; i++){
   if(color[i] == -1)
     dfs(i, id++);
 }
}
 
int main(){
 int a, b, trial, start, goal, kaisuu;
 scanf("%d %d", &num, &kaisuu);
 
 for(i=0; i<kaisuu; i++){
   scanf("%d %d", &a, &b);
   list[a].push_back(b);
   list[b].push_back(a);
 }

 assigncolor();

 scanf("%d", &trial);

 for(i=1; i<=trial; i++){
   scanf("%d %d", &start, &goal);
   if(color[start] == color[goal]){  
     printf("yes\n");
   } else{
     printf("no\n");
   }
 }
 
 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 <bits/stdc++.h>
using namespace std;

int n, m;

bool frend(vector<vector<int>> &M, vector<int> &memo, int i, int t) {
  if (memo[i] == 1) return false;
  for (int z = 0; z < M[i].size(); z++) {
    if (M[i][z] == t) return true;
    memo[i] = 1;
    if (frend(M, memo, M[i][z], t)) { return true; }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n);

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  for (int i = 0; i < qn; i++) {
    vector<int> memo(n);
    bool res = frend(M, memo, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

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

int n, m;

bool frend(vector<vector<int>> &M, int i, int t) {
  for (int z = i + 1; z < n; z++) {
    if (M[i][z] == 0) continue;
    if (M[i][z] == 1 && z == t) return true;
    if (frend(M, z, t)) {
      return true;
    } else {
      return false;
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n, vector<int>(n));

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id][f] = 1;
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
    if (q[i][0] > q[i][1]) { swap(q[i][0], q[i][1]); }
  }

  for (int i = 0; i < qn; i++) {
    bool res = frend(M, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

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

const int N = 100000;
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 x = S.top(); S.pop();
        for(int i = 0; i < G[x].size(); i++){
            int v = G[x][i];
            if(color[v] == NIL){
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void solve(){
    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 q, s, t;
    cin >> n >> q;

    while(q-- > 0){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    solve();

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

    return 0;
}