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<vector>
#include<stack>
using namespace std;
static const int MAX=100000;
static const int NIL=-1;

int n;
vector<int> g[MAX];
int color[MAX];

void dfs(int r,int c){
  stack<int> s;
  s.push(r);
  color[r]=c;
  while(!s.empty()){
    int u=s.top();
    s.pop();
    for(int i=0;i<g[u].size();i++){
      int v=g[u][i];
      if(color[v]==NIL){
	color[v]=c;
	s.push(v);
      }
    }
  }
}

void assignColor(){
  int id=1;
  for(int i=0;i<n;i++) color[i]=NIL;
  for(int u=0;u<n;u++){
    if(color[v]==NIL) dfs(u,id++);
  }
}

int main(){
  int s,t,m,q;

  cin>>n>>m;

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

  assignColor();

  cin>>q;

  for(int i=0;i<q;i++){
     cin>>s>>t;
     if(color[s]==color[t]){
       cout<<"yes"<<endl;
     }else{
       cout<<"no"<<endl;
     }
  }
  return 0;
}

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

using namespace std;

int WHITE = 0;
int GRAY = 1;
int BLACK = 2;

#define MAX 100000

int color[MAX];
vector<int> G[MAX];
int reach[MAX];

void find_route(int s) {
  queue<int> S;

  S.push(s);
  color[s] = GRAY;

  reach[s] = s;

  while(!S.empty()){
    int v = S.front(); S.pop();

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

      if(color[u] == WHITE) {
        color[u] = GRAY;
        reach[u] = s;
        S.push(u);
      }
    }
  }
}

int main() {
  for(int i = 0; i < MAX; i++) color[i] = WHITE;
  for (int i = 0; i < MAX; i++) reach[i] = -1;

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    G[u].push_back(v);
    G[v].push_back(u);
  }

  for(int i = 0; i < MAX; i++) {
    if(color[i] != GRAY) {
      find_route(i);
    }
  }

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

    if(reach[s] != -1 && reach[s] == reach[t]) {
      cout << "yes";
    } else {
      cout << "no";
    }
    cout << endl;
  }

  return 0;
}

C++: #include <vector>
#include <iostream>
#include <stdio.h>

using namespace std;

struct UnionFind{
    vector<int> par;

    UnionFind(int N) : par(N) {
        for (int i = 0; i < N; ++i) {
            par[i] = i;
        }
    }

    int root(int x){
        if(par[x] == x) {
            return x;
        }
        return par[x] = root(par[x]);
    }

    void unite(int x, int y){

        int rx = root(x);
        int ry = root(y);
        if(rx == ry){
            return;
        }
        par[rx] = ry;
    }

    bool same(int x, int y){
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
int main(){
    int n, m;
    cin >> n >> m;
    UnionFind g(n);
    int x, y;
    for (int i = 0; i < m; ++i) {
        cin  >> x >> y;
        g.unite(x, y);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        cin >> x >> y;
        if(g.same(x, y)){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

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

const int M = 10000000;

void connected_components(
  std::vector<std::vector<int> >& adj_list,
  std::vector<int>& component_numbers,
  int root, int current_component){
  assert(component_numbers[root] == M);

  std::queue<int> target_q;
  target_q.push(root);
  component_numbers[root] = current_component;
  // dist_q.push(0;)
  while(!target_q.empty()){
    int vtx = target_q.front(); target_q.pop();
    for(int const&  u : adj_list[vtx]){
      if(component_numbers[u] == M){
        target_q.push(u);
        component_numbers[u] = current_component;
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int n, m, q;
  std::cin >> n;
  std::cin >> m;

  std::vector<std::vector<int> > adj_list(n);

  for (size_t i = 0; i < m; i++) {
    int a, b;
    std::cin >> a;
    std::cin >> b;
    adj_list[a].push_back(b);
    adj_list[b].push_back(a);
  }

  std::vector<int> component(n, M);

  for (size_t i = 0; i < n; i++) {
    if(component[i] == M){
      connected_components(adj_list, component, i, i);
    }
  }

  std::cin >> q;

  for(size_t i =0; i < q; i++){
    int a, b;
    std::cin >> a;
    std::cin >> b;
    bool in_same_component = (component[a] == component[b]);
    std::cout << (in_same_component ? "yes" : "no") << std::endl;
  }


  return 0;
}

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

using namespace std;

static const int N = 100000;
static const int NIL = -1;

int n, m, c;
vector<int> G[N];
int color[N];

void bfs(int u)
{
	queue<int> q;
	q.push(u);
	color[u] = c;

	while (!q.empty())
	{
		u = q.front(); q.pop();
		for (int i = 0; i < G[u].size(); ++i)
		{
			int v = G[u][i];
			if (color[v] == -1)
			{
				q.push(v);
				color[v] = c;
			}
		}
	}
}

void DrawColor()
{
	c = 0;
	for (int i = 0; i < n; ++i) color[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		if (color[i] == -1) bfs(i);
		c++;
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; ++i)
	{
		int u, v; cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	DrawColor();

	int n2, p1, p2;
	cin >> n2;
	for (int i = 0; i < n2; ++i)
	{
		cin >> p1 >> p2;
		if (color[p1] == color[p2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}

	return 0;
}

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

int main()
{
	int n, m;
	cin >> n >> m;
	person *P;
	P = new person[n];
	for (int i = 0; i < n; i++)P[i].id = i;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		cin >> 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;
		cin >> 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;
	}
	delete[] P;
	return 0;
}