Go: // AOJ ALDS1_11_D: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D

package main

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

var nocolor = -1

type graph struct {
	graph  [][]int
	colors []int
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func newGraph(nodeNum int) *graph {
	colors := make([]int, nodeNum)
	g := make([][]int, nodeNum)
	for i := 0; i < nodeNum; i++ {
		g[i] = []int{}
		colors[i] = nocolor
	}

	return &graph{g, colors}
}

func constructGraph(sc *bufio.Scanner) *graph {
	usersNum := nextInt(sc)
	linkNum := nextInt(sc)

	g := newGraph(usersNum)
	for i := 0; i < linkNum; i++ {
		u := nextInt(sc) // node
		l := nextInt(sc) // link node
		// edge each other
		g.graph[u] = append(g.graph[u], l)
		g.graph[l] = append(g.graph[l], u)
	}
	return g
}

func (g *graph) dfs(r, color int) {
	g.colors[r] = color

	stack := list.New()
	stack.PushFront(r)

	for stack.Len() > 0 {
		v := stack.Remove(stack.Front()).(int)
		for _, u := range g.graph[v] {
			if g.colors[u] == nocolor {
				g.colors[u] = color
				stack.PushFront(u)
			}
		}
	}
}

func (g *graph) assignColor() {
	color := 1
	for u := 0; u < len(g.graph); u++ {
		if g.colors[u] == nocolor {
			g.dfs(u, color)
			color++
		}
	}
}

func (g *graph) isLink(s, t int) bool {
	return g.colors[s] == g.colors[t]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// construct graph and friendly colors
	g := constructGraph(sc)
	g.assignColor()

	// judge link
	q := nextInt(sc)
	for i := 0; i < q; i++ {
		s := nextInt(sc)
		t := nextInt(sc)

		if g.isLink(s, t) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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

int *P;

int root(int a)
{
    if(P[a] == a) return a;
    P[a] = root(P[a]);
    return P[a]; 
}

void join(int a, int b){ int x = root(a), y = root(b); P[x] = y; }

bool same(int a, int b){ return (root(a) == root(b) ? true : false); }

int main()
{
    int i, n, m, a, b, q;
    scanf("%d %d", &n, &m);
    P = new int [n];
    for(i = 0; i < n; i++) P[i] = i;

    for(i = 0; i < m; i++){ scanf("%d %d", &a, &b); join(a, b); }

    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(same(a, b)){ printf("yes\n"); }else{ printf("no\n"); }
    }
    delete [] P;
    return 0; 
}

C++: #include <bits/stdc++.h>
#define MAX 1000001
//#define EZPZ_LEMON_SQUEEZY
using namespace std;
vector<int> M[MAX];
int n, e, q;
bool vis[MAX];
int color[MAX];
void dfs(int index, int cor)
{
	vis[index] = 1;
	color[index] = cor;
	for (int i = 0; i < M[index].size(); i++) {
		if (!vis[M[index][i]]) {
			dfs(M[index][i], cor);
		}
	}
}

int main()
{
#ifdef EZPZ_LEMON_SQUEEZY
	freopen("D:\\out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	int s, t;
	cin >> n >> e;
	for (int i = 0; i < e; i++) {
		cin >> s >> t;
		M[s].push_back(t);
		M[t].push_back(s);
	}
	memset(vis, 0, sizeof(vis));
	memset(color, 0, sizeof(color));
	int c = 1;
	for (int i = 0; i < n; i++) {
		if (!vis[i]) {
			dfs(i, c);
			c++;
		}
	}

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

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
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++: // Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <vector>
#include <stack>

using namespace std;
static const int NMAX = 100000;
static const int NIL = -1;

int n, group[NMAX];
vector<int> G[NMAX];

void depthFirstSearch(int ix, int id) {
	int y;
	stack<int> S;
	S.push(ix);
	while (!S.empty()) {
		ix = S.top(); S.pop();
		for (unsigned int j = 0; j < G[ix].size(); j++) {
			y = G[ix][j];
			if (group[y] == NIL) {
				group[y] = id;
				S.push(y);
			}
		}
	}
}

void classifyID() {
	int id = 0;
	for (int i = 0; i < n; i++) {
		if (group[i] == NIL) {
			depthFirstSearch(i, id);
			id++;
		}
	}
}

int main(void) {
	int m, x, y;

	cin >> n >> m;
	for (int i = 0; i < n; i++) group[i] = NIL; // initialize group list
	for (int i = 0; i < m; i++) { // create graph in linked list representation
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	classifyID();

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> x >> y;
		if ((group[x] != NIL) && (group[y] != NIL) && (group[x] == group[y]))
			cout << "yes";
		else
			cout << "no";
		cout << endl;
	}

	return 0;
}

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

int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
	d[i]=i;
}
for(int l=0;l<m;l++){
	int a,b;
	cin>>a>>b;
	if(d[a]!=d[b]){
		int k=d[b];
		for(int h=0;h<n;h++){
			if(d[h]==k){
				d[h]=d[a];
			}
		}
		
		
	}
}
cin>>q;
for(int y=0;y<q;y++){
	int a,b;
	cin>>a>>b;
	if(d[a]==d[b]){
		cout<<"yes"<<endl;
	}
	else{
		cout<<"no"<<endl;
	}
}
	return 0;
}

C++: #include <iostream>
#include <vector>
#define N 100000
#define white 0

int color[N], n, S[N];
std::vector<int> c[N];



void tansaku_2(int a, int b) {
    int i, t, co, u;
    S[0] = a;
    t = 0;
    co = 1;
    while (1) {
        u = S[t];
        for (i = 0; i < c[u].size(); ++i) {
            if (color[c[u][i]] == white){
                color[c[u][i]] = b;
                S[co++] = c[u][i];
            }
        }
        S[t++] = 0;
        if (t == co) break;
    }
}

void tansaku() {
    int i, d;
    d = 1;
    
    for (i = 0; i < n; ++i) {
        if (color[i] == white) {
            color[i] = d;
            tansaku_2(i , d++);
        }
    }
}

int main() {
    int m, q, i, a, b;
    
    scanf("%d %d", &n, &m);
    for (i = 0; i < n; ++i) {
        color[i] = white;
    }
    for (i = 0; i < m; ++i) {
        scanf("%d %d", &a, &b);
        c[a].push_back(b);
        c[b].push_back(a);
    }
    tansaku();
    
    scanf("%d", &q);
    for (i = 0; i < q; ++i) {
        scanf("%d %d", &a, &b);
        if (color[a] == color[b]) {
            printf("yes\n");
        }
        else{
            printf("no\n");
        }
    }
    return 0;
}