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 <bits/stdc++.h>

using namespace std;
//int graph[100005][100005]; <-CompileError????????????????????????
vector<int> graph[100005];
int d[100005];
int color[100005];	
void bfs(int s, int n, int gpc){
	queue<int> Q;
	for(int i=0;i<n;i++){
		d[i] = 2e9;
	}
	color[s] = -1;
	d[s] = 0;
	Q.push(s);
	while(Q.size()!=0){
		int u = Q.front();
		Q.pop();
		for(int i=0;i<(int)graph[u].size();i++){
			int v = graph[u][i];
			if(color[v] == -2){
				color[v] = -1;
				d[v] = d[u] +1;
				Q.push(v);
			}
		}
		color[u] = gpc;
	}
}
void check(int n){
	int group = 0;
	for(int i=0;i<n;i++) color[i] = -2;
	for(int u=0;u<n;u++){
		if(color[u] == -2)bfs(u, n, group++);
	}
}

int main(){
	int n, m;
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int num;
		int tmp;
		cin >> num >> tmp;
		graph[num].push_back(tmp);
		graph[tmp].push_back(num);
	}
	check(n);
	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;
	}
}

C++: #include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//_____
#define pb push_back
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
//_______

vector <int> G[100005];
int group[100005];
int color = 1;

void dfs(int u)
{
   group[u] = color;

   rep(i, G[u].size())
      if( group[ G[u][i] ] == 0 )dfs( G[u][i] );
}

signed main()
{
   FAST();
   int N, M;
   cin >> N >> M;

   rep(i, M){
      int a,b;
      cin >> a >> b;
      G[a].pb(b);
      G[b].pb(a);
   }

   rep(i, N){
      if(group[i] == 0 )dfs(i);
      ++color;
   }

   int Q;
   cin >> Q;
   while(Q--){
      int a,b;
      cin >> a >> b;
      outl( group[a] == group[b] ? "yes" : "no" );
   }
}

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, 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 <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

struct Info{
	Info(){s = t = -1;}
	Info(int tmp_s,int tmp_t){
		s = tmp_s;
		t = tmp_t;
	}
	int s;
	int t;
};

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		global = new Info[m];
		each = new int*[n];
		each_index = new int[n];
		num_of_each = new int[n];
		for(int i=0;i<n;i++){
			each_index[i] = 0;
			num_of_each[i] = 0;
		}
		global_index = 0;
	}

	void push(int s,int t){
		global[global_index++] = Info(s,t);
		num_of_each[s]++;
		num_of_each[t]++;
	}

	void allocate(){
		for(int i=0;i<n;i++){
			if(num_of_each[i] > 0){
				each[i] = new int[num_of_each[i]];
			}
		}
		Info tmp;
		for(int i=0;i<m;i++){
			tmp = global[i];
			each[tmp.s][each_index[tmp.s]++] = tmp.t;
			each[tmp.t][each_index[tmp.t]++] = tmp.s;
		}
	}

	bool canAccsess(int s,int t){
		queue<int> Q;
		Q.push(s);
		int node,went[n];
		for(int i=0;i<n;i++)went[i]=0;
		while(!Q.empty()){
			node = Q.front();
			went[node] = 1;
			Q.pop();
			for(int i=0;i<num_of_each[node];i++){
				if(each[node][i] == t) return true;
				else if(went[each[node][i]] == 0){
					Q.push(each[node][i]);
				}
			}
		}
		return false;
	}

private:
	Info* global;
	int** each;
	int* each_index;
	int* num_of_each;
	int global_index,n,m;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.allocate();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}