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<cinttypes>
#include<numeric>


using ui32 = std::uint32_t;

class union_find {
public:
	using size_type = decltype(std::declval<std::vector<int>>().size());
private:
	std::vector<size_type> par;
	std::vector<size_type> size_;

public:
	union_find() :par{}, size_{} {}
	union_find(size_type s) :union_find{} {
		par.resize(s);
		std::iota(std::begin(par), std::end(par), 0);

		size_.resize(s, 1);
	}

	auto root(size_type index_)->size_type {
		if (par[index_] == index_)
			return
				par[index_];
		else
			return
				par[index_] = root(par[index_]);
	}

	auto size(size_type index_) {
		return size_[root(index_)];
	}

	auto same(size_type ai, size_type bi) {
		return root(ai) == root(bi);
	}

	auto unite(size_type ai, size_type bi) {
		auto ar = root(ai);
		auto br = root(bi);

		if (ar == br)
			return;

		const auto as = size(ar);
		const auto bs = size(br);

		if (as >= bs)
			std::swap(ar, br);

		par[br] = par[ar];

		if(as == bs)
			size_[ar] = size_[br] = as + bs + 1;

	}

};

int main() {

	ui32 n, m, q;

	std::cin >> n >> m;
	union_find uf(n);
	for (ui32 i{}; i < m; ++i) {
		ui32 s, t;
		std::cin >> s >> t;
		uf.unite(s, t);
	}

	std::cin >> q;
	for (ui32 i{}; i < q; ++i) {
		ui32 s, t; 
		std::cin >> s >> t;
		const auto ans = uf.same(s, t)?"yes":"no";
		std::cout << ans << "\n";
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX=100005;
vector<int> G[MAX];
bool vis[MAX];
int group[MAX];
int n,m,number=0;
void dfs(int u){
	group[u]=number;
	vis[u]=1;
	for(unsigned int i=0;i<G[u].size();i++){
		if(!vis[G[u][i]])dfs(G[u][i]);
	}
}
int main(){
	int q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int u,v;
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=0;i<n;i++){
			if(!vis[i]){
				dfs(i);
				number++;
			}
		}
	cin>>q;
	for(int i=0;i<q;i++){
		int u,v;
		cin>>u>>v;
		printf("%s\n",((group[u]==group[v])?"yes":"no"));
	}
}

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

int color[N];
vector<int> G[N];
queue<int> Q;
int n;

void bfs(int s,int c) {
	Q.push(s);
	color[s] = c;
	while (!Q.empty()) {
		for (unsigned int it = 0;it < G[Q.front()].size();it++)
			if (color[G[Q.front()][it]] == 0){
				Q.push(G[Q.front()][it]);
				color[G[Q.front()][it]] = c;
		    }
		Q.pop();
	 }
}

void Color() {
	int c = 0;
	for(int i=0;i<n;i++)
	if(color[i]==0) bfs(i, ++c);
}

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

	Color();

	cin >> q;
	for (int i = 0;i < q;i++) {
		int u, v;
		cin >> u >> v;
		if (color[u] == color[v] && color[u] != 0)
			cout << "yes" << endl;
		else
			cout << "no"<<endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<stack>
#include<queue>
#include<functional>
#include<iterator>
#include<cmath>

using namespace std;

typedef pair<int,int> P;


const int max_n = 1e5;

int par[max_n];
int rnk[max_n];

void init(int n){
	for(int i=0; i<n; i++){
		par[i] = i;
		rnk[i] = 0;
	}
}

int find_root(int x){
	if(par[x] == x) return x;
	else			return par[x] = find_root(par[x]);
}

void unite(int x, int y){
	x = find_root(x);
	y = find_root(y);
	if(x == y) return;

	if(rnk[x] < rnk[y]){
		par[x] = y;
	}
	else{
		par[y] = x;
		if(rnk[x] == rnk[y]) rnk[x]++;
	}
}

bool same(int x, int y){
	return find_root(x) == find_root(y);
}


int main(){
	int n,m;
	cin >> n >> m;

	init(n);

	int s,t;
	for(int i=0;i<m;i++){
		cin >> s >> t;
		unite(s,t);
	}

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

	return 0;
}

C++: #include<stdio.h>
#include<queue>
 
#define MAX 100000
 
int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;
 
bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;
 
    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }
 
    return false;
}
 
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }
 
    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        //bfs(s, d) ? printf("yes\n") : printf("no\n");
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <string>
#include <climits>
using namespace std;

#define MAX_N  100000
#define MAX_M  100000
#define MAX_Q  10000

int n, m, q;
queue<int> Q;
int d[MAX_N] = {};

int bf(vector<vector<int> >& sns, int src, int dst)
{
    Q.push(src);
    while (!Q.empty()) {
        if (dst == (src = Q.front())) return 1;
        Q.pop();
        d[src]++;
        for (auto itr = sns.at(src).begin(); itr != sns.at(src).end(); itr++) {
            if (!d[*itr]) Q.push(*itr);
        }
    }
    return 0;
}

int main()
{
    cin >> n >> m;
    vector<vector<int> > sns(n);
    int s, d;
    while (m--) {
        cin >> s >> d;
        sns.at(s).push_back(d);
        sns.at(d).push_back(s);
    }
    cin >> q;
    while (q--) {
        cin >> s >> d;
        cout << (bf(sns, s, d) ? "yes" : "no") << endl;
    }
    return 0;
}