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<cstdio>
#include<queue>
#include<vector>
using namespace std;

vector<int> ad[100000];
vector<int> v,d;

int main()
{
	int i,j,n,m,a,b,q,l,k;
	queue<int> Q;
	
	cin >> n;
	cin >> m;
	for(i=0;i<=m-1;i++)
	{
		cin >> a;
		cin >> b;
		ad[a].push_back(b);
		ad[b].push_back(a);
	}
	for(i=0;i<=n-1;i++) 
	{
		ad[i].push_back(-1);
		v.push_back(0);
		d.push_back(-1);
	}
	j=0;
	for(i=0;i<=n-1;i++)
	{
		if(v[i]==0)
		{
			Q.push(i);
			v[i]=1;
			d[i]=j;
			while(Q.empty()!=1)
			{
				l=Q.front();
				for(k=0;ad[l][k]!=-1;k++)
				{
					if(v[ad[l][k]]==0)
					{
						Q.push(ad[l][k]);
						v[ad[l][k]]=1;
						d[ad[l][k]]=j;
					}
				}
				Q.pop();
			}
			j++;
		}
	}
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		if(d[a]==d[b]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: /*
 * ALDS1_11_D.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

#include<iostream>
#include<vector>

using namespace std;

bool visited[100000];

void dfsRec(vector<int>* a, int v) {
	if(visited[v]) return;
	visited[v] = 1;
	for(int &x:a[v]) {
		dfsRec(a, x);
	}
}

int main() {
	int n, m;
	vector<int> adj[100000];

	cin >> n >> m;
	int u, v;
	for(int i=0; i<m; i++) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	int q;
	cin >> q;
	while(q--) {
		for(int i=0; i<n; i++) {
			visited[i] = 0;
		}
		cin >> u >> v;
		dfsRec(adj, u);
		(visited[v]) ? printf("yes\n") : printf("no\n");
	}
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

vector<int> SNS[100000];
int Group[100000] = { 0 };

int main()
{
	int n, m, q, s, t;
	int man1, man2;
	int grNo = 1;
	int current, next;
	queue<int> Q;
	vector<int>::iterator it;
	
	cin >> n >> m;
	rep(i, m) {
		cin >> man1 >> man2;
		SNS[man1].push_back(man2);
		SNS[man2].push_back(man1);
	}

	rep(i, n) {
		if (Group[i] == 0) {
			Q.push(i);
			Group[i] = grNo;

			while (!Q.empty()) {
				current = Q.front();

				for (it = SNS[current].begin(); it != SNS[current].end(); it++) {
					next = *it;
					if (Group[next] == 0) {
						Q.push(next);
						Group[next] = grNo;
					}
				}

				Q.pop();
			}

			grNo++;
		}
	}

	cin >> q;
	rep(i, q) {
		cin >> s >> t;
		if (Group[s] == Group[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include <stdio.h>
int ancestor[10];
int find(int x){
    if(ancestor[x]==x)return x;
    return ancestor[x]=find(ancestor[x]);
}
int min(int a,int b){
    if(a<b)return a;
    return b;
}
int max(int a,int b){
    if(a>b)return a;
    return b;
}
int main(){
    int n,q,m,temp,x,y;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)ancestor[i]=i;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        temp=find(x);
        q=find(y);
        ancestor[max(temp,q)]=min(temp,q);
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d%d",&x,&y);
        if(find(x)!=find(y))printf("no\n");
        else printf("yes\n");
    }
}

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct Vertex {
    vector<ll> node;
};

Vertex V[100005];
bool U[100005];
ll Parent[100005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    REP(_, M) {
        ll a, b;
        cin >> a >> b;
        V[a].node.push_back(b);
        V[b].node.push_back(a);
    }

    REP(i, N) {
        Parent[i] = i;
    }
    memset(U, 0, sizeof(U));

    REP(i, N) {
        if (U[i])
            continue;
        queue<ll> q;
        q.push(i);
        U[i] = true;
        while (!q.empty()) {
            ll t = q.front();
            q.pop();
            for (auto &n:V[t].node) {
                if (U[n])
                    continue;
                Parent[n] = i;
                q.push(n);
                U[n] = true;
            }
        }
    }

    ll Q;
    cin >> Q;
    REP(_, Q) {
        ll s, t;
        cin >> s >> t;
        if (Parent[s] == Parent[t])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;
#define MAX 100005
#define NIL -1

vector<int> V[MAX];
int color[MAX];


void search(int v, int c) {
	if (color[v] != NIL)return;
	color[v] = c;
	for (int i = 0; i < V[v].size(); i++) {
		search(V[v][i], c);
	}
}

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

	int id = 1;
	for (int i = 0; i < n; i++)color[i] = NIL;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)search(u, id++);
	}

	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (color[s] == color[t])cout << "yes\n";
		else cout << "no\n";
	}

	return 0;
}