Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

C++: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>


using namespace std;

int *passed;

bool do_check(int s, int t, vector< vector<int> > a){
  queue<int> q;
  
  bool flag = false;
  if(s==t){flag=true;}
  q.push(s);
  while(q.size()>0 && flag==false){
    s = q.front();
    q.pop();
    for(int i=0;i<a[s].size();i++){
      int tmp = a[s].at(i);
      if(tmp == t){
	flag = true;
      }
      if(passed[tmp] == 0){
	passed[tmp] = 1;
	q.push(tmp);
      }
    }
  }
  return flag;
}

int main(){
  int n, m;
  cin >> n >> m;
  passed = new int[n];
  vector< vector<int> > a;

  for(int i=0;i<n;i++){
    vector<int> tmp;
    a.push_back(tmp);
  }

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

  // Questions
  int q_num;
  cin >> q_num;
  // cout << "q_num: " << q_num << endl;
  for(int i=0;i<q_num;i++){
    for(int j=0;j<n;j++){
      passed[j]=0;
    }
    int s, t;
    cin >> s >> t;

    // cout << s << ": " << do_check(s, t, a) << endl;
    if(do_check(s, t, a)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  
}

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
}