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 <algorithm> //find

using namespace std;

#define N_MAX 100000

int search_connection(int s, int t);
vector<int> G[N_MAX], check_v;

int main()
{
    int n, m, i, s, t, q;

    cin >> n >> m;

    //??£??\??????????????????
    for (i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    //??????????????????
    cin >> q;
    for (i = 0; i < q; i++) {
//        cout << "Q. " << i << endl;

        cin >> s >> t;

        if (check_v.size() != 0) check_v.clear();

        check_v.push_back(s);
        if ( search_connection(s, t) )
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

int search_connection(int s, int t) {
    int i;
//    cout << "search " << s << ", " << t << endl;

    for (auto itr = G[s].begin(); itr != G[s].end(); itr++) {
        if (*itr == t) {
            return 1;
        }
        else if ( find(check_v.begin(), check_v.end(), *itr) != check_v.end() ) {
            //*itr?????¢?????¢?´¢??????node?????´??????????????¢?´¢????????????
        }
        else {
            //*itr???????????¢?´¢???????????????node?????´???
            check_v.push_back(*itr);
            if ( search_connection(*itr, t) )
                return 1;
        }
    }

    return 0;
}

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

#define MAX 100000

using namespace std;

int visited[MAX];
//adjacent list
vector<int> G[MAX];

//can go from s to t or not
bool ans;

void dfs(int s,int t){
  visited[s]=1;
  if(s==t) ans=true;
  for(int i=0;i<G[s].size();i++){
    if(visited[G[s][i]]==0){
      dfs(G[s][i],t);
    }
  }
}

int main(){
  int n,m; cin >> n >> m;
  for(int i=0;i<m;i++){
    int x,y; cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  int q; cin >> q;
  for(int i=0;i<q;i++){
    for(int i=0;i<n;i++) visited[i]=0;
    ans=false;
    int s,t; cin >> s >> t;
    dfs(s,t);
    if(ans) cout << "yes" << endl;
    else cout << "no" << endl;
  }
}

C++: #include<iostream>
#include<vector>
#inlcude<stack>
using namespace std;
#define MAX 100000
#define NIL -1

int n, color[MAX];
vector<int> G[MAX];


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



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

int main() {

  int  m, i, j, v, s, t, q;

  cin >> n >> m;

  for (i=0; i<m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (i=0; i<q; i++) {
    cin >> s >> t;
    if (color[s]== color[t]) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
int par[100005];
int ra[100005];
void init(int n){
  for(int i=0;i<n;i++){
    par[i]=i;
    ra[i]=0;
  }
}
int find(int x){
  if(par[x]==x){
    return x;
  }
  else{
    return par[x]=find(par[x]);
  }
}
void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y)return;
  if(ra[x]<ra[y]){
    par[x]=y;
  }
  else{
    par[y]=x;
    if(ra[x]==ra[y])ra[x]++;
  }
}
bool same(int x,int y){
  return find(x)==find(y);
}

int main(){
  int n,m;
  cin >> n >> m;
  init(n);
  for(int i=0;i<m;i++){
    int a,b;
    cin >> a >> b;
    unite(a,b);
  }
  int q;
  cin >> q;
  for(int i=0;i<q;i++){
    int a,b;
    cin >> a >> b;
    if(same(a,b))cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

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

using namespace std;
const int maxn=100000+5;

int fa[maxn];
int V,E;
void init(){
    for(int i=1;i<=V;i++)
        fa[i]=i;
}

int find(int x){
    int y=fa[x];
    for(;fa[y]!=y;y=fa[y]);
    while(x!=y){
        int temp=fa[x];
        fa[x]=y;
        x=temp;
    }
    return y;
}

void merge(int x,int y){
    int nx=find(x);
    int ny=find(y);
    if(nx!=ny){
        fa[nx]=ny;
    }
}
int main(){
    cin>>V>>E;
    init();
    while(E--){
        int in,out;
        cin>>in>>out;
        merge(in,out);
    }
    int T;
    cin>>T;
    while(T--){
        int x,y;
        cin>>x>>y;
        if(find(x)!=find(y)) cout<<"no"<<endl;
        else cout<<"yes"<<endl;
    }
}

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

int x[100000][100];
bool y[100000];
int z[100000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][z[a]]=b;
		x[b][z[b]]=a;
		z[a]++;
		z[b]++;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<20;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<z[j];k++){
						y[x[j][k]]=true;
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}