Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     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() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		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()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

double distanceAB(double xa, double ya, double xb, double yb);
//void trace(int A[], int N);

/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/


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();
		REP(G[u].size())
		{
			int v = G[u][i];
			if (color[v] == NIL)
			{
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor()
{
	int id = 1;
	REP(n) color[i] = NIL;
	REP(n) if (color[i] == NIL) dfs(i, id++);
}


int main()
{
	int s, t, m, q;
	
	cin >> n >> m;
	
	REP(m)
	{
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	
	assignColor();
	
	cin >> q;
	
	REP(q)
	{
		cin >> s >> t;
		if (color[s] == color[t])
		{
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}
	
	return 0;
}


/*
void trace(int A[], int N)
{
	REP(N)
	{
		if (i > 0) cout << " ";
		cout << A[i];
	}
	cout << endl;
}
*/


double distanceAB(double xa, double ya, double xb, double yb)
{
	return sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya));
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>

using namespace std;

class Friend
{
public:
  vector<int> v;
  int solve();
  bool isFriend( vector<vector<int>>& adj, vector<int>& visited, int u, int v );
};

bool Friend::isFriend( vector<vector<int>>& adj, vector<int>& visited, int u, int v )
{
  if ( u == v ) return true;

  if ( visited[u] ) return false;

  visited[u] = 1;

  int n = adj.size();
  
  for ( auto i = 0; i < n; ++i )
    {
      if ( adj[u][i] )
        if ( isFriend( adj, visited, i, v ) ) return true;
    }

  return false;
}

int Friend::solve()
{
  int n, m;
  cin >> n >> m;
  
  vector<vector<int>> adj( n, vector<int>(n,0) );

  for ( auto i = 0; i < m; ++i )
    {
      int u, v;
      cin >> u >> v;
      adj[u][v] = 1;
      adj[v][u] = 1;
    }

  int q;
  cin >> q;

  for ( auto i = 0; i < q; ++i )
    {
      int u, v;
      cin >> u >> v;
      vector<int> visited( n, 0 );
      
      if ( isFriend( adj, visited, u, v ) )
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  
  return 0;
}

int main()
{
  Friend f;

  f.solve();

  return 0;
}

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

using namespace std;

static const int LEN=100000;
static const int NIL=-1;

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

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(unsigned int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}

void assignColor()
{
    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);
    }
    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<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int>V[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 < V[u].size(); i++) {
			int v = V[u][i];
			if (color[v] == NIL) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor() {
	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;
		V[s].push_back(t);
		V[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<iostream>
#include<vector>
using namespace std;
int cnt=1;
struct Point{
	int x;
	int y;
};

bool depth(vector< vector<int> > &vv,vector<int> &f,int num1,int num2){
	
	f[num1]=cnt;
	cnt++;
	
	for(int i=0; i<vv[num1].size(); i++){
		if(vv[num1][i]!=0 && f[i]==0){
			depth(vv,f,i,num2);
		}
	}

	if(f[num2]!=0){
		return 1;
	}else{
		return 0;
	}	

}
int main(){
	int n,m,l;
	cin>>n>>m;
	vector< vector<int> > v(n,vector<int> (n,0));
	for(int i=0; i<m; i++){
		Point a;
		cin>>a.x>>a.y;
		v[a.x][a.y]=1;
	}
	cin>>l;
	vector<string> ans(l);
	for(int i=0; i<l; i++){
		Point b;
		cin>>b.x>>b.y;
		vector<int> ft(n,0);
		int tmp;
		if(b.y<b.x){
			tmp=b.y;
			b.y=b.x;
			b.x=tmp;
		}
		if(depth(v,ft,b.x,b.y)){
			ans[i]="yes";
		}else{
			ans[i]="no";
		}
	}
	for(int i=0; i<l; i++){
		cout<<ans[i]<<endl;
	}
  return 0;
}