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<stack>
#include<queue>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;
int color[100000];

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			//_getch();
			//cout << G[s][i] << endl;
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else if (color[i] == 0){
				Q.push(G[s][i]);
				color[i] = 1;
			}
		}
		if (!Q.empty())s = Q.front();
	}
}

int main(){
	int n, 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);
	}
	cin >> q; fill(color, color + 100000, 0);
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
		fill(color, color + 100000, 0);
	}

	return 0;
}

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;
}

int main(){
  int n,m,join[10000],s,t;
  vector<int> group[10000];
  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];

      if(x!=t || !group[x].empty()){
	group[x].push_back(s);
	join[s] = join[x];
      }

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

  }
 
  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++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D -- connected componenets
//method: check if their have the same component
//https://en.wikipedia.org/wiki/Connected_component_(graph_theory) -- connected component
//follow up : how cal the numbers of component--sorting the array and count
vector<int> neighbors[100005];
//why choose vector, not matrix
int root[100005];
//start is for the root
void dfs(int i, int start){
	root[i] = start;
	//visut all i's neighbors
	//c++ 11
	for(int j = 0; j< neighbors[i].size(); j++){
		int temp = neighbors[i].at(j);
		if(root[temp]==-1){
			dfs(temp,start);
		}
	}
}

int main(){
	int n,m,x,y,q;
	memset(root, -1, sizeof(root));
	scanf("%d %d",&n,&m);
	for(int i = 0; i<m; i++){
		scanf("%d %d",&x,&y);
		neighbors[x].push_back(y);
		neighbors[y].push_back(x);
	}
	for(int i = 0; i<n; i++){
		if(root[i]==-1){
			dfs(i,i);
		}
	}
	scanf("%d", &q);
	for(int i = 0; i<q; i++){
		scanf("%d %d",&x, &y);
		//printf("%d %d\n",root[x],root[y]);
		if(root[x] == root[y]){
			printf("yes\n");
		}else printf("no\n");
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_DEPRECATE
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int         N_MAX = 100000 + 2;
vector<int>       G[N_MAX];
int               n, m;
int               Q;
int               color[N_MAX];

void dfs(int v, int c) {
    color[v] = c;
    int size = G[v].size();
    for (int i = 0; i < size; i++) {
        int to = G[v][i];
        if (!color[to]) dfs(to, c);
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        memset(color, 0, sizeof(color));
        for (int i = 0; i < n; i++) {
            G[i].clear();
        }
        for (int i = 0; i < m; i++) {
            int from, to;
            scanf("%d%d", &from, &to);
            G[from].push_back(to);
            G[to].push_back(from);
        }
        int c = 1;
        for (int i = 0; i < n; i++) {
            if (!color[i]) {
                dfs(i, c);
                c++;
            }
        }

        scanf("%d", &Q);
        for (int i = 0; i < Q; i++) {
            int from, to;
            scanf("%d%d", &from, &to);
            if (color[from] == color[to])
                puts("yes");
            else
                puts("no");
        }
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 100005

using namespace std;
int n, m, q;
vector<int> A[Lens];
queue<int>	Q;
char c[Lens], d[Lens];

int Find(int s, int t)
{
	Q.push(s);
	c[s] = 'g';
	while (!Q.empty())
	{
		s = Q.front();
		Q.pop();
		c[s] = 'g';
		for (int j = 0; j < A[s].size(); j++)
		{
			if (c[A[s][j]] == 'w')
				Q.push(A[s][j]);
			if (A[s][j] == t)
			{
				return 1;
			}
		}
	}
	return 0;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		d[s] = d[t] = 'w';
		A[s].push_back(t);
		A[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		while (!Q.empty())
			Q.pop();
		for (int j = 0; j < n; j++)
			c[j] = d[j];
		int s, t;
		cin >> s >> t;
		if (Find(s, t))
			cout << "yes" << endl;

		else
			cout << "no" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#define MAX 100001
using namespace std;
vector<int> v[MAX];
int color[MAX];

void dfs(int x,int value){
	stack<int> s;
	s.push(x);
	color[x] = value;
	while(!s.empty()){
		int u = s.top();
		s.pop();
		for(int i = 0;i < v[u].size();i++){
			int a = v[u][i];
			if(color[a] == -1){
				color[a] = value;
				s.push(a);
			}
		}
	}
}

int main(){
	int n,m,q,x,y,value;
	cin >> n >> m;
	for(int i = 0;i < m;i++){
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}

	value = 1;
	for(int i = 0;i < n;i++){
		color[i] = -1;
	}
	for(int i = 0;i < n;i++){
		if(color[i] == -1)dfs(i,value++);
	}

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