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<iostream>
#include<vector>
#include<stack>
#include<algorithm>

void color(std::vector<std::vector<int> > n) {
	std::vector<int> color(n.size(), 0);
	std::stack<int> s;

	int count = 1;
	for (size_t i = 0; i < n.size(); i++) {
		if (color[i] != 0) continue;
		color[i] = count;
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
				if (color[*it] == 0) {
					s.push(*it);
					color[*it] = count;
				}
			}
		}
		count++;
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (color[from] == color[to]) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	color(n);

	return 0;
}

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

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

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

void assignColor(){
	int id = 1;
	for(int i = 0; i<n; i++) color[i] = NIL;
	for(int u = 0; u<n; u++) {
		if(color[u] == NIL) dfs(u,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);
	}
	
	assignColor();
	
	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 <iostream>
#include <climits>
#include <stack>
#include <vector>

using namespace std;
static const int MAX=100;
static const int NIL=-1;
int n;
vector <int> G[MAX];
int color[MAX];

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

void assign_color()
{
    int id=1;
    for(int i=0;i<n;i++) color[i]=NIL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NIL) dfs(u,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);
    }
    assign_color();
    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<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

void dfs(int s, int id, vector<vector<int>> a, vector<int> &comp) {
	if (comp[s] == id) return;
	comp[s] = id;
	int k = a[s].size();
	REP(j, k) {
		int i = a[s][j];
		dfs(i, id, a, comp);
	}
	return;
}

void divide(int size, vector<vector<int>> a, vector<int> &comp) {
	int s = 0;
	int id = 1;
	while (1) {
		dfs(s, id, a, comp);
		while (s < size && comp[s]>0) s++;
		if (s == size) break;
		id++;
	}
	return;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	int q;
	cin >> q;
	vector<int> comp(n, 0);
	divide(n, a, comp);
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		string res = "no\n";
		if (comp[s] == comp[t]) res = "yes\n";
		cout << res;
	}

	return 0;
}

C++: #include <algorithm>
#include <cstdio>
using namespace std;

int par[100005];
int Rank[100005];

void init(int n){
  for(int i=0;i<n;i++){
    par[i]=i;
    Rank[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(Rank[x]<Rank[y]){
    par[x]=y;
  }else{
    par[y]=x;
    if(Rank[x]==Rank[y])Rank[x]++;
  }
}

bool same(int x,int y){
  return find(x)==find(y);
}

int main(){
  int n,m,q,s,t;
  scanf("%d%d",&n,&m);
  init(n);
  for(int i=0;i<m;i++){
    scanf("%d%d",&s,&t);
    if(same(s,t))continue;
    unite(s,t);
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++){
    scanf("%d%d",&s,&t);
    if(same(s,t))printf("yse\n");
    else printf("no\n");
  }
  return 0;
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		printf("%d,", g->id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}