Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;

vector<int> ad[100000];
vector<int> v,d;

int main()
{
	int i,j,n,m,a,b,q,l,k;
	queue<int> Q;
	
	cin >> n;
	cin >> m;
	for(i=0;i<=m-1;i++)
	{
		cin >> a;
		cin >> b;
		ad[a].push_back(b);
		ad[b].push_back(a);
	}
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		for(j=0;j<=n-1;j++)
		{
			v.push_back(0);
			d.push_back(-1);
		}
		v[a]=1;
		d[a]=0;
		Q.push(a);
		while(Q.empty()!=1)
		{
			l=Q.front();
			for(k=0;k<=ad[l].size()-1;k++)
			{
				if(v[ad[l][k]]==0)
				{
				    Q.push(ad[l][k]);
				    v[ad[l][k]]=1;
					d[ad[l][k]]=d[l]+1;
				}
			}
			Q.pop();
		}
		if(d[b]>=0) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

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