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<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
using ll=long long;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=1e9;
const ll INF=1e18;
using P=pair<int,int>;
int par[100005]={};
int find(int x) {
  if(par[x]==x) {
    return x;
  }
  return par[x]=find(par[x]);
}
bool same(int x,int y) {
  return par[x]==par[y];
}
void unit(int x,int y) {
  x=find(x),y=find(y);
  if(x!=y) {
    par[x]=y;
  }
}
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  int n,m,s,t,q;
  cin >> n >> m;
  for(int i=0; i<n; i++) {
    par[i]=i;
  }
  for(int i=0; i<m; i++) {
    cin >> s >> t;
    unit(s,t);
  }
  cin >> q;
  while(q--) {
    cin >> s >> t;
    if(find(s)==find(t)) {
      cout << "yes" << "\n";
    }
    else {
      cout << "no" << "\n";
    }
  }
}

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

int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
	d[i]=i;
}
for(int l=0;l<m;l++){
	int j,h;
	cin>>j>>h;
while(d[j]!=j) {
	j=d[j];
}
while(d[h]!=h) {
	h=d[h];
}
if(j!=h){
	d[j]=h;
}
}
cin>>q;
for(int y=0;y<q;y++){
int j,h;
	cin>>j>>h;
while(d[j]!=j) {
	j=d[j];
}
while(d[h]!=h) {
	h=d[h];
}
	if(j==h){
		cout<<"yes"<<endl;
	}
	else{
		cout<<"no"<<endl;
	}
}
	return 0;
}

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

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define floot10 cout<<fixed<<setprecision(10);
#define int long long

struct UnionFind{
  int n;
  vector<int> r,p;
  UnionFind(){}
  UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
  int find(int x){
	return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
	return find(x)==find(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);
	r[x]+=r[y];
	p[y]=x;
  }
};


signed main(){
	int n,m;
	cin>>n>>m;
	UnionFind hoge(n);
	lp(i,m){
		int a,b;
		cin>>a>>b;
		hoge.unite(a,b);
	}
	int q;
	cin>>q;
	lp(i,q){
		int a,b;
		cin>>a>>b;
		if(hoge.same(a,b)==true) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

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

const int N = 100000;
int color[N];
std::vector<int> adj_list[N];

void DFS(int node_id, int c)
{
    std::stack<int> S;
    S.push(node_id);
    color[node_id] = c;

    while(!S.empty()) {
        int target = S.top();
        S.pop();
        for (int i = 0; i < adj_list[target].size(); i++) {
            int friend_id = adj_list[target][i];
            if (color[friend_id] == -1) {
                color[friend_id] = c;
                S.push(friend_id);
            }
        }
    }
}

void AssignColor(int n)
{
    int id = 1;
    for (int i = 0; i < n; i++) color[i] = -1;
    for (int i = 0; i < n; i++) {
        if (color[i] == -1) DFS(i, id++);
    }
}

int main(int argc, char *argv[])
{
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);

    // make adj matrix
    for (int i = 0; i < m; i++) {
        int node_id, friend_id;
        scanf("%d", &node_id);
        scanf("%d", &friend_id);
        adj_list[node_id].push_back(friend_id);
        adj_list[friend_id].push_back(node_id);
    }

    AssignColor(n);

    int q_num;
    scanf("%d", &q_num);
    for (int i = 0; i < q_num; i++) {
        int src, dst;
        scanf("%d", &src);
        scanf("%d", &dst);
        if (color[src] == color[dst]) fprintf(stdout, "yes\n");
        else fprintf(stdout, "no\n");
    }

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxN=100000;
int link[maxN+10];
struct bian{
    int next;
    int zhongdian;
    bian():next(-1),zhongdian(-1){}
}a[maxN+10];
int k=0;
int flag;
void dfs(int x,int y)
{

        int g=link[x];
        while(g!=-1)
        {
            if(a[g].zhongdian==y)
            {
               flag=1;
               return;
            }
            dfs(a[g].zhongdian,y);
            g=a[g].next;
        }
        return;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<maxN+10;i++)
    {
        link[i]=-1;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        a[k].zhongdian=y;
        a[k].next=link[x];
        link[x]=k;
        k++;
    }
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        flag=0;
        dfs(x,y);
       /* int g=link[x];
        while(g!=-1)
        {
            if(a[g].zhongdian==y)
            {
                flag=1;break;
            }
            g=a[g].next;
        }*/
        if(flag==1)
        {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
    return 0;
}