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 <cassert>
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>

class TVertex
{
    int FIndex;
    int FCommuniyId;
    std::list<TVertex*> FList;

public:
    TVertex(int Index)
     : FIndex(Index), FCommuniyId(0)
    {}
    void AddPath(TVertex* Vertex)
    {
        FList.push_back(Vertex);
    }
    void SetCommunity(int CommunityId)
    {
        if(FCommuniyId) return;
        FCommuniyId = CommunityId;
        for(std::list<TVertex*>::iterator It = FList.begin(); It != FList.end(); ++It){
            TVertex* V = *It;
            V->SetCommunity(CommunityId);
        }
    }
    int CommunityId()const
    {
        return FCommuniyId;
    }
};
class TGraph
{
    std::vector<TVertex*> FTable;
    void Init(int n)
    {
        assert(n > 0);
        FTable.resize(n);
        for(int i = 0; i < n; ++i){
            FTable[i] = new TVertex(i);
        }
    }
public:
    TGraph(int n)
    {
        Init(n);
    }
    void MakeFriend(int s, int t)
    {
        assert(s < FTable.size());
        assert(t < FTable.size());
        FTable[s]->AddPath(FTable[t]);
        FTable[t]->AddPath(FTable[s]);
    }
    void SetCommunity()
    {
        for(std::size_t i = 0; i < FTable.size(); ++i){
            FTable[i]->SetCommunity(i+1);
        }
    }
    bool IsSameCommunity(int s, int t)const
    {
        assert(s < FTable.size());
        assert(t < FTable.size());
        return FTable[s]->CommunityId() == FTable[t]->CommunityId();
    }
};

int main()
{
    int n, m;
    std::cin >> n >> m;
    TGraph Graph(n);
    for(int i = 0; i < m; ++i){
        int s, t;
        std::cin >> s >> t;
        Graph.MakeFriend(s, t);
    }
    int q;
    std::cin >> q;
    Graph.SetCommunity();
    for(int i = 0; i < q; ++i){
        int s, t;
        std::cin >> s >> t;
        std::cout << (Graph.IsSameCommunity(s, t) ? "yes" : "no") << std::endl;
    }
    return 0;
    
}

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

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m, q;
    cin >> n >> m;

    vvi g(n);

    rep(i, 0, m) {
        int s, t;
        cin >> s >> t;

        g[s].push_back(t);
        g[t].push_back(s);
    }

    vi map(n, -1);
    vector<bool> seen(n, false);

    int t = 0;
    rep(i, 0, n) {
        if (seen[i]) { continue; }
        queue<int> st;
        st.push(i);
        seen[i] = true;
        map[i] = t;

        while (!st.empty()) {
            int v = st.front();
            st.pop();

            for (auto itr = g[v].begin(); itr != g[v].end(); ++itr) {
                if (!seen[*itr]) {
                    st.push(*itr);
                    seen[*itr] = true;
                    map[*itr] = t;
                }
            }
        }
        ++t;
    }

    cin >> q;
    rep(i, 0, q) {
        int s, t;
        cin >> s >> t;
        cout << (map[s] == map[t] ? "yes" : "no") << endl;
    }

    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>

class TUser
{
	std::list<TUser*> FAccessible;
public:
	void AddFriend(TUser* Friend);
	bool IsAccesible(const TUser* Target)const;
};

void TUser::AddFriend(TUser* Friend)
{
	if(IsAccesible(Friend)) return;
	FAccessible.push_back(Friend);
	for(std::list<TUser*>::iterator It = FAccessible.begin(); It != FAccessible.end(); ++It){
		(*It)->AddFriend(Friend);
		Friend->AddFriend(*It);
	}
}

bool TUser::IsAccesible(const TUser* Target)const
{
	return std::find(FAccessible.begin(), FAccessible.end(), Target) != FAccessible.end();
}

class TSns
{
	std::vector<TUser> FUsers;
public:
	TSns(int UserCount);
	
	void AddLink(int UserA, int UserB);
	std::string IsAccesible(int UserA, int UserB)const;
};

TSns::TSns(int UserCount)
: FUsers(UserCount)
{
}

void TSns::AddLink(int UserA, int UserB)
{
	FUsers[UserA].AddFriend(&FUsers[UserB]);
	FUsers[UserB].AddFriend(&FUsers[UserA]);
}

std::string TSns::IsAccesible(int UserA, int UserB)const
{
	if(FUsers[UserA].IsAccesible(&FUsers[UserB]) || FUsers[UserB].IsAccesible(&FUsers[UserA])) return "yes";
	return "no";
}

int main()
{
	int UserCount;
	std::cin >> UserCount;
	TSns Sns(UserCount);
	
	int LinkCount;
	std::cin >> LinkCount;
	for(int i = 0; i < LinkCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		Sns.AddLink(UserA, UserB);
	}
	
	int QuestionCount;
	std::cin >> QuestionCount;
	for(int i = 0; i < QuestionCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		std::cout << Sns.IsAccesible(UserA, UserB) << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int N;
vector<int> G[100000];
int color[100000];

void dfs(int s, int c){
	stack<int> S;
	S.push(s);
	color[s] = c;
	while(!S.empty()){
		int u = S.top();
		S.pop();
		rep(i, G[u].size()){
			int p = G[u][i];
			if(color[p] == 0){
				color[p] = c;
				S.push(p);
			}
		}
	}
}

void colorAssign(){
	rep(i, N){
		color[i] = 0;
	}
	int id = 1;
	rep(i, N){
		if(color[i] == 0){
			dfs(i, id);
			id++;
		}
	}

}

int main(){
	int m, s, t;
	cin >> N >> m;
	rep(i, m){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	colorAssign();
	int q;
	cin >> q;
	rep(i, q){
		cin >> s >> t;
		if(color[s] == color[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}