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++: #include<iostream>
#include<vector>
#include<stack>
#include<algorithm>

void color(std::vector<std::vector<int> > n) {
	std::vector<int> color(n.size(), 0);
	std::stack<int> s;

	int count = 1;
	for (size_t i = 0; i < n.size(); i++) {
		if (color[i] != 0) continue;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				if (color[*it] == 0) {
					s.push(*it);
					color[*it] = count;
				}
			}
		}
		count++;
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (color[from] == color[to]) {
			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);
	}

	color(n);

	return 0;
}

C++: #include<iostream>
#include<vector>
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 col) {
	color[r] = col;
	for (int i = 0; i < G[r].size(); i++) {
		int v = G[r][i];
		if (color[v] == NIL) {
			dfs(v, col);
		}
	}
}

void assignColor() {
	int col = 0;
	for (int i = 0; i < n; i++) {
		color[i] = NIL;
	}
	for (int i = 0; i < n; i++) {
		if (color[i] == NIL) {
			dfs(i, col++);
		}
	}
}

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

	assignColor();

	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> f >> s;
		if (color[f] == color[s]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>

#define N 100000

using namespace std;

vector<int> G[N];
int color[N];

void dfs(int s,int c){
  color[s]=c;
  for(int i=0;i<G[s].size();i++){
    if(color[G[s][i]]!=c) dfs(G[s][i],c);
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<m;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int q; scanf("%d",&q);
  for(int i=0;i<q;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    dfs(s,i);
    if(color[s]==color[t]) cout << "yes\n";
    else cout << "no\n";
  }
}

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

const int maxm = 100000 + 5;
const int maxn = 100000 + 5;

struct Edge{
  int to,next;
} G[maxm<<1];

int head[maxn],cnt = 0;
int color[maxn];
bool vis[maxn];

void Insert(int u,int v){
  G[++cnt].next = head[u];
  G[cnt].to = v;
  head[u] = cnt;
}

void Dfs(int u,int c){
  vis[u] = true;
  color[u] = c;
  for (int e = head[u];e; e = G[e].next) {
    int v = G[e].to;
    if (!vis[v]) {
      Dfs(v,c);
    }
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for (int i=1;i<=m; i++) {
    int u,v;
    scanf("%d %d",&u,&v);
    Insert(u,v);
    Insert(v,u);
  }
  for (int i=1; i<=n; i++) {
    if (!vis[i]) {
      Dfs(i,i);
    }
  }
  int q;
  scanf("%d",&q);
  for(int i=1; i<=q; i++){
    int u,v;
    scanf("%d%d",&u,&v);
    if (color[u]==color[v]) {
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

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++){ scanf("%d %d", &a, &b); 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++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector< vector<int> >
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int main(){
    int N, M;
    cin >> N >> M;
    Graph G(N);
    rep(i, M){
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    vector<int> connect(N, -1);

    for(int c = 0; c < N; c++){
        if(connect[c] != -1) continue;

        queue<int> que;
        connect[c] = c;
        que.push(c);

        while(!que.empty()){
            int v = que.front();
            que.pop();
            for(int i = 0; i < G[v].size(); i++){
                int v2 = G[v][i];
                if(connect[v2] != -1) continue;
                connect[v2] = c;
                que.push(v2);
            }
        }
    }

    int q;
    cin >> q;
    rep(i, q){
        int s, t;
        cin >> s >> t;
        if(connect[s] == connect[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}