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<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;
	int i, u, v;
	while ( !S.empty() ) {
		u = S.top();
		S.pop();
		for ( i = 0; i < G[u].size(); i++ ) {
			v = G[u][i];
			if ( color[v] == NIL ) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

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

int main() {
	int s, t, m, q, i;

	cin >> n >> m;

	for ( i = 0; i < m; i++ ) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	assignColor();

	cin >> q;

	for ( 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>

std::vector<int> v[100010];
int n;
int m;

bool visited[10010];

/**
bool dfs(int from, int to) {

  if(visited[to]) {
    return true;
  }

  visited[from] = true;
  for(int u=0; u<v[from].size(); ++u) {
    if(visited[v[from][u]]) continue;
    else {
      visited[v[from][u]] = true;
      dfs(v[from][u], to);
    }
  }

  return false;
}
**/

void dfs(int from) {
  visited[from] = true;
  for(int u=0; u<v[from].size(); ++u) {
    if(visited[v[from][u]]) continue;
    else {
      dfs(v[from][u]);
    }
  }
}

int main() {
  std::cin >> n >> m;

  for(int i=0; i<m; ++i) {
    int from, to;
    std::cin >> from >> to;
    v[from].push_back(to);
    v[to].push_back(from);
  }

  int q_n;
  std::cin >> q_n;
  bool answers[10010];
  for(int i=0; i<q_n; ++i) {
    int from, to;
    std::cin >> from >> to;
    for(int j=0; j<n; ++j) visited[j] = false;

    dfs(from);
    answers[i] = visited[to];
  }

  for(int i=0; i<q_n; ++i) {
    if(answers[i]) std::cout << "yes" << std::endl;
    else std::cout << "no" << std::endl;
  }

}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);

using namespace std;

vector<int> G[200001];
int color[200001];
int s[10000],head,tail;
void dfs(int r,int c){
  head=tail=0;
  s[tail++]=r;
  color[r]=c;
  while(head!=tail){
    int u=s[head++];
    for(int i=0;i<(int)G[u].size();i++){
      int v=G[u][i];
      // if(v!=0){
	if(color[v]==-1){
	  s[tail++]=v;
	  color[v]=c;
	}
	//}
    }
  }
}
int main(){
  int n,m,ia,ib,p,id=1;
  rep(i,200000)color[i]=-1;
  cin>>n>>p;
  rep(i,p){
    cin>>ia>>ib;
    G[ia].push_back(ib);
  }
  rep(i,p)if(color[i]==-1)dfs(i,id++);
  cin>>m;
  rep(i,m){
    cin>>ia>>ib;
    if(color[ia]==color[ib])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }

  return 0;
}

C++: /* 
 * ALDS1_11B.cpp
 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

using Friends = vector<int>;
using SNS = vector<Friends>;

int n, m, q;
bool visited[100000];
SNS sns;

bool is_friend(int id, int target) {
    if (visited[id])
        return false;

    visited[id] = true;
    for (int i = 0; i < sns[id].size(); ++i) {
        int now = sns[id][i];
        if (is_friend(now, target))
            return true;
    }
    return false;
}

int main(void) {

    scanf("%d %d", &n, &m);
    sns.resize(n);
    for (int i = 0, id, f; i < m; ++i) {
        scanf("%d %d", &id, &f);
        sns[id].push_back(f);
        sns[f].push_back(id);
    }

    scanf("%d", &q);
    for (int i = 0, id, f; i < q; ++i) {
        scanf("%d %d", &id, &f);
        memset(visited, 0, sizeof(bool) * n);
        if (is_friend(id, f))
            printf("yes\n");
        else
            printf("no\n");
    }

    return 0;
}

C++: #include <iostream>
#include <assert.h>
using namespace std;
#define MAX_V 100000

int represent[MAX_V];
int status[MAX_V];

int findRepresent(int x)
{
    if (represent[x] == x) {
        return x;
    } else {
        represent[x] = findRepresent(represent[x]);
        return represent[x];
    }
}

void unite(int x, int y)
{
    x = findRepresent(x);
    y = findRepresent(y);
    if (x == y)
        return;
    if (status[x] < status[y])
        swap(x, y);
    assert(status[x] >= status[y]);
    represent[y] = x;
    if (status[x] == status[y])
        status[x]++;
}

bool same(int x, int y)
{
    return (findRepresent(x) == findRepresent(y));
}

int main()
{
    for (int i = 0; i < MAX_V; ++i) {
        represent[i] = i;
        status[i] = 0;
    }

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        unite(a, b);
    }
    for (int i = 0; i < n; ++i) {
        cout << represent[i] << " " << status[i] << endl;
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int from, to;
        cin >> from >> to;
        cout << (same(from, to)? "yes" : "no") << '\n';
    }
    return 0;
}