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>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define MAX_N 100000

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;
bool used[MAX_N];
int cmp[MAX_N];
int V;

void add_edge(int from, int to) {
	G[from].push_back(to);
	rG[to].push_back(from);
}

void dfs(int v) {
	used[v] = true;
	for (int i = 0; i < G[v].size(); ++i)
		if (!used[G[v][i]])
			dfs(G[v][i]);
	vs.push_back(v);
}

void rdfs(int v, int k) {
	used[v] = true;
	cmp[v] = k;
	for (int i = 0; i < rG[v].size(); ++i)
		if (!used[rG[v][i]])
			rdfs(rG[v][i], k);
}

int scc() {
	fill_n(used, MAX_N, false);
	vs.clear();
	for (int v = 0; v < V; ++v)
		if (!used[v])
			dfs(v);
	fill_n(used, MAX_N, false);
	int k = 0;
	for (int i = vs.size() - 1; i >= 0; --i)
		if (!used[vs[i]])
			rdfs(vs[i], k++);
	return k;
}

int main() {
	int n, m;
	cin >> n >> m;
	V = n;
	for (int i = 0; i < m; ++i) {
		int s, t;
		cin >> s >> t;
		add_edge(s, t);
	}
	int k = scc();
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int s, t;
		cin >> s >> t;
		fill_n(used, MAX_N, false);
		dfs(s);
		if (used[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

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

bool is_conne(int, int);
int n, m;
std::vector<int> connect_dir[100000]{};
int visited_mother[100000] = {};

int main()
{
	int q, s, t;
	std::cin >> n >> m;
	for (int i = 0; i < m; ++i)
	{
		std::cin >> s >> t;
		connect_dir[s].push_back(t);
		connect_dir[t].push_back(s);
	}
	for (int i = 0; i < n; ++i)
		visited_mother[i] = -1;

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

bool is_conne(int s, int t)
{
	if (visited_mother[s] != visited_mother[t]) return false;
	if (visited_mother[s] != -1) return true;

	visited_mother[s] = s;
	std::vector<int> visited_list{ s };
	int pre_size{ 0 };
	bool flag{};

	while (visited_list.size() != pre_size)
	{
		int now_size = visited_list.size();
		for (int i = pre_size; i < now_size; ++i)
			for (auto e : connect_dir[visited_list[i]])
			{
				if (visited_mother[e] != -1) continue;
				visited_mother[e] = s;
				visited_list.push_back(e);
				if (e == t) flag = true;
			}
		pre_size = now_size;
	}
	if (flag)
		return true;
	return false;
}

C++: #include<iostream>
#include<unordered_map>

C++: #include <iostream>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
    d[j]=h;
    d[a]=h;
}
else{
  d[h]=j;
    d[b]=j;
}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<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 <cstdio>
#include <queue>

#define MAX 100000


using namespace std;


int gcol[MAX];

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



	cin >> n >> m;

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

		if (gcol[s] != 0) {
			gcol[t] = gcol[s];
		}
		else if (gcol[t] != 0) {
			gcol[s] = gcol[t];
		}
		else {
			gcol[s] = gcol[t] = type;
			type++;
		}
	}

	/*
	for (int i = 0; i < m; i++) {
		cout << "gcol[" << i << "] = " << gcol[i] << endl;
	}
	*/

	cin >> q;

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

		if (gcol[s] == gcol[t] && gcol[s] != 0) {
			cout << "yes";
		}
		else {
			cout << "no";
		}

		cout << endl;

	}

	return 0;
}

C++: //
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;
int find(int join[],int x){
  if(x==join[x])
    return x;

  return join[x];
}
int main(int argc, char *argv[])
{
  int n,m,join[100000],num[100000],s,t;
  cin >> n >> m;

  for(int i=0; i<n; i++){
    join[i] = i;
    num[i] = 0;
  }

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

    if(s!=t){
      if(num[s] < num[t])
	join[s] = join[t];
      
      else{
	join[t]=join[s];
	if(num[s]==num[t])
	  num[s]++;
      }

    }
  }

  int q;
  cin >> q;

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

    if(find(join,s)==find(join,t))
      cout << "yes\n";

    else
      cout << "no\n";
  }
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
using namespace std;

#define MAX_N (100005)
int n, m;
vector<int> G[MAX_N];
int Group[MAX_N];

int main()
{
    int id = 0;

    scanf("%d %d", &n, &m);
    
    for ( int i = 0; i < m; i++ ) {
	int s, t;
	scanf("%d %d", &s, &t);
	G[s].push_back(t);
	G[t].push_back(s);
    }

    for ( int i = 0; i < n; i++ ) {
	if ( Group[i] == 0 ) {
	    id++;
	    queue<int> q;
	    q.push(i);
	    Group[i] = id;
	    while ( !q.empty() ) {
		int cur = q.front(); q.pop();
		for ( int j = 0; j < G[cur].size(); j++ ) {
		    int fri = G[cur][j];
		    if ( Group[fri] == 0 ) {
			Group[fri] = id;
			q.push(fri);
		    }
		}
	    }
	}
    }

    int q;
    scanf("%d", &q);

    for ( int i = 0; i < q; i++ ) {
	int s, t;
	scanf("%d %d", &s, &t);
	if ( Group[s] == Group[t] ) {
	    printf("yes\n");
	}
	else {
	    printf("no\n");
	}
    }

    return 0;
}