Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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

vector<int> ad[100000];
vector<int> v,d;

int main()
{
	int i,j,n,m,a,b,q,l,k;
	queue<int> Q;
	
	cin >> n;
	cin >> m;
	for(i=0;i<=m-1;i++)
	{
		cin >> a;
		cin >> b;
		ad[a].push_back(b);
		ad[b].push_back(a);
	}
	for(i=0;i<=n-1;i++) 
	{
		ad[i].push_back(-1);
		v.push_back(0);
		d.push_back(-1);
	}
	j=0;
	for(i=0;i<=n-1;i++)
	{
		if(v[i]==0)
		{
			Q.push(i);
			v[i]=1;
			d[i]=j;
			while(Q.empty()!=1)
			{
				l=Q.front();
				for(k=0;ad[l][k]!=-1;k++)
				{
					if(v[ad[l][k]]==0)
					{
						Q.push(ad[l][k]);
						v[ad[l][k]]=1;
						d[ad[l][k]]=j;
					}
				}
				Q.pop();
			}
			j++;
		}
	}
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		if(d[a]==d[b]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: /*
 * ALDS1_11_D.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

#include<iostream>
#include<vector>

using namespace std;

bool visited[100000];

void dfsRec(vector<int>* a, int v) {
	if(visited[v]) return;
	visited[v] = 1;
	for(int &x:a[v]) {
		dfsRec(a, x);
	}
}

int main() {
	int n, m;
	vector<int> adj[100000];

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

	int q;
	cin >> q;
	while(q--) {
		for(int i=0; i<n; i++) {
			visited[i] = 0;
		}
		cin >> u >> v;
		dfsRec(adj, u);
		(visited[v]) ? printf("yes\n") : printf("no\n");
	}
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

vector<int> SNS[100000];
int Group[100000] = { 0 };

int main()
{
	int n, m, q, s, t;
	int man1, man2;
	int grNo = 1;
	int current, next;
	queue<int> Q;
	vector<int>::iterator it;
	
	cin >> n >> m;
	rep(i, m) {
		cin >> man1 >> man2;
		SNS[man1].push_back(man2);
		SNS[man2].push_back(man1);
	}

	rep(i, n) {
		if (Group[i] == 0) {
			Q.push(i);
			Group[i] = grNo;

			while (!Q.empty()) {
				current = Q.front();

				for (it = SNS[current].begin(); it != SNS[current].end(); it++) {
					next = *it;
					if (Group[next] == 0) {
						Q.push(next);
						Group[next] = grNo;
					}
				}

				Q.pop();
			}

			grNo++;
		}
	}

	cin >> q;
	rep(i, q) {
		cin >> s >> t;
		if (Group[s] == Group[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include <stdio.h>
int ancestor[10];
int find(int x){
    if(ancestor[x]==x)return x;
    return ancestor[x]=find(ancestor[x]);
}
int min(int a,int b){
    if(a<b)return a;
    return b;
}
int max(int a,int b){
    if(a>b)return a;
    return b;
}
int main(){
    int n,q,m,temp,x,y;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)ancestor[i]=i;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        temp=find(x);
        q=find(y);
        ancestor[max(temp,q)]=min(temp,q);
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d%d",&x,&y);
        if(find(x)!=find(y))printf("no\n");
        else printf("yes\n");
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct Vertex {
    vector<ll> node;
};

Vertex V[100005];
bool U[100005];
ll Parent[100005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    REP(_, M) {
        ll a, b;
        cin >> a >> b;
        V[a].node.push_back(b);
        V[b].node.push_back(a);
    }

    REP(i, N) {
        Parent[i] = i;
    }
    memset(U, 0, sizeof(U));

    REP(i, N) {
        if (U[i])
            continue;
        queue<ll> q;
        q.push(i);
        U[i] = true;
        while (!q.empty()) {
            ll t = q.front();
            q.pop();
            for (auto &n:V[t].node) {
                if (U[n])
                    continue;
                Parent[n] = i;
                q.push(n);
                U[n] = true;
            }
        }
    }

    ll Q;
    cin >> Q;
    REP(_, Q) {
        ll s, t;
        cin >> s >> t;
        if (Parent[s] == Parent[t])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;
#define MAX 100005
#define NIL -1

vector<int> V[MAX];
int color[MAX];


void search(int v, int c) {
	if (color[v] != NIL)return;
	color[v] = c;
	for (int i = 0; i < V[v].size(); i++) {
		search(V[v][i], c);
	}
}

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

	int id = 1;
	for (int i = 0; i < n; i++)color[i] = NIL;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)search(u, id++);
	}

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

	return 0;
}

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

void assignColor() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int i = 0; i < n; i++) {
    if (color[i] == NIL) {
      dfs(i, id++);
    }
  }
}

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

  assignColor();

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    if (color[u] == color[v]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}