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 <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

class TUser
{
	std::set<TUser*> FAccessible;
public:
	void AddFriend(TUser* Friend);
	bool IsAccesible(TUser* Target)const;
};

void TUser::AddFriend(TUser* Friend)
{
	if(IsAccesible(Friend)) return;
	FAccessible.insert(Friend);
	for(std::set<TUser*>::iterator It = FAccessible.begin(); It != FAccessible.end(); ++It){
		(*It)->AddFriend(Friend);
		Friend->AddFriend(*It);
	}
}

bool TUser::IsAccesible(TUser* Target)const
{
	return (FAccessible.find(Target) != FAccessible.end());
}

class TSns
{
	std::vector<TUser> FUsers;
public:
	TSns(int UserCount);
	
	void AddLink(int UserA, int UserB);
	std::string IsAccesible(int UserA, int UserB);
};

TSns::TSns(int UserCount)
: FUsers(UserCount)
{
}

void TSns::AddLink(int UserA, int UserB)
{
	FUsers[UserA].AddFriend(&FUsers[UserB]);
	FUsers[UserB].AddFriend(&FUsers[UserA]);
}

std::string TSns::IsAccesible(int UserA, int UserB)
{
	if(FUsers[UserA].IsAccesible(&FUsers[UserB])) return "yes";
	return "no";
}

int main()
{
	int UserCount;
	std::cin >> UserCount;
	TSns Sns(UserCount);
	
	int LinkCount;
	std::cin >> LinkCount;
	for(int i = 0; i < LinkCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		Sns.AddLink(UserA, UserB);
	}
	
	int QuestionCount;
	std::cin >> QuestionCount;
	for(int i = 0; i < QuestionCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		std::cout << Sns.IsAccesible(UserA, UserB) << std::endl;
	}
	return 0;
}

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[u] == 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<stack>
using namespace std;

bool DFS(const vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);

    int idx = 0;
    do {
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


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

    vector<vector<int> > G(n);

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (int j = 0; j < G[i].size(); j++){
//         cout << G[i][j] << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef complex<double> P;

int main() {
  int n, m; cin >> n >> m;
  vector<int> adj[n];

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

  int q; cin >> q;
  int sd[n] = {};

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

    queue<int> que;
    que.push(s);
    bool f = false;

    while(!que.empty()) {
      int v = que.front(); que.pop();
      if(v == t) {
        f = true;
        break;
      }

      for (int j = 0; j < n; j++) {
        if(find(adj[v].begin(), adj[v].end(), j) != adj[v].end() && sd[j] == 0) {
          que.push(j);
        }
      }
    }

    cout << (f ? "yes" : "no") << endl;

  }
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX_V 100000

int n, v, q;
vector<int> G[MAX_V];
bool visited[MAX_V];
int group[MAX_V];


void init(){
  for(int i = 0; i < v; i++){
    visited[i] = false;
    group[i] = -1;
  }
}

void dfs(int v, int g){
  group[v] = g;
  visited[v] = true;
  for(int i = 0; i < G[v].size(); i++){
    int to = G[v][i];
    if(!visited[to]){
      dfs(to, g);
    }
  }
}

int main(void){
  cin >> v >> n;
  for(int i = 0; i < n; i++){
    int from, to;
    cin >> from >> to;
    G[from].push_back(to);
    G[to].push_back(from);
  }
  init();
  for(int i = 0; i < v; i++){
    if(!visited[i]) dfs(i, i);
  }
  cin >> q;
  for(int i = 0; i < q; i++){
    int s, t;
    cin >> s >> t;
    if(group[s] != -1 && group[t] != -1 && group[s] == group[t]) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

C++: //Connected Component
#include <iostream>
#include <vector>

using namespace std;

static const int N = 100000;
int n, colors[N] = {};

void dfs(vector<vector<int>>  &G, int u, int c) {
    if(colors[u] != -1) return;
    colors[u] = c;
    for(auto v: G[u]) {
        dfs(G, v, c);
    }
}

int main() {
    int m, s, t, q;
    cin >> n >> m;
    vector<vector<int>> G(n);
    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++) colors[i] = -1;
    for(int i = 0; i < n; i++) {
        dfs(G, i, i);
    }

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