Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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

std::vector<int> v[100010];
int n;
int m;

bool visited[10010];

bool dfs(int from, int to) {

  if(visited[to]) {
    return true;
  }

  visited[from] = true;
  for(int u=0; u<v[from].size(); ++u) {
    if(visited[v[from][u]]) continue;
    else {
      visited[v[from][u]] = true;
      dfs(v[from][u], to);
    }
  }

  return false;
}

int main() {
  std::cin >> n >> m;

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

  int q_n;
  std::cin >> q_n;
  bool answers[10010];
  for(int i=0; i<q_n; ++i) {
    int from, to;
    std::cin >> from >> to;
    for(int j=0; j<n; ++j) visited[j] = false;

    bool connected = dfs(from, to);
    answers[i] = visited[to];
  }

  for(int i=0; i<q_n; ++i) {
    if(answers[i]) std::cout << "yes" << std::endl;
    else std::cout << "no" << std::endl;
  }

}

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

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

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    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  u, k, v,q;
	cin >> n >>v;

	for (int i = 0; i < v; i++) {
		cin >> u >> k;
		G[u].push_back(k);
		G[k].push_back(u);
	}
	assignColor();

	cin >> q;
	for (int i = 0; i < v; i++) {
		cin >> u >> k;
		if (color[u] == color[k]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	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,q,m;

  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<vector>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};

void dfs (int v, int marker){
	if(c[v]!=0){
		return;
	}
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		if (c[i]==0){
			dfs(i,marker);
			marker++;
		}
	}
	
	
	cin >> q;
	int d[2][q];
	for (int i=0;i<q;i++){
		cin >> d[i][0];
		cin >> d[i][1]; 
	}
	for (int i=0;i<q;i++){
		if(c[d[i][0]]==c[d[i][1]]){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}
	
	out(c)
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=1<<21;
const int maxn=100000+10;
int n,m,q;
int p[maxn];
int rank[maxn];
void init(int n)
{
   for(int i=0;i<n;i++)
   {
       p[i]=i;
       rank[i]=0;
   }
}
int fnd(int x)
{
    if(x==p[x]) return x;
    return fnd(p[x]);
}
bool is_same(int x,int y)
{
   return fnd(x)==fnd(y);
}

void uni(int x,int y)
{
   x=fnd(x);
   y=fnd(y);
   if(rank[x]>rank[y])
   {
       p[y]=x;
   }
   else
   {
       p[x]=y;
       if(rank[x]==rank[y]) rank[y]++;
   }
}

int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int x,y;
    init(n);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        uni(x,y);
    }
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&x,&y);
        if(is_same(x,y)) printf("yes\n");
        else printf("no\n");
    }


    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>

#define MAX 100000


using namespace std;


int gcol[MAX];

int main(void)
{
	int n;
	int m;
	int s;
	int t;
	int q;
	int type = 1;



	cin >> n >> m;

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


		if (gcol[s] != 0 && gcol[t] != 0) {
			for (int j = 0; j < n; j++) {
				if (gcol[s] == gcol[j]) {
					gcol[j] = gcol[s];
				}
			}
		}
		if (gcol[s] != 0) {
			gcol[t] = gcol[s];
		}
		else if (gcol[t] != 0) {
			gcol[s] = gcol[t];
		}
		else {
			gcol[s] = gcol[t] = type;
			type++;
		}


	}

	cin >> q;

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

		if (gcol[s] == gcol[t] && gcol[s] != 0) {
			cout << "yes";
		}
		else {
			cout << "no";
		}

		cout << endl;

	}

	return 0;
}