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++: #ifndef ___Class_Union_Find
#define ___Class_Union_Find

#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

class Union_Find
{
private:

    vector<int> data;
    
public:

    Union_Find(unsigned size_) : data(size_, -1) { }
    
    void unite(int x, int y);
    
    bool same(int x, int y);
    
    int root(int x);
};

void Union_Find::unite(int x, int y)
{
    x = root(x);
    y = root(y);
    
    if (x != y)
    {
        if (data[x] < data[y])
        {
            swap(x, y);
        }
        
        data[x] += data[y];
        data[y] = x;
    }
}

bool Union_Find::same(int x, int y)
{
    return root(x) == root(y);
}

int Union_Find::root(int x)
{
    return data[x] < 0 ? x : data[x] = root(data[x]);
}

#endif // ___Class_Union_Find


// ------ main ------ //

#include <cstdio>

int n, m, q, x, y;

int main()
{
    scanf("%d", &n);
    scanf("%d", &m);
    
    Union_Find u(n);
    
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y);
        
        u.unite(x, y);
    }
    
    scanf("%d", &q);
    
    for(int i = 0; i < q; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y);
        
        printf("%s\n", u.same(x, y) ? "yes" : "no");
    }
    
    return 0;
}

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

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P   = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

//---------------------------------------------------------------------------------------------------

struct UnionFind {
    vector<int> _ids; // 添字iが属する集合のID

    UnionFind(int n) {
        _ids.assign(n, -1);
    }

    int root(int x) {
        const auto rx               = _ids[x];
        return rx < 0 ? x : _ids[x] = root(rx);
    }

    // 要素xとyは同じ連結成分に属するか？
    bool connected(int x, int y) {
        return root(x) == root(y);
    }

    // 要素xが属する集合の要素数
    int size(int x) {
        return -_ids[root(x)];
    }

    // 要素xとyを連結
    bool unite(int x, int y) {
        auto rx = root(x), ry = root(y);
        if (rx == ry) return false;

        if (_ids[rx] > _ids[ry]) swap(rx, ry);
        _ids[rx] += _ids[ry];
        _ids[ry] = rx;
        return true;
    }
};
//---------------------------------------------------------------------------------------------------

signed main() {
    int N, Q;
    cin >> N >> Q;
    UnionFind uf(N);
    rep(i, 0, Q) {
        int a, b;
        cin >> a >> b;
        uf.unite(a, b);
    }
    cin >> Q;
    rep(i, 0, Q) {
        int a, b;
        cin >> a >> b;
        cout << (uf.connected(a, b) ? "yes" : "no") << "\n";
    }
}

C++: #include<iostream>
#include<vector>
using namespace std;

bool is_friend(const vector<vector<int> >& u, int s, int t, vector<int>& searched){
    if (s == t) return true;
    for (int i = 0; i < u[s].size(); i++) {
        if (!searched[u[s][i]]) {
            searched[u[s][i]] = 1;
            if (is_friend(u, u[s][i], t, searched)) return true;
        }
    }
    return false;
}
int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > u(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        u[s].push_back(t);
        u[t].push_back(s);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        vector<int> searched(n, 0);
        searched[s] = 1;
        if (is_friend(u, s, t, searched)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<set>
using namespace std;

void set_color(const vector<vector<int> >& G, vector<int>& color,
                int color_name, int a, int i) {
    for (int i = 0; i < G[a].size(); i++) {
        if (color[G[a][i]] == -1) {
            color[G[a][i]] = color_name;
            set_color(G, color, color_name, G[a][i], 0);
        }
    }
}


bool DFS(const vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0 || G[b].size() == 0) return false;
    static vector<int> color(G.size(), -1);
    static int color_name = 0;
    if (color[a] != color[b]) return false;
    if (color[a] == -1) {
        set_color(G, color, color_name++, a, 0);
    }
    if (color[a] == color[b]) return true;
    return false;
}


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

    vector<vector<int> > G(n);

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (vector<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
           cout << "yes" << endl;
        } else {
           cout << "no"  << endl;
        }
    }
    return 0;
}