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<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<ctime>

void dfs(std::vector<std::vector<int> > &n) {
	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		std::stack<int> s;
		std::vector<std::vector<int> > went(n.size(), std::vector<int>(n.size(), 0));
		s.push(from);
		bool flag = false;
		while (s.size() != 0 && flag == false){
			int num = s.top(); s.pop();
			for (size_t j = 0; j < n[num].size(); j++) {
				if (n[num][j] == to) {
					flag = true;
					break;
				}
				if (went[num][j] == 0) {
					s.push(n[num][j]);
					went[num][j] = 1;
				}
			}
		}
		if (flag) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	dfs(n);

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};

void dfs (int v, int marker){
	if(c[v]!=0){
		return;
	}
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		if (c[i]==0){
			dfs(i,marker);
			marker++;
		}
	}
	
	
	cin >> q;
	int d[2][q];
	for (int i=0;i<q;i++){
		cin >> d[i][0];
		cin >> d[i][1]; 
	}
	for (int i=0;i<q;i++){
		if(c[d[i][0]]==c[d[i][1]]){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}
	
	out(c);
	return 0;
}

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

const int MAX = 100005;
const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];


void dfs(int r, int c) {
  color[r] = c;
  for (int u: G[r]) {
    if (color[u] == NIL) {
      dfs(u, c);
    }
  }
}

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);
  }
  int color_id {0};
  memset(color, NIL, sizeof color);
  for (int i = 0; i < n; i++) {
    if (color[i] == NIL) {
      dfs(i, color_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>
#include <assert.h>
using namespace std;
#define MAX_V 100000

int represent[MAX_V];
int status[MAX_V];

int findRepresent(int x)
{
    if (represent[x] == x) {
        return x;
    } else {
        represent[x] = findRepresent(represent[x]);
        return represent[x];
    }
}

void unite(int x, int y)
{
    x = findRepresent(x);
    y = findRepresent(y);
    if (x == y)
        return;
    if (status[x] < status[y])
        swap(x, y);
    assert(status[x] >= status[y]);
    represent[y] = x;
    if (status[x] == status[y])
        status[x]++;
}

bool same(int x, int y)
{
    return (findRepresent(x) == findRepresent(y));
}

int main()
{
    for (int i = 0; i < MAX_V; ++i) {
        represent[i] = i;
        status[i] = 0;
    }

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        unite(a, b);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int from, to;
        cin >> from >> to;
        cout << (same(from, to)? "yes" : "no") << '\n';
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
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;
    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<iostream>
using namespace std;
#include<stdio.h>

int *P;

int root(int a)
{
    if(P[a] == a) return a;
    P[a] = root(P[a]);
    return P[a]; 
}

void join(int a, int b){ int x = root(a), y = root(b); x = P[y]; }

bool same(int a, int b){ return (root(a) == root(b) ? true : false); }

int main()
{
    int i, n, m, a, b, q;
    scanf("%d %d", &n, &m);
    P = new int [n];
    for(i = 0; i < n; i++) P[i] = i;

    for(i = 0; i < m; i++) join(a, b);

    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(same(a, b)){ printf("yes\n"); }else{ printf("no\n"); }
    }
    delete [] P;
    return 0; 
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct UnionFind{
  int n;
  vector<int> p,r;
  UnionFind(){}
  UnionFind(int n):n(n){init();}
  void init(){
    p.clear();
    r.clear();
    p.resize(n);
    r.resize(n,1);
    for(int i=0;i<n;i++) p[i]=i;
  }
  int find(int x){
    return (p[x]==x?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
    x=find(x);y=find(y);
    return x==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);
    p[y]=x;
    r[x]+=r[y];
  }
};
signed 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;
    cout<<(uf.same(s,t)?"yes":"no")<<endl;
  } 
  return 0;
}