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<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------


int n, m, q;
vector<int> g[100005];
int col[100005];

void dfs(int r, int c)
{
	stack<int> stk;
	stk.push(r);
	col[r] = c;

	while (!stk.empty()) {
		int u = stk.top(); stk.pop();
		for (int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if (col[v] == -1) {
				stk.push(v);
				col[v] = c;
			}
		}

	}
}

void solve()
{
	int c = 0;

	for (int i = 0; i < n; i++) {
		col[i] = -1;
	}

	for (int i = 0; i < n; i++) {
		if (col[i] == -1) {
			dfs(i, ++c);
		}
	}
}


int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		g[s].push_back(t);
		g[t].push_back(s);
	}
	solve();
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (col[s] == col[t]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;
}

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

int n, m;
stack<int> S;
vector<int> V[100000];
int color[100000];

void dfs(int i, int id){
    S.push(i);
    color[i] = id;

    while(!S.empty()){
        int u = S.top();
        S.pop();
        for(int i=0; i<V[u].size(); i++){
            int v = V[u][i];
            if(color[v]==-1){
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void dfs_init(){

    int id = 1; //グラフの識別
    for(int i=0; i<n; i++){
        color[i] = -1;
    }
    for(int i=0; i<n; i++){
        if(color[i]==-1) dfs(i, id++);
    }
}

int main(){

    cin >> n >> m;

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

    dfs_init();

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

    return 0;
}

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

using namespace std;

struct UnionFind{
    UnionFind(int size):_array(size, -1){
    }

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

    void unify(int x, int y){
        int rootx = root(x);
        int rooty = root(y);
        if( rootx == rooty ){
            return;
        }
        if( size(rootx) < size(rooty) ) swap(rootx,rooty);
        _array[rootx] += _array[rooty];
        _array[rooty] = rootx;
    }

    int root(int x){
        if( _array[x] < 0 ){
            return x;
        }
        return _array[x] = root(_array[x]);
    }

    int size(int x){
        return -_array[root(x)];
    }

    vector<int> _array;
};

void DFS(vector<vector<int>>& graph, int v, vector<int>& visited, int root, UnionFind& uf){
    visited[v] = true;
    uf.unify(v, root);
    for(int i = 0 ; i < graph[v].size(); i++){
        if(visited[graph[v][i]] == false){
            DFS(graph, graph[v][i], visited, root, uf);
        }
    }
}

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

    vector<vector<int>> graph(n);
    for(int i = 0; i < m; i++){
        int v1, v2;
        cin >> v1 >> v2;
        graph[v1].push_back(v2);
        graph[v2].push_back(v1);
    }

    vector<int> visited(n, false);
    UnionFind uf(n);

    for(int i = 0; i < n ; i++){
        if( visited[i] == false ){
            DFS(graph, i, visited, i, uf);
        }
    }

    int q;
    cin >> q;

    for(int i = 0; i < q; i++ ){
        int v1, v2;
        cin >> v1 >> v2;
        if( uf.same(v1, v2) ){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

    return 0;

}

C++: //TLEになってしまった

#include<iostream>
#include<vector>
#include<utility>
using namespace std;
#define INF 1000000000

void dfs(vector< vector<int> >& rel,vector<int>& v,int c,int k){
  int l=rel[k].size();
  for(int i=0;i<l;i++){
    if(v[rel[k][i]]==0){
      v[rel[k][i]]=c;
      dfs(rel,v,c,rel[k][i]);
    }
  }
}


int main(){
  int n,m;cin >> n >> m;
  vector<int> v(n,0);//番号を記録(訪問済みでない場合は0)
  vector< vector<int> > rel(n);//隣接リスト

  //隣接リスト、Nodeの行列を作る必要ない
  for(int i=0;i<m;i++){
    int x,y;cin >> x >> y;
    rel[x].push_back(y);rel[y].push_back(x);
  }

  int col=0;
  for(int i=0;i<n;i++){
    if(v[i]==0){
      col++;
      dfs(rel,v,col,i);
    }
  }

  int q;cin >>q;
  for(int i=0;i<q;i++){
    int s,t;cin >> s >> t;
    //非連結だと0だろ
    if(v[s]==0 or v[s]!=v[t]) cout << "no" << endl;
    else cout << "yes" << endl;
  }
  # if 0
  for(int i=0;i<n;i++){cout <<  v[i] << " ";}
  cout << endl;
  # endif
}