Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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
}