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<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vi=vector<int>;
using vb=vector<bool>;
using pii=pair<int,int>;

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}

//pow(llpow,modpow)
template<class T> ll llpow(ll x,T n){ll ans=1;if(x==0)ans=0;while(n){if(n&1)ans*=x;x*=x;n>>=1;}return ans;}
long long modpow(long long a, long long n, long long mod) {long long res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
//最大公約数
template<class T> inline T gcd(T x,T y){if(y==0)return x; else {return gcd(y,x%y);}}
//最小公倍数
template<class T> inline T lcm(T x,T y){return x/gcd(x,y)*y;}
//逆元
long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

long long INF = 1LL<<60;
int main( ){
    int n,m;
    cin>>n>>m;
    vi conne[n];
    rep(i,0,m){
        int s,t;
        cin>>s>>t;
        conne[s].push_back(t);
        conne[t].push_back(s);
    }
    int white=0;
    vi color(n,white);
    int c=1;
    rep(i,0,n){
        c++;
        if(color[i]==white){
        stack<int> st;
        st.push(i);
        while(!st.empty()){
            int n=st.top(); st.pop();
            color[n]=c;
            for(int itr:conne[n]){
                if(color[itr]!=c){
                    st.push(itr);
                }
            }
        }
        }
    }
    int q; cin>>q;
    rep(i,0,q){
        int from,to;
        cin>>from>>to;
        if(color[from]==color[to])cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

C++: /*
** Connected Components
*/

# include <cstdio>
# include <cstring>
# include <vector>
# include <queue>
using namespace std;

const int MAX = 100000;

int n, m, q, c[MAX];
vector<int> g[MAX];

void init() {
    scanf("%d%d", &n, &m);
    int u, v;
    while (m--) {
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
}

bool find_con(int u, int v) {
    queue<int> que;
    que.push(u);
    memset(c, 0, sizeof(c));
    c[u] = 1;
    bool rec = false;

    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = 0; i < g[t].size(); i++) {
            if (g[t][i] == v) {
                return true;
            }
            if (c[g[t][i]] == 0) {
                c[g[t][i]] = 1;
                que.push(g[t][i]);
            }
        }
    }

    return rec;
}

void solve() {
    init();
    scanf("%d", &q);
    int u, v;
    while (q--) {
        scanf("%d%d", &u, &v);
        if (find_con(u, v)) {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
}

int main() {
    // freopen("infile", "r", stdin);
    // freopen("outfile", "w", stdout);
    solve();
    return 0;
}

C++: #include<stdio.h>
#include<queue>

#define MAX 10000

int G[MAX+1][MAX+1];
int n, m;

bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;

    std::queue<int> Q;
    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
            //printf("s:%d, cur:%d, i:%d\n", src, cur, i);
        }
    }

    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }

    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        bfs(s, d) ? printf("yes\n") : printf("no\n");
    }

    return 0;
}

C++: #include<stdio.h>
#include<queue>

using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
	queue<int> q;

	while (table[child] != -1) {
		q.push(child);
		child = table[child];
	}
	while (!q.empty()) {
		table[q.front()] = child;
		q.pop();
	}
	return child;
}

int main() {
	int n, m, q;
	int table[MAX];

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)table[i] = -1;
	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &n, &m);
		
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
	}

	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		
		scanf("%d %d", &n, &m);

		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}
	return 0;
}

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

using namespace std;

int main() {
  vector<int> R[100000];
  queue<int> Q;
  int n,m,s,t,q;
  int visit[100000];

  cin >> n >> m;
  for(int i=0;i<m;i++){
    cin >> s >> t;
    R[s].push_back(t);
    R[t].push_back(s);
  }
  cin >> q;
  for(int i=0;i<q;i++){
    cin >> s >> t;
    for(int j=0;j<n;j++) visit[j] = 0;
    Q.push(s);
    int f=0;
    while(Q.empty() == false && f==0){
      int u = Q.front();
      Q.pop();
      visit[u] = 1;
      if(u==t){
	f = 1;
	break;
      }
      for(int j=0;j<R[u].size();j++){
	int v = R[u][j];
	if(visit[v]==0) Q.push(v);
      }
    }
    if(f==1) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}