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 <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int SIZE = 100001;
int group[SIZE];

void assignGroup(int u, int g, std::vector<int> *a) {
  if (group[u] != 0) return;

  group[u] = g;
  // for (int i = 0; i < a[u].size(); ++i) {
  //   if (a[u][i] && group[i] == 0) assignGroup(i, g, a);
  // }
  for (auto v : a[u]) {
    // printf("u: %d, v: %d, group[u]: %d, group[v]: %d\n", u, v, group[u],
    //        group[v]);
    if (group[v] == 0) assignGroup(v, g, a);
  }
}

bool isConnected(int s, int t) { return group[s] == group[t]; }

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> a[n];

  rep(i, m) {
    int s, t;
    std::cin >> s >> t;
    a[s].push_back(t);
    a[t].push_back(s);
  }

  // std::cout << "/* message */" << '\n';
  // rep(i, n) {
  //   for (auto v : a[i]) std::cout << v << ' ';
  //   std::cout << '\n';
  // }
  // std::cout << "/* message */" << '\n';

  int g = 0;
  rep(i, n) {
    if (group[i] == 0) { assignGroup(i, ++g, a); }
  }

  // rep(i, n) std::cout << group[i] << ' ';

  int q;
  std::cin >> q;
  rep(i, q) {
    int s, t;
    std::cin >> s >> t;
    if (isConnected(s, t))
      std::cout << "yes" << '\n';
    else
      std::cout << "no" << '\n';
  }

  return 0;
}

C++: //
// Created by ?????°?????? on 2016/05/15.
//

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

const int MAX = (1 << 21);
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++: #include <iostream>
#include <vector>
#include <stack>

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n; //number of users
vector<int> G[MAX];
int color[MAX];

void dfs(int userid, int groupid) {
    stack<int> S;
    S.push(userid);
    color[userid] = groupid;
    while ( !S.empty() ) {
        int user = S.top(); S.pop();
        for (unsigned long i = 0; i < G[user].size(); i++) {
            int partner = G[user][i];
            //if partner is not member of a group -> join to user's group
            if (color[partner] == NIL) {
                color[partner] = groupid;
                //add partner to stack, then check partner's partner list (use while)
                S.push(partner);
            }
        }
    }
}

//create a group list named as color
//result is color = [1, 1, 1, 2, 2, 3, 3, 3, 3, 3]
void assingColor() {
    int id = 1;
    for (int i = 0; i < n; i++) color[i] = NIL; // create empty group list
    for (int u = 0; u < n; u++) {
        //u = user id, id = group id
        if (color[u] == NIL) dfs(u, id++); //fill group list with dfs
    }
    //for (int i = 0; i < n; i++) cout << color[i] << " ";
    //cout << endl;
}

int main()
{
    int m; //number of relationships
    int s, t; //user and partner id
    int q; //number of question
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t); //push user's partner id to user relationship list
        G[t].push_back(s); //push user id to user's partner relationship list
    }
    assingColor();

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

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

vector<vector<int> > g;
vector<int> ids;

void rec(int v, int id)
{
  //fprintf(stderr, "DEBUG: v=%d, id=%d\n", v, id);
  int n = g[v].size();
  ids[v] = id;
  for (int i = 0; i < n; ++i) {
    if (ids[g[v][i]] == -1) {
      rec(g[v][i], id);
    }
  }
}

int main()
{
  int n, m;
  fscanf(stdin, "%d %d", &n, &m);
  g = vector<vector<int> >(n);
  ids = vector<int>(n, -1);
  for (int i = 0; i < m; ++i) {
    int s, t;
    fscanf(stdin, "%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  int id = 0;
  for (int i = 0; i < n; ++i) {
    if (ids[i] == -1) {
      rec(i, id);
      ++id;
    }
  }
  int q;
  fscanf(stdin, "%d", &q);
  // fprintf(stderr, "DEBUG: ");
  // copy(ids.begin(), ids.end(), ostream_iterator<int>(cerr, ", "));
  // fprintf(stderr, "\n");
  for (int i = 0; i < q; ++i) {
    int s, t;
    fscanf(stdin, "%d %d", &s, &t);
    fprintf(stdout, "%s\n", ids[s] == ids[t] ? "yes" : "no");
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int color[N];
vector<int> G[N];

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

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

    memset(color, -1, sizeof(color));
    int c = 0;
    for (int u = 0; u < n; u++) {
        if (color[u] == -1) {
            dfs(u, c);
            c++;
        }
    }

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