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

using namespace std;
static const int MAX=100;
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[i]==NIL)
            {
                color[v]=c;
                S.push(v);
            }
        }
    }
}

void assign_color()
{
    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);
    }
    assign_color();
    
    for(int i=0;i<n;i++)
    {
        cin>>s>>t;
        if(color[s]==color[r])
        {
            cout<<"yes"<<endl;
        }
        else
        {
            cout<<"no"<<endl;
        }
    }
    return 0;
}

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 assingColor(){
  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);
  }

  assingColor();
  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>
using namespace std;

bool is_friend(const vector<vector<int> >& u, int s, int t, vector<int>& searched){
    if (s == t) return true;
    int len = u[s].size();
    for (int i = 0; i < len; 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 <queue>
#include <set>
using namespace std;

const int MAX_N = 100000;

int G[MAX_N];
set<int> A[MAX_N];

void assign_gid(int user_num) {
  int gid;
  queue<int> Q;

  Q.push(0);
  G[0] = gid = 0;

  while (!Q.empty()) {
    int c = Q.front(); Q.pop();

    if (G[c] == -1) G[c] = ++gid;

    for (set<int>::iterator it = A[c].begin(); it != A[c].end(); it++) {
      if (G[*it] == -1) {
        G[*it] = gid;
        Q.push(*it);
      }
    }

    if (Q.empty()) {
      for (int i = 1; i < user_num; i++)
        if (G[i] == -1) Q.push(i);
    }
  }
}

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

  for (int i = 0; i < n; i++) G[i] = -1;

  for (int i = 0; i < m; i++) {
    int s, d; cin >> s >> d;
    A[s].insert(d);
    A[d].insert(s);
  }

  assign_gid(n);

  int q; cin >> q;

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

  return 0;
}

C++: /**
 */

#include <iostream>
#include <vector>
using namespace std;
vector< vector<int> > rel;
vector<bool> visit;
int n, m, q;

bool fre(int j, int k) 
{
	for (int i=0;i<rel[j].size();i++)
	{
		int kk = rel[j][i];
		if (kk==k) return true;
		if (visit[kk])continue;
		visit[kk]=true;
		if(fre(kk,k))return true;
	}
	return false;
}

int main()
{
	cin >> n;
	rel.resize(n);
	cin >> m;
	for (int i=1;i<=m;i++){
		int j, k;
		cin >> j >> k;
		rel[j].push_back(k);
		rel[k].push_back(j);
	}

	cin >> q;
	for (int i=1;i<=q;i++){
		int j, k;
		cin >> j >> k;
		visit.clear();
		visit.resize(n);
		cout << (fre(j, k) ? "yes" : "no") << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
const double PI = acos(-1);
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;

class UnionFind {
public:
  vector <ll> par;
  vector <ll> siz;
  UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i) par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i) par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  bool issame(ll x, ll y) {
    return root(x) == root(y);
  }
  ll size(ll x) {
    return siz[root(x)];
  }
};

int main() {
  int n,m; cin >> n >> m;
  UnionFind uf(n);
  rep(i,m) {
    int s,t; cin >> s >> t;
    uf.merge(s,t);
  }

  int q; cin >> q;
  vector<int> s(q),t(q);
  rep(i,q) cin >> s[i] >> t[i];
  rep(i,q) {
    if (uf.issame(s[i],t[i])) cout << "yes" << endl;
    else cout << "no" << endl;
  }
}