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 <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector< vector<int> > M;
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

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

  cin >> n >> m;
  REP(i, n) {
    vector<int> vc;
    M.push_back(vc);
  }
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

C++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
using namespace std;

bool isFriend(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    if (sns[x][y] == 1) return true;
    bool res = false;
    arrived[x] = 1;
    for (int i = 0; i < sns.size(); i++) {
        if (sns[x][i] == 1 && !arrived[i]) {
            bool tmp = isFriend(sns, arrived, i, y);
            if (tmp) {
                res = true;
                break;
            }
        }
    }
    return res;
}

int main()
{
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>(n, 0));
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x][y] = 1;
        sns[y][x] = 1;
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        bool res = isFriend(sns, arrived, x, y);
        if (res) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    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;
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); X[B].push_back(A); }
	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (COLOR[i] == 0) {
			Q.push(i); cnt++; COLOR[i] = cnt;
			while (!Q.empty()) {
				int a = Q.front(); Q.pop();
				for (int j = 0; j < X[a].size(); j++) {
					int to = X[a][j];
					if (COLOR[to] == 0) {
						COLOR[to] = cnt;
						Q.push(to);
					}
				}
			}
		}
	}
	cin >> Q1;
	for (int i = 0; i < Q1; i++) {
		cin >> A >> B;
		if (COLOR[A] == COLOR[B]) {
			cout << "1" << endl;
		}
		else {
			cout << "0" << endl;
		}
	}
	return 0;
}

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

using namespace std;

class Disjoint {
public:
    Disjoint(int n) {
        for (int i = 0; i < n; i++) {
            id.push_back(i);
            size.push_back(1);
        }
    }

    bool isConnected(int s, int t) {
        return find(s) == find(t);
    }

    int find(int vertex) {
        while (vertex != id[vertex]) {
            id[vertex] = id[id[vertex]];
            vertex = id[vertex];
        }
        return vertex;
    }

    void connect(int s, int t) {
        int sRoot = find(s);
        int tRoot = find(t);

        if (sRoot == tRoot)
            return;

        if (size[sRoot] < size[tRoot]) {
            size[tRoot] += size[sRoot];
            id[sRoot] = id[tRoot];
        }
        else {
            size[sRoot] += size[tRoot];
            id[tRoot] = id[sRoot];
        }
    }

private:
    vector<int> id;
    vector<int> size;
};

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

    Disjoint disjoint = Disjoint(n);
    int s, t;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        disjoint.connect(s, t);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> s >> t;
        if (disjoint.isConnected(s, t))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include<string.h>
#define MAX 100005
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int N,M;
int Time=0;
vector<int> V[MAX];
int sup[MAX];
void DFS(int i,int u)
{
	stack<int> Q;
	Q.push(i);
	sup[i]=u;
	while(!Q.empty())
	{
		int k=Q.top();
		Q.pop();
		FOR(j,0,V[k].size())
			if(!sup[V[k][j]])
			{
				sup[V[k][j]]=u;
				Q.push(V[k][j]);
			}
	}
}
void solve()
{
	FOR(i,0,N)
		if(!sup[i])
			DFS(i,++Time);
}
int main()
{
	memset(sup,0,sizeof(sup));
	int s,t,q;
	scanf("%d %d",&N,&M);
	FOR(i,0,M)
	{
		scanf("%d %d",&s,&t);
		V[s].push_back(t);
		V[t].push_back(s);
	}
	solve();
	
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d %d",&s,&t);
		if(sup[s]==sup[t]) printf("yes\n");
		else printf("no");
	}
	return 0;
}