Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func newGraph(nNodes int) *Graph {
	g := Graph{}
	g.nodes = make([]*GraphNode, nNodes)
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode] = &GraphNode{}
		g.nodes[iNode].key = GraphNodeKey(iNode)
	}
	return &g
}

var infty = 1000000000

type GraphNodeKey int

// GraphNode is a node for undirected graph
type GraphNode struct {
	key       GraphNodeKey
	adjacents []*GraphNode
	visited   bool
	d, f      int
	parent    *GraphNode
	cc        int
}

func (g *Graph) setAdjacent(l, r GraphNodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

// Graph is a undirected graph
type Graph struct {
	nodes []*GraphNode
}

func (g *Graph) initialize() {
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode].visited = false
		g.nodes[iNode].d = infty
		g.nodes[iNode].f = infty
		g.nodes[iNode].parent = nil
		g.nodes[iNode].cc = -1 // 22.3-12
	}
}

type QueueEntry struct {
	node *GraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *GraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

var time = 0

func fillDistancesByDepthFirstSearch(g *Graph) {
	g.initialize()
	time = 0
	cc := 0

	for _, u := range g.nodes {
		if !u.visited {
			dfsVisit(g, u, cc)
			cc++
		}
	}
}

func dfsVisit(g *Graph, u *GraphNode, cc int) {
	time++
	u.d = time
	u.cc = cc

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v, cc)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nUsers := nextInt()
	nEdges := nextInt()
	graph := newGraph(nUsers)
	for iEdge := 0; iEdge < nEdges; iEdge++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())
		graph.setAdjacent(l, r)
		graph.setAdjacent(r, l)
	}

	fillDistancesByDepthFirstSearch(graph)

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())

		// fmt.Println(graph.nodes[l].d, graph.nodes[r].d, graph.nodes[r].f, graph.nodes[l].f)
		if graph.nodes[l].cc == graph.nodes[r].cc {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: //
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  int n,m,join[100000],num[100000],s,t;
  cin >> n >> m;

  for(int i=0; i<n; i++){
    join[i] = i;
    num[i] = 1;
  }

  for(int i=0; i<m; i++){
    cin >> s >> t;

    if(join[t] != join[s]){
      int js = join[s];
      int jt = join[t];

      jt = js; 
      num[js] += 1;
      num[jt] -= 1;
      
      
      for(int j=0,count=0; count<num[t]; j++){
	if(join[j]==jt){
	  join[j] = js;
	  num[t] += 1;
	  num[t] -= 1;
	}
      }
    }
  }

  int q;
  cin >> q;
  bool answer[10000];

  for(int i=0; i<q; i++){
    answer[i] = false;
    cin >> s >> t;

    if(join[s]==join[t])
      answer[i] = true;
  }

  for(int i=0; i<q; i++){
    if(answer[i])
      cout << "yes\n";

    else
      cout << "no\n";
  }
}

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>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
using Graph = vector<vector<ll>>;

ll n;
vector<ll> fri[100100];
int color[100100];

void dfs(int r, int c){
  stack<ll> s;
  s.push(r);
  color[r] = c;
  while(!s.empty()){
    int u = s.top(); s.pop();
    rep(i, 0, fri[u].size()){
      int v = fri[u][i];
      if(color[v] == -1){
        color[v] = c;
        s.push(v);
      }
    }
  }
}

void assignColor(){
  int id = 1;
  rep(i, 0, n) color[i] = -1;
  rep(u, 0, n){
    if(color[u] == -1) dfs(u, id++);
  }
}

int main() {
  ll m; cin >> n >> m;
  
  rep(i, 0, m){
    ll a, b; cin >> a >> b;
    fri[a].push_back(b);
    fri[b].push_back(a);
  }

  assignColor();

  ll q; cin >> q;
  rep(i, 0, q){
    ll a, b; cin >> a >> b;
    if(color[a] == color[b]) puts("yes");
    else puts("no");
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#define P pair<int, int>

using namespace std;

int main(){
  int n, m, q, c[100009];
  vector<int> G[100009];
  queue<int> Q;
  memset(c, -1, sizeof(c));
  cin >> n >> m;
  for(int i=0;i<m;i++){
    int s, e;
    cin >> s >> e;
    G[s].push_back(e);
    G[e].push_back(s);
  }
  for(int i=0;i<n;i++){
    if(c[i]!=-1)continue;
    c[i] = i;
    Q.push(i);
    while(!Q.empty()){
      int node = Q.front();Q.pop();
      for(int j=0;j<G[node].size();j++){
        if(c[G[node][j]]==-1){
          c[G[node][j]] = i;
          Q.push(G[node][j]);
        }
      }
    }
  }
  cin >> q;
  for(int i=0;i<q;i++){
    int s, e;cin >> s >> e;
    cout << (c[s] == c[e]? "yes": "no") << endl;
  }
}

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_D: Connected Components

#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>

using uint = unsigned int;

class Graph {
  std::vector<std::vector<uint>> g_;
  std::vector<uint> root_;
  
public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

  void add(uint from, uint to)
  {
    g_[from].push_back(to);
  }

  void setup()
  {
    std::fill(root_.begin(), root_.end(), UINT_MAX);
    for (auto i = 0u; i < g_.size(); ++i ) {
      dfs(i, i);
    }
  }

  bool reachable(uint from, uint to) const
  {
    return root_[from] == root_[to];
  }

private:
  void dfs(uint i, uint root)
  {
    if (root_[i] != UINT_MAX) return;
    root_[i] = root;
    for (auto j = 0u; j < g_[i].size(); ++j) {
      dfs(g_[i][j], root);
    }
  }
  
};

int main()
{
  uint n, m;
  std::scanf("%u %u", &n, &m);

  Graph g(n);

  uint s, t;
  for (auto i = 0u; i < m; ++i) {
    std::scanf("%u %u", &s, &t);
    g.add(s, t);
  }
  g.setup();

  uint q;
  std::scanf("%u", &q);
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%u %u", &s, &t);
    std::puts(g.reachable(s, t) ? "yes" : "no");
  }
}

// eof