Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

int n;
int parent[100000];
int r[100000]; //rank

void init(int n){
  for(int i = 0;i < n;i++){
    parent[i] = i;
    r[i] = 0;
  }
}

int find(int x){
  if(parent[x] == x) return x;
  else return parent[x] = find(parent[x]);
}

void unite(int x, int y){
  x = find(x);
  y = find(y);
  
  if(x == y) return ;
  
  if(r[x] < r[y]) parent[x] = y;
  else{
    parent[y] = x;
    if(r[x] == r[y]) r[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

int main(){
  
  int m, q, s, t;
  
  cin >> n >> m;
  init(n);
  for(int i = 0;i < m;i++){
    cin >> s >> t;
    unite(s, t);
  }
  
  cin >> q;
  for(int i = 0;i < q;i++){
    cin >> s >> t;
    if(same(s, t)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  
  return 0;
}

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

const int nmax = 100005;
int n;
const int NIL = -1;

std::vector<int> adjlist[nmax];
std::array<int, nmax> group;

std::stack<int> stk;
std::array<int, nmax> state;

void init()
{
  group.fill(NIL);
  state.fill(0);
}

int next(int v)
{
  for (int i=0; i<adjlist[v].size(); ++i)
    {
      auto v_next = adjlist[v][i];
      if (state[v_next] == 0)
        return v_next;
    }
  return NIL;
}

void dfs_1v(int u, int group_id)
{
  stk.push(u);
  state[u] = 1;
  group[u] = group_id;

  while (!stk.empty())
    {
      auto v = stk.top();
      auto v_next = next(v);

      if (v_next != NIL)
        {
          stk.push(v_next);
          state[v_next] = 1;
          group[v_next] = group_id;
        }
      else
        {
          state[v] = 2;
          stk.pop();
        }
    }
}

void grouping()
{
  init();

  auto group_id = 0;
  for (int i=0; i<n; ++i)
    {
      if (state[i] == 0)
        {
          dfs_1v(i, group_id);
          group_id++;
        }
    }
}

int main ()
{
  int m;
  std::cin >> n >> m;

  for (int i=0; i<m; ++i)
    {
      int s, t;
      std::cin >> s >> t;

      adjlist[s].push_back(t);
      adjlist[t].push_back(s);
    }

  grouping();

  int q;
  std::cin >> q;
  for (int i=0; i<q; ++i)
    {
      int s, t;
      std::cin >> s >> t;
      if (group[s] == group[t] && group[s] != NIL)
        std::cout << "yes\n";
      else
        std::cout << "no\n";
    }
}

C++: #include<iostream>
#include<list>
#include<iterator>
using namespace std;

class person
{
public:
	int id;
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		for(list<person*>::iterator temp=lis.begin();temp!=lis.end();++temp)
		{
			if ((*temp) == Per)return true;
			else if ((*temp)->Isdone == true)continue;
			else
			{
				if ((*temp)->serch(Per))return true;
			}
		}
		return false;
	}
};

person P[100001];

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)P[i].id = i;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		scanf_s("%d%d",&p1, &p2);
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}			 
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		scanf_s("%d%d", &p1, &p2);
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
int M[N][N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, n) {
      if (M[s][i] == 1 && !V[i]) {
        q.push(i);
        V[i] = true;
      }
    }
  }

  return V[v];
}

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  REP(i, n) {
    REP(j, n) {
      M[i][j] = 0;
    }
  }

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u][v] = M[v][u] = 1;
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

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

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	//int d[100000];

	std::stack<int> s;
	for (size_t i = 0; i < n.size(); i++) {
		s.push(i);
		int d[100000] = {};
		/*
		for (int k = 0; k < n.size(); k++) {
			d[k] = 0;
		}
		*/
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
		if (from == to) return true;
	}
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;
	
	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);
	makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
		std::cout << i << " " << n[i].parent << std::endl;
	}
	/*
	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	//*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}