Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: #include <iostream>
#include <stdio.h>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
scanf("%d %d", &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];
}
d[j]=h;
while(a!=h ||b!=h){
 
    d[a]=h;
    d[b]=h;
a=d[a];
b=d[b];
}
}

scanf("%d",&q);
for(int y=0;y<q;y++){
    int a,b;
scanf("%d %d", &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<bits/stdc++.h>

using namespace std;

int p[100010];

void init(int n)
{
    for(int i=0;i<n;i++)
    {
        p[i]=i;
    }
}

int fnd(int x)
{
    if(p[x]!=x)
    {
        p[x]=fnd(p[x]);
    }
    return p[x];
}

void unit(int a,int b)
{
    int fa=fnd(a),fb=fnd(b);

    if(fa!=fb)
    {
        p[fa]=fb;
    }
}

bool same(int a,int b)
{
    return fnd(a)==fnd(b);
}

int main()
{

    int n,m,q;

    cin>>n>>m;

    init(n);

    int a,b;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b;
        unit(a,b);
    }

    cin>>q;

    for(int i=0;i<q;i++)
    {
        cin>>a>>b;

        if(same(a,b))
        {
            cout<<"yes"<<endl;
        }else
        {
            cout<<"no"<<endl;
        }
    }





    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
static const int MAX=100000;
using namespace std;

vector<int> follow[MAX];
int color[MAX];  //-1 represent No Visited

void bfs(int i,int c)
{
    queue<int> Q;
    Q.push(i);color[i]=c;
    while(!Q.empty())
    {
        int p=Q.front();Q.pop();
        for(int j=0;j<follow[p].size();j++)
        {
            if(color[follow[p][j]]!=-1) continue;
            //else
            Q.push(follow[p][j]); color[follow[p][j]]=color[p];
        }
    }
}

int main()
{
   int n,m;
   cin>>n>>m;
   for(int i=0;i<n;i++) color[i]=-1;
   for(int i=0;i<m;i++)
   {
       int a,b;
       cin>>a>>b;
       follow[a].push_back(b);
       follow[b].push_back(a);
   }
   int c=0;
   for(int i=0;i<n;i++)
   {
       if(color[i]==-1)
       {
           bfs(i, c);
           c++;
       }
   }
   int order_num;cin>>order_num;
   for(int i=0;i< order_num;i++)
   {
       int a,b;
       cin>>a>>b;
       if(color[a]==color[b]) cout<<"yes"<<endl;
       else cout<<"no"<<endl;
   }
}

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++){
    int v=g[u][i];
    if(color[v]==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 <cstdlib>
#include <vector>

using namespace std;

struct UnionFind
{
    vector<int> data;
    UnionFind() {}
 
    // initialize
    UnionFind(int size) : data(size, -1) {}
 
    void unite_set(int x, int y){
        x = root(x);
        y = root(y);
        bool is_unite = (x != y);
        
        if (is_unite){
            if (data[y] < data[x]){
                swap(x, y);
            }
            data[x] += data[y];
            data[y] = x;
        }
    }
 
    bool is_same(int x, int y){
        return root(x) == root(y);
    }
 
    int root(int x){
        return (data[x] < 0) ? x : data[x] = root(data[x]);
    }
 
    int size(int x){
        return -data[root(x)];
    }
};

int main()
{
    int n, q, m;
    int a, b;
    cin >> n;
    struct UnionFind d(n);
    vector<int> data;
    data.resize(n);
    
    for (int i=0; i<n; ++i){
        data[i] = i;
    }
    // unite
    cin >> m;
    for (int i=0; i<m; ++i){
        cin >> a >> b;
        d.unite_set(a, b);
    }
    // find
    cin >> q;
    for (int i=0; i<q; ++i){
        cin >> a >> b;
        if(d.is_same(a, b)){
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}