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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
 
using namespace std;
 
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
 
const int INF=1<<29;
const double EPS=1e-9;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
/*
class UnionFind {
public:
	int N;
	std::vector<int> parent;
	UnionFind(int n) : N(n), parent(N, -1) {

	}
	void Union(int a, int b) {
		a = root(a);
		b = root(b);
		int sizea = size(a);
		int sizeb = size(b);
		if (sizea > sizeb) {
			int temp = b;
			b = a;
			a = temp;
		}
		parent[b] += parent[a];
		parent[a] = b;
	}
	bool Find(int a, int b) {
		return root(a) == root(b);
	}
	int root(int a) {
		if (parent[a] < 0) return a;
		return parent[a] = root(parent[a]);
	}
	int size(int a) {
		a = root(a);
		return -parent[a];
	}
};
*/

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};
int main(int argc, char const *argv[]) {
	int N, M;
	cin >> N >> M;
	UnionFind uf(N);
	for (int i = 0; i < M; ++i) {
		int a, b;
		cin >> a >> b;
		uf.unionSet(a, b);
	}
	int Q;
	cin >> Q;
	for (int i = 0; i < Q; ++i) {
		int a, b;
		cin >> a >> b;
		if (uf.findSet(a, b)) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
vector<vector<int>>vec;
int n;
vector<int>color;
void bsf(int s,int c) {
	queue<int>q;
	q.push(s);
	color[s] = c;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		rep(v, 0, vec[u].size()) {
			if (color[vec[u][v]] != -1)continue;
			color[vec[u][v]] = c;
			q.push(vec[u][v]);
		}
	}
}
void solve() {
	rep(i, 0, n) {
		if (color[i] == -1)bsf(i, i);
	}
}
signed main() {
	cin >> n; vec = vector<vector<int>>(n); color = vector<int>(n, -1);
	int m; cin >> m;
	rep(i, 0, m) {
		int s, t; cin >> s >> t;
		vec[s].push_back(t);
		vec[t].push_back(s);
	}
	solve();
	int q; cin >> q;
	rep(i, 0, q) {
		int s, t; cin >> s >> t;
		if (color[s] == color[t])cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

class UF{
public:
  vector<int> u;
  void init(int n){
    u.resize(n, 0);
    for(int i=0; i<n; i++) u[i] = i;
  }
  int root(int x){
    if( u[x] == x ) return x;
    else return root(u[x]);
  }
  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;
    u[x] = y;
  }
};

int main(void) {

  int n, m;
  cin >> n >> m;
  UF uf;
  uf.init(n);
  int s, t;
  for(int i=0; i<m; i++){
    cin >> s >> t;
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  for(int i=0; i<q; i++){
    cin >> s >> t;
    cout << ( uf.same(s, t) ? "yes" : "no" ) << endl;
  }



  return 0;
}

// EOF

C++: #include<iostream>
#include<vector>
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 col) {
	for (int i = 0; i < G[r].size(); i++) {
		int v = G[r][i];
		if (color[v] == NIL) {
			color[v] = col;
			dfs(v, col);
		}
	}
}

void assignColor() {
	int col = 0;
	for (int i = 0; i < n; i++) {
		color[i] = NIL;
	}
	for (int i = 0; i < n; i++) {
		if (color[i] == NIL) {
			dfs(i, col++);
		}
	}
}

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

	assignColor();

	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> f >> s;
		if (color[f] == color[s]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	
	return 0;
}