Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX= 100001;
int N,color[MAX],id=1;
vector<int> G[MAX];

void dfs(int n,int c)
{
	stack<int> s;
	s.push(n);
	color[n]=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]!=-1) continue;
			color[v]=c;
			s.push(v);
		}
	}
}

void assignColor()
{
	for(int i=0;i<N;i++) color[i]=-1;
	for(int i=0;i<N;i++)
	{
		if(color[i]==-1) dfs(i,id++);
	}
}
int main()
{
	int m,s,t,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;
const int MAXN = 100005;
const int NIL = -1;

int n;
vector<int>G[MAXN];
int color[MAXN];

void dfs(int s, int c)
{
	stack<int> S;
	S.push(s);
	color[s] = 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 AssighColor()
{
	int id = 1;
	for(int i = 0; i < n; i++)
		color[i] = NIL;
	for(int i = 0; i < n; i++)
	{
		if(color[i] == NIL)
			dfs(i, 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);
	}
	
	AssighColor();
	
	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 <bits/stdc++.h>
using namespace std;

struct UnionFind{
  int n;
  vector<int> r, p;
  UnionFind() {}
  UnionFind(int sz): n(sz), r(sz, 1), p(sz, 0){ iota(p.begin(), p.end(), 0); }
  int find(int x) {
    return (x == p[x] ? x : p[x] = find(p[x]));
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  void unite(int x, int y){
    x = find(x); y = find(y);
    if ( x == y ) return;
    if ( r[x] < r[y] ) swap(x, y);
    r[x] += r[y];
    p[y] = x;
  }
};

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

  UnionFind uf(n);
  for ( int i = 0; i < m; i++ ) {
    int s, t;
    cin >> s >> t;
    uf.unite(s, t);
  }

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

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

int main() {
	// 入力
	int n, m;
	std::cin >> n >> m;
	std::vector<std::vector<int>> vec(n);
	std::vector<int> d(n, -1);
	std::stack<int>  s;

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

	// 連結成分分解
	for (int i = 0; i < n; i++) {
		if (d[i] != -1) continue;
		if (vec[i].size() == 0) continue;
		s.push(i);
		while(!s.empty()){
			int x = s.top();
			s.pop();
			for (int j = 0; j < vec[x].size(); j++) {
				if (d[vec[x][j]] == -1) {
					d[vec[x][j]] = i;
					s.push(vec[x][j]);
				}
			}
		}
	}

	int q;
	std::cin >> q;
	while (q--) {
		int a, b;
		std::cin >> a >> b;
		if (d[a] != -1 && d[b] != -1 && d[a] == d[b]) {
			std::cout << "yes" << std::endl;
		}
		else {
			std::cout << "no" << std::endl;
		}
	}
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

using namespace std;

static const int MAX=1000000;
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;
    rep(i,n)color[i]=nil;
    rep(i,n){
        if(color[i]==nil)dfs(i,id++);
    }
    
    
}

int main(){
    int s,t,m,q;
    cin>>n>>m;
    rep(i,m){
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    assignColor();
    
    cin>>q;
    
    rep(i,q){
        cin>>s>>t;
        if(color[s]==color[t]){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
    }
    

return 0;
}

C++: //#define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		printf("%d,", g->id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}