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>
using namespace std;
static const int MAX=1000000;
static const int NIL=-1;
int n;
vector<int>G[MAX];//頂点数を1000000
                  //隣接セット
int color[MAX];

void dfs(int r,int c){
  stack<int>S;
  S.push(r);//始点rをスタックに追加
  color[r]=c; //色を入れる
  while(!S.empty()){ //スタックが空でない場合
    int u=S.top();
    S.pop(); //スタックからuを取り出し訪問
    for(int i=0;i<G[u].size();++i){
      //頂点uに隣接する頂点vを探索
      int v=G[u][i];
      if(color[v]==NIL){
	color[v]=c;
	S.push(v); //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); //t→s
  }
  int id=1; //colorに入れる色
  for(int i=0;i<n;++i){
    color[i]=NIL; //すべてにNIL
  }
  for(int u=0;u<n;++u){
    if(color[u]==NIL) dfs(u,id++);
  }
  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 <stdio.h>
#include <vector>
#include <queue>
#define NUM 100000
#define NIL -1
using namespace std;

int node_num;
vector<int> v[NUM + 1];
queue<int> Q;
int color[NUM + 1];

bool is_connected(int a, int b){
    if(color[a] == color[b]){
        return true;
    }else{
        return false;
    }
}

void add_next(int n){
    for(int i = 0; i < v[n].size(); i++){
        if(color[v[n][i]] != NIL)
            continue;
        Q.push(v[n][i]);
        color[v[n][i]] = color[n];
    }
}

void bfs(){
    for(int i = 1; i <= node_num; i++){
        if(color[i] != NIL)
            continue;
        color[i] = i;
        Q.push(i);
        add_next(i);

        while(Q.size()){
            int top = Q.front();
            Q.pop();
            add_next(top);
        }
    }
    return;
}

void init(){
    for(int i = 0; i <= NUM; i++){
        color[i] = NIL;
    }
}

int main(void){
    init();

    int m;
    scanf("%d", &node_num);
    scanf("%d", &m);

    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        v[a].push_back(b);
        v[b].push_back(a);
    }

    bfs();
     
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        if(is_connected(a, b)){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
}

C++: #include <iostream>
#include <map>
#include <queue>
using namespace std;
int main(){

  int n,m,t1,t2;
  map <int,bool> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;
    g[t1][t2]=1;
    g[t2][t1]=1;
  }
  int l;
  cin>>l;
  for(int i=0;i<l;i++){
    queue <int> q;
    bool f[100000]={1},find=false;
    cin>>t1>>t2;
    q.push(t1);
    for(;!q.empty();){
      for(int j=0;j<n;j++) if(g[q.front()][j]&&!f[j]){
          if(j==t2) find=true;
          f[j]=true;
          q.push(j);
	}
      q.pop();
      if(find) break;
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX_N = 100010;
static const int NIL = -1;

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

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

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

  cin >> n >> m;

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

  for (int i = 0; i < n; ++i) {
    color[i] = NIL;
  }

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

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

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

const int NMAX = 100000;
const int MMAX = 100000;

vector<int> table[NMAX]; 

int color[NMAX];

void search(int s, int id, int n) {
	queue<int> q;
	q.push(s);
	color[s] = id;

	while(true) {
		if (q.empty()) {
			return;
		}
		int current = q.front(); q.pop();
		for (int i = 0; i < table[current].size(); i++) {
			int v = table[current][i];
			if (color[v] == -1) {
				q.push(v);
				color[v] = id;
			}
		}
	}
}

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

int main() {
	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &s, &t);
		table[s].push_back(t);
		table[t].push_back(s);
	}

	assignColor(n);

	int q;
	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		scanf("%d %d", &s, &t);

		bool result = color[s] == color[t];
		if (result) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}

	return 0;
}