Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<iostream>
#include<vector>
#include<stack>
#define MAX 999999
#define NIL -1
using namespace std;


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 assign()
{
  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);
    }

  assign();

  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;
int main(){
  int n,m,s,t,d[100000];
  vector<int>l[100000];
  cin>>n>>m;
  for(int i=0;i<n;i++)d[i]=-1;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    l[s].push_back(t);
    l[t].push_back(s);
  }
  queue<int> q;
  for(int i=0;i<n;i++){
    if(d[i]==-1){
      q.push(i);
      while(!q.empty()){
	int from=q.front();
	q.pop();
	if(d[from]==-1){
	  d[from]=i;
	  for(int j=0;j<l[from].size();j++)q.push(l[from][j]); 
	}
      }
    }    
  }
  int Q;
  cin>>Q;
  for(int i=0;i<Q;i++){
    cin>>s>>t;
    if(d[s]==d[t])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

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

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, color = 0;
	bool answer_yes = false;

	// enter
	cin >> n >> m;
	vector< vector<int> > Adj(n);
	vector<int> Stack;
	vector<int> Check(n, 0);
	vector<int>::iterator it, itend;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].push_back(t);
		Adj[t].push_back(s);
	}
	/*for (i = 0; i < n; i++) {
		cout << "\n" << i << " : " << flush;
		for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	// calculate
	for (i = 0; i < n; i++) {
		if (Check[i] == 0) {
			color++;
			Stack.push_back(i);
			Check[i] = color;
			while (!Stack.empty()) {
				id = Stack.back();
				//cout << "\nid : " << id << " color : " << color << endl;
				itend = Adj[id].end();
				for (it = Adj[id].begin(); it != itend; it++) {
					if (!Check[*it]) {
						Stack.push_back(*it);
						Check[*it] = color;
						break;
					}
				}
				if (it == itend) {
					Stack.pop_back();
				}
			}
		}
	}

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

C++: #include <stdio.h>
int min(int a,int b){
    if(a>b)return b;
    return a;
}
int max(int a,int b){
    if(a>b)return a;
    return b;
}
int main(){
    int n,m,q,x,y,tree[100000],temp,a;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)tree[i]=i;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        temp=max(tree[x],tree[y]);
        a=min(tree[x],tree[y]);
        for(int i=0;i<n;i++){
            if(tree[i]==temp)tree[i]=a;
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d%d",&x,&y);
        if(tree[x]==tree[y])printf("yes\n");
        else printf("no\n");
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

using Lint=long long;
using Graph=vector<vector<int>>;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N,M; cin>>N>>M;
    Graph 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);
    }

    vector<int> color(N,-1);
    int c=0;
    function<void(int)> dfs=[&](int v)
    {
        color[v]=c;
        for(int u:G[v]) if(color[u]==-1){
            dfs(u);
        }
    };
    for(int i=0;i<N;i++) if(color[i]==-1) dfs(i),c++;

    int Q; cin>>Q;
    while(Q--){
        int s,t; cin>>s>>t;
        cout<<(color[s]==color[t]? "yes":"no")<<endl;
    }
    return 0;
}

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

using namespace std;

static const int N = 100;

int n, e, m[N][N];
int color[N], dist;


void dfs(int i, int c){
	queue<int> q; q.push(i);
	color[i] = c;

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


void search(){
	for (int i = 0; i < n; ++i) color[i] = -1;

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


int main()
{
	cin >> n >> e;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) m[i][j] = 0;
	}

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

	search();

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

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int pa[MAX];
int ra[MAX];
void init(){
  for(int i=0;i<MAX;i++){
    pa[i]=i;
    ra[i]=0;
  }
}
 
int find(int x){
  if(pa[x]==x)return x;
  else return pa[x]=find(pa[x]);
}
 
void unite(int x,int y){
  x=find(x),y=find(y);
  if(x==y)return;
  if(ra[x]<ra[y])swap(x,y);
  pa[y]=x;
  if(ra[x]==ra[y])ra[x]++;
}
 
bool same(int x,int y){
  return (find(x)==find(y));
}
 
int n,m,q,a,b;
int main(){
  init();
  scanf("%d %d",&n,&m);
  for(int i=0;i<m;i++){
    scanf("%d %d",&a,&b);
    unite(a,b);
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++){
    scanf("%d %d",&a,&b);
    cout<<(same(a,b)?"yes":"no")<<endl;
  }
  return 0;
}