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++: //#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");
	}
}

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

bool search(vector<int> member,int x){
  for(int i=0; i<member.size(); i++){
    if(member[i] == x)
      return true;
  }
    return false;
}
vector<int> update(vector<int> mother,vector<int> children,int join[],int a,int b){
  mother.push_back(b);
  join[b] = join[a];
  for(int i=0; i<children.size(); i++){
    int num=children[i];
    mother.push_back(num);
    join[num] = join[a];
  }

  return mother;
}
int main(){
  int n,m,join[100000],s,t;
  vector<int> group[100000];
  cin >> n >> m;

  for(int i=0; i<n; i++)
    join[i] = i;
    
  for(int i=0; i<m; i++){
    cin >> s >> t;
    bool flag = search(group[join[t]],s);

    if(!flag){
      int x = join[t];
      int y = join[s];

      if(x!=t || !group[x].empty()){
	group[x] = update(group[x],group[y],join,x,y);
	group[y].clear();
      }

      else{
	group[y].push_back(t);
	join[x] = y;
      }
    }

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

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

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

void G_find(vector<vector<int> > A, vector<int> &v, int u){
	v[u] = 1;
	for(int i=0;i<v.size();i++)
		if((A[u][i] == 1) && (v[i] == 0))
			G_find(A, v, i);
}

int main(){
	int n, m, q, s, t;
	vector<vector<int> > A;
	vector<int> v;

	cin>>n>>m;
	A.resize(n);
	v.resize(n);
	for(int i=0;i<n;i++)
		A[i].resize(n);

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			A[i][j] = 0;

	for(int i=0;i<m;i++){
		cin>>s>>t;
		A[s][t] = 1;
		A[t][s] = 1;
	}

	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s>>t;
		for(int j=0;j<n;j++)
			v[j] = 0;

		G_find(A, v, s);

		if(v[s] == v[t])
			cout<<"yes"<<endl;
		else
			cout<<"no"<<endl;
	}

	return 0;
}

C++: 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<vector>

#define NIL -1
int color[100000];
std::vector<int>V[100005];

void dfs(int i,int id){
    std::stack<int>S;
    S.push(i);
    color[i]=id;
    int j;
    while(!S.empty()){
        int top=S.top();
        S.pop();
        for(j=0;j<V[top].size();j++){
            if(color[V[top][j]]==NIL){
                color[V[top][j]]=id;
                S.push(V[top][j]);
            }
        }
    }
}
int main(){
    int n,m,i;
    scanf("%d %d",&n,&m);
    
    for(i=1;i<=m;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        V[s].push_back(t);
        V[t].push_back(s);
    }
    for(i=0;i<=n-1;i++)color[i]=NIL;
    int id=1;
    for(i=0;i<=n-1;i++){
        if(color[i]==NIL){
            id++;
            dfs(i,id);
        }
    }
    
    int q;
    scanf("%d",&q);
    for(i=1;i<=q;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        if(color[s]==color[t])printf("yes");
        else printf("no");
        printf("\n");
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int cnt=1;
struct Point{
	int x;
	int y;
};

bool depth(vector< vector<int> > &vv,vector<int> &f,vector<int> &c,int num1,int num2){
	
	f[num1]=cnt;
	cnt++;
	
	for(int i=0; i<vv[num1].size(); i++){
		if(vv[num1][i]!=0 && f[i]==0){
			depth(vv,f,c,i,num2);
		}
	}

	if(f[num2]!=0){
		return 1;
	}else{
		return 0;
	}	

}
int main(){
	int n,m,l;
	cin>>n>>m;
	vector< vector<int> > v(n,vector<int> (n,0));
	vector<int> ft(n,0);
	vector<int> ct(n,0);
	for(int i=0; i<m; i++){
		Point a;
		cin>>a.x>>a.y;
		v[a.x][a.y]=1;
	}
	cin>>l;
	vector<string> ans(l);
	for(int i=0; i<l; i++){
		Point b;
		cin>>b.x>>b.y;
		if(depth(v,ft,ct,b.x,b.y)){
			ans[i]="yes";
		}else{
			ans[i]="no";
		}
	}
	for(int i=0; i<l; i++){
		cout<<ans[i]<<endl;
	}
  return 0;
}