Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; 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)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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.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 assignColor(){
  int i,u,id=1;
  for(i=0;i<n;i++) color[i] = NIL;
  for(u=0;u<n;u++){
    if(color[u]==NIL) bfs(u,id++);
  }
}

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

  assignColor();

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

C++: #include <cstdio>
#include <algorithm>
#include <unordered_set>
#include <vector>

int main(){
    int n;
    int m;
    std::scanf("%d", &n);
    std::scanf("%d", &m);
    std::vector<std::unordered_set<int> > relations;
    for(int i = 0; i < m; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        int finds = -1;
        int findt = -1;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end()){
                finds = j;
                break;
            }
        }
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(t) != relations[j].end()){
                findt = j;
                break;
            }
        }
        if(finds == -1 && findt == -1){
            std::unordered_set<int> set;
            set.insert(s);
            set.insert(t);
            relations.push_back(set);
        } else if(findt == -1){
            relations[finds].insert(t);
        } else if(finds == -1){
            relations[findt].insert(s);
        } else if(finds != findt){
            int smaller = std::min(finds, findt);
            int larger = std::max(finds, findt);
            for(auto &x: relations[larger]){
                relations[smaller].insert(x);
            }
            relations[larger] = relations.back();
            relations.pop_back();
        }
    }
    int q;
    std::scanf("%d", &q);
    for(int i = 0; i < q; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        bool find = false;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end() && relations[j].find(t) != relations[j].end()){
                find = true;
		break;
            }
        }
        if(find){
            std::printf("yes\n");
        } else {
            std::printf("no\n");
        }
    }

    return 0;
}

C++: #include<stdio.h>
#include<queue>

#define MAX 10000

int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;

bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;

    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }

    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }

    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        bfs(s, d) ? printf("yes\n") : printf("no\n");
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100000;

int par[MAX_N];
int rank[MAX_N];

void initial(int n){		//?????????
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){		//??¨??????????±???????
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

void unite(int x,int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	if(rank[x] < rank[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){
	return find(x) == find(y);
}

int main()
{
	int n,m,q;
	int a,b;
	cin >> n;
	cin >> m;
	initial(n);
	for(int i=0;i<m;i++){
		cin >> a;
		cin >> b;
		unite(a,b);
	}
	cin >> q;
	for(int i=0;i<q;i++){
		cin >> a;
		cin >> b;
		if(same(a.b)){
			cout << "yes\n";
		}else{
			cout << "no\n";
		}
	}
}

C++: #include<iostream>
#include<map>
#include<set>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, stc = 0;
	bool answer_yes = false;
	map<int, set<int> > Adj;
	map<int, int> Stack;
	set<int> Check;
	set<int>::iterator it;

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].insert(t);
		Adj[t].insert(s);
	}

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;
		
		// calculate
		Stack.insert(pair<int, int>(stc, s));
		Check.insert(s);
		while (!Stack.empty()) {
			id = Stack[stc];
			for (it = Adj[id].begin(); it != Adj[id].end(); it++) {
				if (Check.find(*it) == Check.end()) {
					if (*it == t) {
						answer_yes = true;
						Stack.clear();
						Check.clear();
						stc = 0;
						break;
					}
					stc++;
					Stack.insert(pair<int, int>(stc, *it));
					Check.insert(*it);
					break;
				}
			}
			if (it == Adj[id].end()) {
				Stack.erase(stc);
				stc--;
			}
		}
		if (answer_yes) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
		answer_yes = false;

	}
	
	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

vector<vector<int>> V;
map<int, int> pre;
map<int, int> post;
map<int, int> cc;
int t = 1;
int ccid = 1;

int _DFS(int v, vector<bool> &visited) {
  pre[v] = t;
  cc[v] = ccid;
  ++t;
  for (int out_nbr_id : V[v]) {
    if (!visited[out_nbr_id]) {
      visited[out_nbr_id] = true;
      _DFS(out_nbr_id, visited);
    }
  }
  post[v] = t;
  ++t;
}

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

  V = vector<vector<int>>(n);

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

  vector<bool> visited(n, false);

  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      ++ccid;
      visited[i] = true;
      _DFS(i, visited);
    }
  }

  cin >> s; // ignore # of queries

  while (cin >> s >> t) {
    if (cc[s] == cc[t]) {
      cout << "yes\n";
    } else {
      cout << "no\n";
    }
  }
}