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;

struct Unionfind {
	vector<int> data;

	Unionfind(int n) { data.assign(n, -1); }

	int root(int n) {
		if (data[n] < 0) return (n);
		return (data[n] = root(data[n]));
	}

	int size(int n) { return (-data[root(n)]); };
	bool same(int x, int y) { return (root(x) == root(y)); }

	void unite(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) return;
		if (data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
	}
};

int main() {
    int N, M; cin >> N >> M;
    Unionfind uni(N);
    for (int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        uni.unite(a, b);
    }
    int Q; cin >> Q;
    while (Q--) {
        int a, b; cin >> a >> b;
        cout << (uni.same(a, b) ? "yes" : "no") << '\n';
    }
    return (0);
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std; 

int noc;
vector<int> comp;
vector< vector<int> > adj;

void dfs(int v) {
	comp[v] = noc;
	for (int i = 0; i < adj[v].size(); ++i) {
		if (comp[adj[v][i]] < 0) {
			dfs(adj[v][i]);
		}
	}
	return;
}

int main(void) {
	int nov,noe,u,v;
	cin >> nov >> noe;
	adj = vector< vector<int> >(nov);
	for (int i = 0; i < noe; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	noc = 0;
	comp = vector<int>(nov,-1);
	for (int i = 0; i < nov; ++i) {
		if (comp[i] < 0) {
			++noc;
			dfs(i);
		}
	}

	int noq;
	cin >> noq;
	for (int q = 0; q < noq; ++q) {
		cin >> u >> v;
		if (comp[u] == comp[v]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100000;

class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(){}
    UnionFind(const int node_size) : par(node_size){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    bool unite(int x, int y){
        int z;
        while(par[x] != par[y]){
            if(par[x] > par[y]) swap(x, y);
            z = par[x], par[x] = par[y];
            if(y == z) return false;
            x = z;
        }
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int main()
{
    int n,m,q;
    int a,b;
    cin >> n >> m;
    UnionFind uf(n);
    for(int i=0;i<m;i++){
        cin >> a >> b;
        uf.unite(a,b);
    }
    cin >> q;
    for(int i=0;i<q;i++){
        cin >> a >> b;
        if(uf.same(a,b)){
            cout << "yes\n";
        }else{
            cout << "no\n";
        }
    }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 100000

struct Node{
	int group;
	queue<int> fr;
};

int N, M, Q, group_num = 0;
Node person[MAX];

void isFriend(int a, int b){
	if(person[a].group < 0 && person[b].group < 0){
		int p = a, next, m;
		queue<int> move;
		group_num++;
		person[p].group = group_num;
		//cout << a << " is group " << person[a].group << endl;
		move.push(p);

		while(!move.empty()){
			p = move.front();
			if(person[p].fr.empty()){
				move.pop();
			} else {
				m = person[p].fr.size();
				while(m--){
					next = person[p].fr.front();
					person[p].fr.pop();
					if(person[next].group < 0){
						person[next].group = group_num;
						//cout << next << " is group " << person[next].group << endl;
						move.push(next);
					}
				}
				move.pop();
			}
		}

		if(person[a].group == person[b].group){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	} else if(person[a].group == person[b].group){
		cout << "yes" << endl;
	} else {
		cout << "no" << endl;
	}
}

int main(){
	cin >> N >> M;
	for(int i = 0; i < N; i++)
		person[i].group = -1;
	while(M--){
		int a, b;
		cin >> a >> b;
		person[a].fr.push(b);
		person[b].fr.push(a);
	}
	cin >> Q;
	while(Q--){
		int a, b;
		cin >> a >> b;
		isFriend(a, b);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

// const int N = 100000;
vector<vector<int>> A;
vector<bool> visited;
// int n;

bool dfs(int s, int t) {
    stack<int> S;
    S.push(s);
    visited[s] = true;

    while (!S.empty()) {
        auto j = S.top();
        S.pop();

        auto& v = A[j];
        for (auto it = v.begin(); it != v.end(); ++it) {
            auto i = *it;
            if (i == t) {
                return true;
            }
            if (!visited[i]) {
                visited[j] = true;
                S.push(i);
            }
        }
    }
    return false;
}

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

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<list>
#define MAX 100000
using namespace std;

class person
{
public:
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		while (!lis.empty())
		{
			person* temp = lis.front();
			lis.pop_front();
			if (temp->Isdone == true)continue;
			if (temp == Per)return true;
			else
			{
				if (temp->serch(Per))return true;
			}
		}
		return false;
	}
};

int main()
{
	person *P;
	P = new person[MAX + 1];
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	delete[] P;
	return 0;
}