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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N, M, Q;
vector< vector<int> > graph;
vector< int > group;

void dfs(int u, int num) {
    group[u] = num;
    for (int v : graph[u])
        if (group[v] == -1) dfs(v, num);
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N >> M;
    graph.resize(N+1); group.resize(N+1);
    for (int j = 0; j < M; ++j) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    fill(begin(group), end(group), -1);
    int group_num = 0;
    for (int j = 1; j <= N; ++j) {
        if (group[j] == -1) dfs(j, group_num++);
    }

    cin >> Q;
    for (int j = 0; j < Q; ++j) {
        int u, v; cin >> u >> v;
        cout << (group[u] == group[v] ? "yes" : "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++){
    scanf("%d %d", &s, &t);
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  for(int i=0; i<q; i++){
    scanf("%d %d", &s, &t);
    cout << ( uf.same(s, t) ? "yes" : "no" ) << endl;
  }



  return 0;
}

// EOF

C++: #include <iostream>
#include <vector>
#include <stack>

using namespace std;

vector<int> G[100000];
int color[100000];
int n;

void dfs(int idx, int c)
{
	stack<int> s;
	s.push(idx);
	color[idx] = 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] == -1)
			{
				color[v] = c;
				s.push(v);
			}
		}
	}
}

void AssignColor()
{
	int id = 1;
	for (int i = 0; i < n; ++i) color[i] = -1;
	for (int u = 0; u < n; ++u)
	{
		if (color[u] == -1) 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 <bits/stdc++.h>
using namespace std;
int n, m;
vector< vector<int> > v;
vector<bool> used;

bool dfs(int pos, int dest){
  if(used[pos]) return false;
  used[pos] = true;
  if(pos == dest) return true;
  for(int i = 0; i < v[pos].size(); i++){
    if(dfs(v[pos][i], dest)){
      return true;
    }
  }
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  v = vector< vector<int> >(n, vector<int>(0));
  used = vector<bool>(n, false);
  for(int i = 0; i < m; i++){
    int a, b; cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int q; cin >> q;
  for(int i = 0; i < q; i++){
    used = vector<bool>(n, false);
    int a, b; cin >> a >> b;
    if(dfs(a, b)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t; vector<int>a[100000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	while (!x[B] == 1 || !Q.empty()) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			if (x[a[Q.front()][k]] == 0) { Q.push(a[Q.front()][k]); x[a[Q.front()][k]] = 1; }
		}
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	cin >> n >> m >> q;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (bfs(s, t)) { cout << "yes" << endl; }
		else { cout << "no" << endl; }
	}
	return 0;
}

C++: #pragma GCC optimize("03")
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long 
using namespace std;
int f[200000];
int find(int x){
	if(f[x]==x)
		return x;
	else
		return f[x]=find(f[x]);
}
void merge(int c,int d){
	f[d]=c;
}
int main()
{
    ios::sync_with_stdio(0);cin .tie(NULL);
    ll a,b,num;
   	cin >> a >> b;
   	for(int i=0;i<a;i++)
	f[i]=i;
    for(int i=0;i<b;i++)
    {
    	ll c,d;
    	cin >> c >> d;
    	c=find(c);
    	d=find(d);
   		merge(c,d);
	}
	cin >> num;
	for(int i=0;i<num;i++)
	{
		ll e,g;
		cin >> e >> g;
		if(find(e)==find(g))
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
}

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) {
    if (i >= G[a].size()) return;
    if (color[G[a][i]] == -1) {
        color[G[a][i]] = color_name;
        set_color(G, color, color_name, G[a][i], 0);
    }
    set_color(G, color, color_name, a, i + 1);
}


bool DFS(const vector<vector<int> >& G, int a, int b) {
    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;
}