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<cstring>
#include<algorithm>
#include<cstdio>
#include<stack>
using namespace std;
const int MAXN = 100000 + 7;
const int NIL = -1;

vector<int> G[MAXN];
int status[MAXN];
int N;

void dfs( int  r, int c )
{
    stack<int> S;
    S.push(r);
    status[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 ( status[v] == NIL ) {
                status[v] = c;
                S.push(v);
            }
        }
    }
}

void set_status()
{
    int id = 1;
    memset( status, -1, sizeof(status) );
    for ( int u = 0; u < N; u++ ) {
        if ( status[u] == NIL )
            dfs( u, id++ );
    }
}

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

    set_status();

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

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;

struct UnionFind
{
	// par[i]????????????i????±?????????¨??????????????????i == par[i]?????¨???????????????i?????¨?????????????????§??????
	// sizes[i]???????????????i?????¨?????????????????????????????°???i?????????????????§????????´???????????????????????¨??????
	vector<int> par;
	vector<int> sizes;

	UnionFind(int n) : par(n), sizes(n, 1) {
		rep(i, n) par[i] = i;
	}

	int find(int x) {
		if (x == par[x]) return x;
		return par[x] = find(par[x]); // ????????????????????????????????°???????????????????????¢???
	}

	void unite(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) return;

		// x?????¨???y?????¨????????§???????????????????????????
		if (sizes[x] < sizes[y]) swap(x, y);

		par[y] = x; // ??????????????????????????°??????
		sizes[x] += sizes[y];
		sizes[y] = 0;  // sizes[y]????????????????????¨???????????§0?????\????????????????????????
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}

	int size(int x) {
		return sizes[find(x)];
	}
};

int N, M, Q;

int main() {

	cin >> N >> M;
	UnionFind uf(N);
	rep(i, M){
		int f1, f2; cin >> f1 >> f2;
		uf.unite(f1, f2);
	}
	cin >> Q;
	rep(i, Q)
	{
		int f1, f2; cin >> f1 >> f2;
		cout << (uf.same(f1, f2) ? "yes" : "no") << endl;
	}

}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty())
        que.pop();
    
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

    for(int i=0; i<m; i++){
        cin >> s >> t;

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);

		if(x==t){
		  answer[i]=true;
		  break;
		}
            }
         }

          que.pop();
        }

        que=refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}

C++: /*
 * Problem Description: Connected Components
 * Category: Graph I
 * Author: Khan
 * Date: 21th October, 2017
 */

#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 i = 0; i < n; ++i)
        if(color[i] == NIL)
            DFS(i, id++);
}

int main(int argc, char const *argv[])
{
    //freopen("sample.txt", "r", stdin);
    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;
}