Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){

  bool used[m];
  for(int i=0;i<m;i++) used[i]=false; //初期化

  queue<int> Q;
  Q.push(s); used[s]=true;

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(temp==t){return true;}
      if(!used[temp]){ //未訪問なら
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){

  cin>>n>>m;

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

  int q; cin>>q;

  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s,t)){
      cout<<"yes\n";
    }else{
      cout<<"no\n";
    }
  }

}

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

int n;
vector<int> g[100001];
char c[100001];
int visit[100001];
int finish[100001];
int group[100001];
int m;
int main() {

    cin >> n >> m;
    for (int i1 = 1; i1 <=m; ++i1) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

   stack<int> s;
    for (int l = 0; l < n; ++l) {
        c[l] = 'w';

    }
    int time = 0;
    int start = -1;
    int v;
    int nowg = 0;
    while(true) {
        nowg++;
        start++;
        if(start == n) break;
        if(c[start] == 'w') {
            time++;
            s.push(start);
            c[start] = 'g';
            group[start] = nowg;
            visit[start] = time;
            while (s.size() != 0) {
                time++;
                v = s.top();
                int next = -1;
                for (int i = 0; i < g[v].size() ; ++i) {
                    if(c[g[v].at(i)] == 'w') {
                        next = g[v].at(i);
                        break;
                    }
                }
                if(next == -1) {
                    c[v] = 'b';
                    finish[v] = time;
                    s.pop();
                } else {
                    c[next] = 'g';
                    group[next] = nowg;
                    visit[next] = time;
                    s.push(next);
                }
            }
        }
    }

    int q;
    cin >> q;
    for (int m = 0; m < q; ++m) {
        int a, b;
        cin >> a >> b;
        if(group[a] == group[b]) cout << "yes";
        else cout << "no";
        cout << endl; 


    }



    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

class Graph
{
public:
    Graph() {}
    Graph(int n)
    : con(vs32(n, -1))
    {}
    ~Graph() {}

    bool path(int u, int v)
    {
        bool ret;

        int repu = getRep(u);
        int repv = getRep(v);
        if (repu != repv) ret = false;
        else /*if (repu == repv)*/ ret = true;

        return ret;
    }

    void setPath(int u, int v)
    {
        int repu = getRep(u);
        int repv = getRep(v);


        if (repu == repv) return;
        if (con[repu] > con[repv])
        {
            swap(repu, repv);
        }
        con[repu] += con[repv];
        con[repv] = repu;
    }

    int getRep(int u)
    {
        int ret;
        if (con[u] >= 0)
        {
            con[u] = getRep(con[u]);
            ret = con[u];
        }
        else
        {
            ret = u;
        }
        return ret;
    }

    vs32 con;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    Graph g(n);
    int m;
    cin >> m;
    int u, v;
    rep (i, m)
    {
        cin >> u >> v;
        g.setPath(u, v);
    }

    int q;
    cin >> q;
    rep (i, q)
    {
        cin >> u >> v;
        if (g.path(u, v))
        {
            cout << "yes\n";
        }
        else
        {
            cout << "no\n";
        }
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q; int COLOR[MAX_N], N, M, Q1, A, B;
pair<int, int> R[MAX_N]; vector<pair<int, int>>S[MAX_N]; int T[MAX_N];
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); }
	for (int i = 0; i < Q1; i++) {
		cin >> R[i].first >> R[i].second; S[R[i].first].push_back(make_pair(R[i].second, i));
		S[R[i].second].push_back(make_pair(R[i].first, i));
	}
	for (int i = 0; i < N; i++) {
		Q.push(i); for (int j = 0; j < N; j++) { COLOR[j] = 0; }
		while (!Q.empty()) {
			int B = Q.front(); Q.pop();
			for (int j = 0; j < X[B].size(); j++) {
				if (COLOR[X[B][j]] == 0) { COLOR[X[B][j]] = 1; Q.push(X[B][j]); }
			}
		}
		for (int j = 0; j < S[i].size(); j++) {
			int a1 = S[i][j].first, a2 = S[i][j].second;
			if (COLOR[a1] == 1) { T[a2]++; }
		}
	}
	for (int i = 0; i < Q1; i++) {
		if (T[i] == 2) { cout << '1' << endl; }
		else { cout << '0' << endl; }
	}
	return 0;
}