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++: /*
 * d.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

vi nbrs[MAX_N];
int ids[MAX_N];

/* subroutines */

/* main */

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

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

  int gn = 0;

  for (int st = 0; st < n; st++)
    if (! ids[st]) {
      ids[st] = ++gn;
      qi q;
      q.push(st);

      while (! q.empty()) {
	int u = q.front(); q.pop();
	vi &nbru = nbrs[u];

	for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
	  int &v = *vit;
	  if (! ids[v]) {
	    ids[v] = gn;
	    q.push(v);
	  }
	}
      }
    }

  int qn;
  cin >> qn;
  while (qn--) {
    int s, t;
    cin >> s >> t;
    cout << (ids[s] == ids[t] ? "yes" : "no") << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
vector<int> neighbors[100005];
//why choose vector, not matrix
int root[100005];

void dfs(int i, int start){
	root[i] = start;
	//visut all i's neighbors
	//c++ 11
	for(int j : neighbors[i]){
		if(root[j]==-1){
			dfs(j,start);
		}
	}
}

int main(){
	int n,m,x,y;
	memset(root, -1, sizeof(root));
	scanf("%d %d",&n,&m);
	for(int i = 0; i<m; i++){
		scanf("%d %d",&x,&y)
		neighbors[x].push_back(y);
		neighbors[y].push_back(x);
	}
	for(int i = 0; i<n; i++){
		if(root[x]= -1){
			dfs(i,i);
		}
	}
	scanf("%d", &q);
	for(int i = 0; i<q; i++){
		scanf("%d %d",&x, &y);
		if(root[x] = root[y]){
			printf("Yes\n");
		}else printf("no\n");
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int a[100000],gp[1000000],g=0;
int inf=100000,n,m;
vector< vector<int> > v(100000);
void dfs(int s){
    gp[s]=g;
    a[s]=inf;
    for(int i=0;i<(int)v[s].size();i++){
        if(a[v[s][i]]!=inf){
            dfs(v[s][i]);
        }
    }
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
      int tmp,u;
      cin>>tmp>>u;
      v[tmp].push_back(u);
      v[u].push_back(tmp);
  }
  dfs(0);
  for(int i=0;i<n;i++){
      g++;
      if(!a[i])dfs(i);
  }
  int qu;
  cin>>qu;
  int s[qu],t[qu];
  for(int i=0;i<qu;i++)cin>>s[i]>>t[i];
  for(int i=0;i<qu;i++){
      if(gp[s[i]]==gp[t[i]])
          cout<<"yes\n";
      else
          cout<<"no\n";
  }
}

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)
{
    for (int i = 0; i < sns[x].size(); i++) {
        if (sns[x][i] == y) return true;
    }

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

int main()
{
//    freopen("ALDS1_11_D_in20.txt", "r", stdin);
//    freopen("out20.txt", "w", stdout);
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>());
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x].push_back(y);
        sns[y].push_back(x);
    }

    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 <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <random>
#include <cstdlib>
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

static const int N = 100005;
vector<int> V[N], visit(N, 0), color(N, 0);
queue<int> Q;

void paint(int c) {
	while (Q.size() > 0) {
		int u = Q.front();
		Q.pop();
		color[u] = c;
		Loop(i, V[u].size()) {
			if (color[V[u][i]] == 0) {
				Q.push(V[u][i]);
			}
		}
	}
}

int main() {
	int n, m;
	cin >> n >> m;
	Loop(i, m) {
		int s, t;
		cin >> s >> t;
		V[s].push_back(t);
		V[t].push_back(s);
	}
	//color painting
	int c = 0;
	Loop(i, n) {
		if (color[i] == 0) {
			c++;
			Q.push(i);
			paint(c);
		}
	}
	//end
	int q;
	cin >> q;
	Loop(i, q) {
		int s, t;
		cin >> s >> t;
		if (color[s] == color[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

int n;
int **connection;

bool isFriend(int s, int t){
    int i;
    int f[n];
    for(i=0;i<n;i++)f[i]=0;
    f[s]=1;
    int u;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        u=q.front();q.pop();
        for(i=0;i<n;i++){
            if(connection[u][i]&&f[i]==0){
                connection[u][i]=1;
                connection[i][u]=1;
                if(i==t)return true;
                f[i]=1;
                q.push(i);
            }
        }
    }
    return false;
}

int main(){
    int i, j, m, s, t, q;
    scanf("%d %d",&n,&m);
    connection = (int **)malloc(sizeof(int *)*n);
    for(i=0;i<n;i++){
        connection[i] = (int *)malloc(sizeof(int)*n);
        for(j=0;j<n;j++){
            connection[i][j]=0;
        }
    }
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        connection[s][t]=1;
        connection[t][s]=1;
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(s,t))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}