Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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;
}