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 <string>

using namespace std;

static const int N = 100000;
static const int NIL = -1;

class ListGraph {
private:
    vector<int> Graph[N];
    int Group[N];
    int n;

public:
    ListGraph()
    {
        cin >> n;
        int m;
        cin >> m;
        int u, v;
        for(int i = 0; i < m; i++)
        {
            cin >> u >> v;
            Graph[u].push_back(v);
            Graph[v].push_back(u);
        }
    }

    void dfs(int start, int group)
    {
        stack<int> stack;
        stack.push(start);
        Group[start] = group;
        while(!stack.empty())
        {
            int u = stack.top();
            stack.pop();
            for(int i = 0; i < Graph[u].size(); i++) // uの全隣接頂点に対して
            {
                int v = Graph[u][i];
                if(Group[v] == NIL) // 未訪問(グループ未定)のとき
                {
                    Group[v] = group; // 自分のグループに含める
                    stack.push(v);
                }
            }
        }
    }

    void grouping()
    {
        int id = 1;
        for(int i = 0; i < n; i++)
            Group[i] = NIL;
        for(int u = 0; u < n; u++)
        {
            if(Group[u] == NIL)
                dfs(u, id++);
        }
    }

    string answer(int start, int goal)
    {
        if(Group[start] == Group[goal])
            return "yes";
        else
            return "no";
    }
};

int main()
{
    ListGraph graph;
    graph.grouping();
    int question;
    cin >> question;
    int start, goal;
    for(int i = 0; i < question; i++)
    {
        cin >> start >> goal;
        cout << graph.answer(start, goal) << endl;
    }
}

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

using namespace std;

struct uft {
    uft (int s) : ps {new int[s]}, ds {new int[s]}, s {s} {
        for(int i = 0; i < s; ++i){
            ps[i] = i;
            ds[i] = 0;
        }
    };
    void unite(int x, int y){
        int px = parent(x);
        int py = parent(y);
        if(ds[px] < ds[py]){
            ps[px] = py;
        } else if(ds[px] > ds[py]){
            ps[py] = px;
        } else {
            ps[px] = py;
            ++ds[py];
        }
    };
    int parent(int p){
        return p == ps[p] ? p : ps[p] = parent(ps[p]);
    };
    bool same(int x, int y){
        return parent(x) == parent(y);
    };
    int* ps;
    int* ds;
    int s;
};

struct user {
    user() {};
    user(int i) : id {i}, fs {}, group {-1} {};
    int id;
    vector<int> fs;
    int group;
};

void add_friends(user *us, int x, int y){
    us[x].fs.push_back(y);
    us[y].fs.push_back(x);
}

void dfs(user* us, int id, int g){
    stack<int> ss {};
    ss.push(id);
    while(ss.size()){
        int p = ss.top();
        ss.pop();
        us[p].group = g;
        vector<int> fs = us[p].fs;
        for(auto x : fs){
            if(us[x].group == g){
                continue;
            }
            ss.push(x);
        }
    }
}

void make_group(user* us, int n){
    int g = 0;
    for(int i = 0; i < n; ++i){
        if(us[i].group < 0){
            dfs(us, i, g);
            ++g;
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    //user us[n];
    auto us = uft(n);
    /*for(int i = 0; i < n; ++i){
        us[i] = user(i);
    }*/
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        us.unite(a, b);
    //    add_friends(us, a, b);
    }
    //make_group(us, n);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++ i){
        int a, b;
        cin >> a >> b;
        //if(us[a].group == us[b].group){
        if(us.same(a, b)){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=1<<21;
const int maxn=100+10;
int n,m,q;
int p[maxn];
int rank[maxn];
void init(int n)
{
   for(int i=0;i<n;i++)
   {
       p[i]=i;
       rank[i]=0;
   }
}
int fnd(int x)
{
    if(x==p[x]) return x;
    return fnd(p[x]);
}
bool is_same(int x,int y)
{
   return fnd(x)==fnd(y);
}

void uni(int x,int y)
{
   x=fnd(x);
   y=fnd(y);
   if(rank[x]>rank[y])
   {
       p[y]=x;
   }
   else
   {
       p[x]=y;
       if(rank[x]==rank[y]) rank[y]++;
   }
}

int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int x,y;
    init(n);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        uni(x,y);
    }
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&x,&y);
        if(is_same(x,y)) printf("yes\n");
        else printf("no\n");
    }


    return 0;
}