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<vector>
#include<stack>
#include<queue>
#include<algorithm>

struct node {
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<std::vector<int> > nn(n.size());
	std::vector<int> d(n.size()), dd(n.size());
	int count = 0;
	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		nn[count].push_back(i);
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] != 1) {
					nn[count].push_back(*it);
					s.push(*it);
					d[*it] = 1;
					//std::vector<int>::iterator itt= std::find(n[*it].relation.begin(), n[*it].relation.end(), num);
					//if (itt != n[*it].relation.end()) {
					//	n[*it].relation.erase(itt);
					//}
				}
			}
		}
		count++;
	}

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		for (int j = 0; j < count; j++) {
			if (std::find(nn[j].begin(), nn[j].end(), from) != nn[j].end() &&
				std::find(nn[j].begin(), nn[j].end(), to) != nn[j].end()) {
				std::cout << "yes" << std::endl;
				break;
			}
			if (j == count - 1) {
				std::cout << "no" << std::endl;
			}
		}
	}
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }


struct UnionFind { // UnionFindTree
  vector<int> d;
  UnionFind(int n): d(n,-1) {}
  int find(int x) {
    if(d[x] < 0) return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return false;
    if(-d[x] < -d[y]) swap(x,y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};


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

C++: #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 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 <iostream>
#include <vector>
#define N 100000
#define white 0
using namespace std;

int color[N], n, S[N];
vector<int> c[N];



void tansaku_2(int a, int b) {
    int i, t, co, u;
    S[0] = a;
    t = 0;
    co = 1;
    while (1) {
        u = S[t];
        for (i = 0; i < c[u].size(); ++i) {
            if (color[c[u][i]] == white){
                color[c[u][i]] = b;
                S[co++] = c[u][i];
            }
        }
        S[t++] = 0;
        if (t == co) break;
    }
}

void tansaku() {
    int i, d;
    d = 1;
    
    for (i = 0; i < n; ++i) {
        if (color[i] == white) {
            color[i] = d;
            tansaku_2(i , d++);
        }
    }
}

int main() {
    int m, q, i, a, b;
    
    cin >> n >> m;
    for (i = 0; i < n; ++i) {
        color[i] = white;
    }
    for (i = 0; i < m; ++i) {
        cin >> a >> b;
        c[a].push_back(b);
        c[b].push_back(a);
    }
    tansaku();
    
    cin >> q;
    
    for (i = 0; i < q; ++i) {
        scanf("%d %d", &a, &b);
        if (color[a] == color[b]) {
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }
    return 0;
}