Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include <bits/stdc++.h>
using namespace std;
#define NIL -1

vector<int> G[100003];
int color[100003];
int n;

int nextPoint(int curPoint){
    int next;
    for(int i=0;i<G[curPoint].size();++i){
        next = G[curPoint][i];
        if(color[next] == NIL){
            return next;
        }
    }
    return -1;
}

void dfs(int point, int value){
    color[point] = value;
    while(1){
        int next = nextPoint(point);
        if(next != -1){
            dfs(next,value);
        }
        else{
            color[point] = value;
            return;
        }
    }
}

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

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

void bfs(int s);

int n_user,m,*deg;
vector<int> *G; 

int main(void){
	cin>>n_user>>m;
	deg = new int[n_user];
	G = new vector<int>[n_user];
	for(int i=0;i<n_user;i++)
		deg[i] = -1;

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

	for(int i=0;i<n_user;i++) bfs(i);

	int question,s,t;
	cin>>question;
	for(int i=0;i<question;i++){
		cin>>s>>t;
		if(deg[s]!=-1){
			if(deg[s]==deg[t])
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	delete [] G; delete [] deg;
}

void bfs(int s){
	queue<int> q;
	q.push(s);
	if(deg[s]==-1) deg[s] = s;
	else return ;

	int u;
	while(!q.empty()){
		u = q.front();
		q.pop();
		for(int v=0;v<G[u].size();v++){
			if(deg[G[u][v]]==-1){
				deg[G[u][v]] = deg[u];
				q.push(G[u][v]);
			}
		}
	}
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

int n,m,q,from,to;
int rank[100000],parent[100000];

int find(int x){
	if(x == parent[x])return x;
	else{
		return parent[x] = find(parent[x]);
	}
}

bool isSame(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(rank[x] < rank[y]){
		parent[x] = y;
	}else{
		parent[y] = x;
		if(rank[x] == rank[y])rank[x]++;
	}
}

int main(){

	scanf("%d %d",&n,&m);

	for(int i = 0; i < n; i++){
		rank[i] = 0;
		parent[i] = i;
	}

	for(int i = 0; i < m; i++){
		scanf("%d %d",&from,&to);

		if(isSame(from,to))continue;
		unite(from,to);
	}

	scanf("%d",&q);

	int left,right;

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

		if(isSame(left,right))printf("yes\n");
		else{
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <list>
using namespace std;

class Node
{
private:
public:
	Node();
	int id;
	list<int> next_node_list;
	int next_idx;
	void AddNextNode(int id);
	void SetGroup(int prev_id, int num);
	int group;
	void Init(int id);
};

Node::Node()
{
	group = 0;
	next_idx = 0;
}

void Node::AddNextNode(int id)
{
	next_node_list.push_back(id);
}

Node *Node_list;

void Node::SetGroup(int prev_id, int num)
{
	group = num;

	list<int>::iterator itr;
	itr = next_node_list.begin();
	for (int i = 0; i < next_idx; i++) {
		itr++;
	}
	
	for(; itr != next_node_list.end(); itr++){
		next_idx++;
		if (prev_id != *itr) {
			Node_list[*itr].SetGroup(id, num);
		}
	}
}

void Node::Init(int id)
{
	this->id = id;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n]();
	for (int i = 0; i < n; i++) {
		Node_list[i].Init(i);
	}
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}

	//group
	int group = 1;
	for (int i = 0; i < m; i++) {
		if (Node_list[i].group) continue;
		Node_list[i].SetGroup(-1, group);
		group++;
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (Node_list[s].group == Node_list[t].group && Node_list[s].group != 0) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
void refresh(queue<Node> que,Node node[],int n){
    while(!que.empty())
        que.pop();

    for(int i=0; i<n; i++)
        node[i].flag=false;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

    for(int i=0; i<m; i++){
        cin >> s >> t;

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(x==t){
                answer[i]=true;
                break;
            }

            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);
            }
         }

          que.pop();
        }

        refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}