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<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);
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		v.clear();
		d.clear();
		for(j=0;j<=n-1;j++)
		{
			v.push_back(0);
			d.push_back(-1);
		}
		v[a]=1;
		d[a]=0;
		Q.push(a);
		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]]=d[l]+1;
				}
			}
			Q.pop();
		}
		if(d[b]>=0) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <stack>
#include <string.h>
#define rep(i, n) for(int i=0;i<n;i++)
#define NOT_FOUND 0
#define ENQUEUED 1
using namespace std;

vector<int> graph[100001];
int memo[100001];

int dfs(int start, int end) {
    if (start == end) {
        return 1;
    }
    memset(memo, NOT_FOUND, sizeof(memo));
    stack<int> stack;
    stack.push(start);
    memo[start] = ENQUEUED;
    int counter = 0;

    while(!stack.empty() && counter != 10) {
        counter += 1;
        int v = stack.top();
        stack.pop();
        vector<int> edges = graph[v];

        for (int i = 0; i < edges.size(); i++) {
            int next = edges[i];
            if (next == end) {
                return 1;
            }
            if (memo[next] == NOT_FOUND) {
                memo[next] = ENQUEUED;
                stack.push(next);
            }
        }
    }
    return 0;
}

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

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

    int q;
    cin >> q;

    for (int i = 0; i < q; i++) {
        int s, d;
        cin >> s >> d;
        if (dfs(s, d)) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
vi G;
inline int root(int x){ return G[x] < 0 ? x : G[x] = root(G[x]); }
inline void uniteSet(int x, int y){
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (G[x] > G[y]) swap(x, y);
    G[y] = x;
}
inline bool findSet(int x, int y){
	if((x != -1 && y != -1) && (G[x] == G[y])) return true;
	return root(x) == root(y);
}
int main() {
	int n, m; cin >> n >> m;
	G.resize(n, -1);
	int s, t;
	rep(i, 0, m) {
		cin >> s >> t;
		uniteSet(s, t);
	}
	int q; cin >> q;
	rep(i, 0, q) {
		cin >> s >> t;
		P(findSet(s, t) ? "yes" : "no");
	}
	return 0;
}

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

#define MAX 100000

#define WHITE 0
#define GRAY 1
#define BLACK 2


using namespace std;

vector<int> G[MAX];
int graphc[MAX];
int color[MAX];
int colorCount;

void def(int o)
{
	int v;

	for (int i = 0; i < G[o].size(); i++) {

		v = G[o][i];
		if (color[v] == WHITE) {

			color[v] = GRAY;
			def(v);
		}
	}
	color[o] = BLACK;
	graphc[o] = colorCount;
}


int main(void)
{
	int n;
	int m;
	int s;
	int t;
	int q;


	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	for (int i = 0; i < n; i++) {

		if (color[i] == WHITE) {
			colorCount++;
			def(i);
			
		}
	}



	cin >> q;

	for (int i = 0; i < q; i++) {

		cin >> s >> t;

		cout << ((graphc[s] == graphc[t]) ? "yes" : "no" )<< endl;
	}

	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<iostream>
using namespace std;
int visited[100000]; int n;
vector<int> G[100000];

//void dfs_visit(int u)
//{
//  if(visited[u] == 1) return;
//  visited[u] = 1;
//  for(int i=0; i<G[u].size(); i++)
//  {
//    int v = G[u][i];
//    if(visited[v] == 0)
//      dfs_visit(v);
//  }
//}
//void dfs(int u, int v)
//{
//  memset(visited, 0, sizeof(n)*n);
//  dfs_visit(u);
//  if(visited[u] == visited[v])
//    printf("yes\n");
//  else printf("no\n");
//}  果然超时了哦。属实是弟弟行为哦~不过好歹能用哈哈哈。
void dfs_visit(int u, int id)
{
  if(visited[u] != 0) return;
  visited[u] = id;
  stack<int> s;//  诶~有意思。用stack来实现DFS一次把下层节点全压栈与一次压下层的一个节点仅仅是子节点的访问顺序颠倒了。
  s.push(u);
  while(!s.empty())
  {
    int v = s.top(); s.pop();
    for(int i=0; i<G[v].size(); i++)
    {
      int x = G[v][i];
      if(visited[x] == 0)
      {
        s.push(x);
        visited[x] = id;
      }
    }
  }
}
void dfs(int u)
{
  memset(visited, 0, sizeof(n)*n);
  int id = 1;// use id to denotes different circle so that all nodes can be visited by one bfs.
  for(int i=0; i<n; i++)
  {
    if(visited[i] == 0)
      dfs_visit(i, id++); // first circle id = 1, then 2 and so on ...
  }
}

int main()
{
  int m, q, u, v;
  scanf("%d%d",&n, &m);
  for(int i=0; i<m; i++)
  {
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  scanf("%d", &q);
  dfs(0);
  for(int i=0; i<q; i++)
  {
    scanf("%d%d", &u, &v);
    if(visited[u] == visited[v])  printf("yes\n");
    else printf("no\n");
  }


  return 0;
}