Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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<cstdio>
#include<vector>
#include<stack>
using namespace std;
int n;
vector<int> a[100000];
int b[100000];
void dfs(int r,int c){
    stack<int> x;
    x.push(r);
    b[r]=c;
    while(!x.empty()){
        int s=x.top();
        x.pop();
        for(int i=0;i<a[s].size();i++){
            int v=a[s][i];
            if(b[v]==-1){
                b[v]=c;
                x.push(v);
            }
        }
    }
}
void pengyou(){
    int id =1;
    for(int i=0;i<n;i++)b[i]=-1;
    for(int i=0;i<n;i++){
        if(b[i]==-1)dfs(i,id++);
    }
}

int main(){
    int m;
    int s,t;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%d%d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    pengyou();
    int q;
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d%d",&s,&t);
        if(b[t]==b[s])printf("yes\n");
        else printf("no\n");


    }

}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
static const ll MAX = 110000;
static const ll NIL = -1;

ll n;
vector<ll> G[MAX];
ll color[MAX];

void dfs(ll r, ll c) {
  stack<ll> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    ll u = S.top(); S.pop();
    for (ll i = 0; i < G[u].size(); i++) {
      ll v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assignColor() {
  ll id = 1;
  for (ll i = 0; i < n; i++) color[i] = NIL;
  for (ll u = 0; u < n; u++) {
    if (color[u] == NIL) dfs(u, id++);
  }
}

int main() {
  ll s, t, m, q;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (ll 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 <map>
#include <queue>
using namespace std;
int main(){
  int n,m,t1,t2,c[100000]={}; map <int,bool> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;g[t1][t2]=1;g[t2][t1]=1;
  }
  int l;cin>>l;
  for(int i=0;i<l;i++){
    queue <int> q;
    bool f[100000]={},find=false;
    cin>>t1>>t2;
    if(c[t1]&&c[t2]){
      if(c[t1]==c[t2]) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
      continue;
    }
    q.push(t1);f[t1]=true;c[t1]=i+1;
    for(;!q.empty();){
      for(int j=0;j<n;j++) if(g[q.front()][j]&&!f[j]){
          if(j==t2) find=true;
          f[j]=true;c[j]=i+1;
          q.push(j);
	}
      q.pop();
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;

template<u32 N_>
class UnionFindTree {
private:
  u32 par[N_];
  u32 rank[N_];
public:
  UnionFindTree(u32 n) {
    assert(n < N_);
    for(u32 i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
    return;
  }
  int find(u32 x) {
    if( par[x] == x ) return x;
    return par[x] = find(par[x]);
  }
  void unite(u32 x, u32 y) {
    x = find(x);
    y = find(y);
    if( x == y ) return;
    if( rank[x] < rank[y] ) {
      par[x] = y;
      return;
    }
    par[y] = x;
    if( rank[x] == rank[y] ) rank[x] += 1;
    return;
  }  
};

int main() {

  u32 n, m;
  std::cin >> n >> m;

  UnionFindTree<200000> uft(n);

  for(u32 i = 0; i < m; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    uft.unite(x, y);
  }

  u32 q;
  std::cin >> q;

  for(u32 i = 0; i < q; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    std::cout << (uft.find(x) == uft.find(y) ? "yes" : "no") << std::endl;
  }
  
  return 0;
}

C++: #include <vector>
#include <iostream>
using namespace std;
int n, m, q, x, y, t; int group[111111]; vector<int> f[111111];
int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) group[i] = i, f[i].push_back(i);
	for(int i = 0; i < m; i++) {
		scanf("%d%d", &x, &y);
		if(group[x] == group[y]) continue;
		x = group[x], y = group[y], t = f[y].size();
		if(f[y].size() > f[x].size()) swap(x, y);
		for(int j = 0; j < t; j++) {
			group[f[y].back()] = group[x];
			f[x].push_back(f[y].back());
			f[y].pop_back();
		}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &x, &y);
		printf("%s\n", group[x] == group[y] ? "yes" : "no");
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5

vector<int> G[MAX];
queue<int> qu;

int main() {
	int n, m, q;
	int ind,tmp;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		flag = 0;
		qu.push(ind);
		while (not qu.empty()) {
			int u = qu.front(); qu.pop();
			if (u == tmp) {
				flag = 1;
				break;
			}
			for (int j = 0; j < G[u].size(); j++) {
				qu.push(G[u][j]);
			}
		}
		if (not flag) {
			swap(ind, tmp);
			qu.push(ind);
			while (not qu.empty()) {
				int u = qu.front(); qu.pop();
				if (u == tmp) {
					flag = 1;
					break;
				}
				for (int j = 0; j < G[u].size(); j++) {
					qu.push(G[u][j]);
				}
			}
		}
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,m,i,s,t,p[1<<17],q;
int root(int u){
  return p[u]==u?u:p[u]=root(p[u]);
}
int main(void){
  for(i=0;i<1<<17;i++)p[i]=i;
  cin>>n>>m;
  while(m--){
    cin>>s>>t;
    s=root(s);
    t=root(t);
    // if(s>t)swap(s,t);
    if(s!=t)p[t]=s;
  }
  cin>>q;
  while(q--){
    cin>>s>>t;
    cout<<(root(s)==root(t)?"yes":"no")<<endl;
  }
  return 0;
}