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>
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9;
int idx[MAX]={0};
void init(){
	int i;
	for(i=0;i<MAX;i++)
		idx[i]=i;
}
int findroot(int i){
	while(i!=idx[i])
		i=idx[i];
	return i;
}
void munion(int u ,int v){
	u=findroot(u);
	v=findroot(v);
	if(u!=v)
		idx[v]=u;
}
int main(){
	int i ,j ,n ,m;
	cin>>n>>m;
	init();
	while(m--){
		int x ,y;
		cin>>x>>y;
		munion(x ,y);
	}
	int q;
	cin>>q;
	while(q--){
		int  u ,v;
		cin>>u>>v;
		u=findroot(u);
		v=findroot(v);
		if(u==v)
			cout<<"yes"<<"\n";
		else
			cout<<"no"<<"\n";
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5
#define NIL -1
int flag = 0;
vector<int> G[MAX];
queue<int> qu;
int vis[MAX];
int n;
int ind, tmp;

void bfs(int u,int k) {
	qu.push(u);
	vis[u] = k;
	while (not qu.empty()) {
		int u = qu.front(); qu.pop();
		for (int j = 0; j < G[u].size(); j++) {
			int v = G[u][j];
			if (vis[v] == NIL) {
				vis[v] = k;
				qu.push(v);
			}
		}
	}
}

void calc() {
	int id = 1;
	for (int i = 0; i < n; i++) vis[i] = NIL;
	for (int i = 0; i < n; i++) {
		if (vis[i] == NIL) bfs(i, id++);
	}
}

int main() {
	int m, q;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
		G[tmp].push_back(ind);
	}
	calc();
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		if (vis[ind] == vis[tmp])
		cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

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 m, s, t;

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  int q;
  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 <map>
#include <stack>
using namespace std;

#define null -1

void DFS(vector<int> rVal[], map<int, int> uColor_m, int idOrSs) {
	stack<int> stack;
	stack.push(idOrSs);
	uColor_m[idOrSs] = idOrSs;

	while (!stack.empty()) {
		int curUVal = stack.top();
		stack.pop();
		for (int i = 0; i < (signed)rVal[idOrSs].size(); i++) {
			int curV = rVal[curUVal][i];
			if (uColor_m[curV] != null) continue;
			uColor_m[curV] = idOrSs;
			stack.push(curV);
		}
	}
}

void assignColor(int uNum, vector<int> rVal[], map<int, int> uColor_m) {
	for (int i = 0; i < uNum; i++)
		uColor_m[i] = null;
	for (int i = 0; i < uNum; i++) {
		if (uColor_m[i] == null) DFS(rVal, uColor_m, i);
	}
}

int main() {
	int userNum;
	int relationNum;
	map<int, int> uColor_map; //<?????????, ??°??????????????????>
	//??????????????¨????????????
	vector<int> relationVal[100000];
	//int userColor[100000];

	cin >> userNum >> relationNum;
	for (int i = 0; i < relationNum; i++) {
		int userVal1, userVal2;
		cin >> userVal1 >> userVal2;
		relationVal[userVal1].push_back(userVal2);
		relationVal[userVal2].push_back(userVal1);
		uColor_map[i] = null;
	}

	assignColor(userNum, relationVal, uColor_map);

	int qNum;
	cin >> qNum;
	for (int i = 0; i < qNum; i++) {
		int uV1, uV2;
		cin >> uV1 >> uV2;
		if (uColor_map[uV1] == uColor_map[uV2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

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

#define N 100005
#define UNDEFINED -1

int n, m, s, t, q;
std::vector<int> adj[N];
int group[N];

void dfs(int x, int g) {
    std::stack<int> S;
    S.push(x);
    group[x] = g;
    while(!S.empty()) {
        int u = S.top(); S.pop();
        for(int i = 0; i < adj[u].size(); i++) {
            int v = adj[u][i];
            if(group[v] == UNDEFINED) {
                group[v] = g;
                S.push(v);
            }
        }
    }
}

int main() {
    for(int i = 0; i < N; i++) {
        group[i] = UNDEFINED;
    }

    std::cin >> n >> m;
    for(int i = 0; i < m; i++) {
        std::cin >> s >> t;
        adj[s].push_back(t);
        adj[t].push_back(s);
    }

    int g = 0;
    for(int i = 0; i < n; i++) {
        if(group[i] == UNDEFINED) {
            dfs(i, g++);
        }
    }


    std::cin >> q;
    for(int i = 0; i < q; i++) {
        std::cin >> s >> t;
        if(group[s] != UNDEFINED && group[t] != UNDEFINED && group[s] == group[t]) {
            puts("yes");
        } else {
            puts("no");
        }
    }
    return 0;
}