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++: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20180319_AOJ
{
    class ALDS1_11D
    {
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int[] n_m = line.Split(' ').Select(int.Parse).ToArray();
                int n = n_m[0];
                int m = n_m[1];
                int[,] matrix = new int[n, n];

                for (int i = 0; i < m; i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    matrix[s_t[0], s_t[1]] = 1;
                    matrix[s_t[1], s_t[0]] = 1;
                }

                int[] group = new int[n];
                int gn = 0;
                for (int i = 0; i < n; i++)
                {
                    if (group[i] == 0)
                    {
                        gn++;
                        group[i] = gn;
                    }

                    gn = group[i];
                    for (int t = 0; t < n; t++)
                    {
                        if (matrix[i, t] == 1)
                        {
                            int tn = group[t];
                            if (tn > 0)
                            {
                                Replace(group, tn, gn);
                            }
                            else
                            {
                                group[t] = gn;
                            }
                        }
                    }
                }

                int q = int.Parse(Console.ReadLine());
                for (int i = 0; i < q; i++)
                {
                    int[] s_t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    if (group[s_t[0]] == group[s_t[1]])
                    {
                        Console.WriteLine("yes");
                    }
                    else
                    {
                        Console.WriteLine("no");
                    }
                }
            }
        }

        static void Replace<T>(T[] arr, T source, T dest)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].Equals(source))
                {
                    arr[i] = dest;
                }
            }
        }
    }
}

C++: /* ALDS1_11_D: Connected Components */

#include <iostream>
#include <vector>
using std::vector;

bool is_connectable(vector<int> sns[], int id, int goal_id) {
    vector<int> friends = sns[id];
    for (int i = 0; i < friends.size(); i++) {
        int friend_id = friends[i];
        if (friend_id == goal_id || is_connectable(sns, friend_id, goal_id))
            return true;
    }
    return false;
}

int main() {
    std::ios::sync_with_stdio(false);

    int n_users, n_relations;
    std::cin >> n_users >> n_relations;

    int id, friend_id, n_friends;
    vector<int> *sns = new vector<int>[n_users];
    for (int i = 0; i < n_relations; i++) {
        std::cin >> id >> friend_id;
        sns[id].push_back(friend_id);
    }

    int n_questions;
    std::cin >> n_questions;

    for (int i = 0; i < n_questions; i++) {
        std::cin >> id >> friend_id;
        bool connectable = is_connectable(sns, id, friend_id);
        std::cout << ( connectable ? "yes" : "no" ) << "\n";
    }
    return 0;
}

C++: #include <iostream>
#include <stack>
#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++: #include <cstdio>
#include <algorithm>
#define rep(i, n) for(int i=0; i<(n); ++i)

int parent[100005];

int find(int x){
   if( parent[x] == x )return x;
   return parent[x] = find( parent[x] );
}

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

   if(x == y)return;
   parent[x] = y;
}

int main()
{
   int N, M;
   int x, y, tmp;
   scanf("%d%d", &N, &M);
   std::iota(parent, parent + N, 0);

   rep(i, M){
      scanf("%d%d", &x, &y);
      unite(x, y);
   }

   int Q;
   scanf("%d", &Q);
   while(Q--){
      scanf("%d%d", &x, &y);
      puts( find(x) == find(y) ? "yes" : "no" );
   }
}

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

const int maxm = 100000 + 5;
const int maxn = 100000 + 5;

struct Edge{
	int to,next;
}G[maxm<<1];

int head[maxn],cnt = 0;
int color[maxn];
bool vis[maxn];

void Insert(int u,int v){
	G[++cnt].next = head[u];
	G[cnt].to = v;
	head[u] = cnt;
}

void Dfs(int u,int c){
	vis[u] = true;
	color[u] = c;
	for(int e = head[u];e; e = G[e].next){
		int v = G[e].to;
		if(!vis[v]){
			Dfs(v,c);
		}
	}
}

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= m; i++){
		int u,v;
		scanf("%d%d",&u,&v);
		Insert(u,v);
		Insert(v,u);
	}
	for(int i = 1;i <= n; i++){
		if(!vis[i]){
			Dfs(i,i);	
		}
	}
	int q;
	scanf("%d",&q);
	for(int i = 1;i <= q; i++){
		int u,v;
		scanf("%d%d",&u,&v);
		if(color[u] == color[v])printf("yes\n");
		else printf("no\n");
	}
}