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 <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int MAX_N = 100100;
template<typename Type> struct UnionFind
{
    vector<Type> p, r;
    UnionFind(int sz)
    {
        p.resize(sz);
        r.resize(sz, -1);
        for(int i = 0; i < sz; i++) p[i] = i;
    }
    Type Find(Type x)
    {
        if (x == p[x]) return x;
        return p[x] = Find(p[x]);
    }
    bool isSame(Type a, Type b) { return Find(a) == Find(b); }
    void unite(Type a, Type b)
    {
        a = Find(a), b = Find(b);
        if(a == b) return;
        if(r[a] < r[b]) p[a] = b;
        else { p[b] = a; if(r[a] == r[b]) r[a]++; }
    }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m; cin >> n >> m;
    UnionFind<int> UF(n + 1);
    for(int i = 0; i < m; i++)
    {
        int s, t; cin >> s >> t;
        if(UF.isSame(s, t)) continue;
        UF.unite(s, t);
    }
    int q; cin >> q;
    for(int i = 0; i < q; i++)
    {
        int s, t; cin >> s >> t;
        cout << (UF.isSame(s, t) ? "yes" : "no") << "\n";
    }
    return 0;
}

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

int main(){
	int n,m,q,i,j;
	int s,t;
	vector<int> L(100005,-1);
	cin >> n >> m;
	
	for (j=0;j<n;j++){
		L[j]=j;
	}
	
	for (i=0;i<m;i++){
		cin >> s >> t ;
		if (L[s]!=L[t]){
			for (j=0;j<n;j++){
				if (L[j]==L[t]){
					L[j]=L[s];
				}
			}
		}
	}
	
	cin >> q;
	for (i=0;i<q;i++){
		cin >> s >> t;
		if (L[s]==L[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int MAX=100000;
static const int NIL=-1;
int n;
vector<int>G[MAX]; //頂点数を100000とする
                   //隣接リスト
int color[MAX];
void dfs(int r,int c){
  stack<int>S;
  S.push(r); //始点rをスタックに追加
  color[r]=c;//色を入れる
  while(!S.empty()){ //スタックがからでない間
    int u=S.top();
    S.pop(); //スタックから頂点uを取り出し訪問
    for(int i=0;i<G[u].size();i++){
      //頂点uに隣接する頂点vを探索
      int v=G[u][i];
      if(color[v]==NIL){
	color[v]=c;
	S.push(v); //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); //sからtへ辺
    G[t].push_back(s); //tからsへ辺
  }
  int id=1; //colorに入れる色(情報)
  for(int i=0;i<n;i++){
    color[i]=NIL; //全てにNIL
  }
  for(int u=0;u<n;u++){
    if(color[u]==NIL) dfs(u,id++); //id(色)にかぶりがないように
  }
  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>
using namespace std;
#include<stdio.h>

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[3000][3000];
    int dist[3000];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct UnionFind{
    vector<int> par;
    UnionFind(int N) : par(N){
        for(int i = 0; i < N; i++){
            par[i] = -1;
        }
    }
    int root(int x){
        if(par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if(rx == ry) return;
        else{
            if(par[rx] > par[ry]) swap(rx, ry);
            par[rx] += par[ry];
            par[ry] = rx;
       }
    }
    bool same(int x, int y){
        return root(x) == root(y);
    }
    void check(){
        for(auto x : par){
            cout << x << " ";
        }
        cout << endl;
    }
};

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

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

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