Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[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;
  }
}