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++: /*
* @Author: Samson
* @Date:   2018-05-07 21:12:41
* @Last Modified by:   Samson
* @Last Modified time: 2018-05-07 21:44:27
*/
//   @URL : 
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<stack>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

int n,m,q,color[MAXN];
vector<int> v[MAXN];

void dfs(int x,int id)
{
	stack<int> s;
	int t;
	s.push(x);
	color[x] = id;
	while(s.size())
	{
		t = s.top();
		s.pop();
		for(int i = 0; i < v[t].size(); ++i)
		{
			int val = v[t][i];
			if(color[val] == -1)
			{
				color[val] = id;
				s.push(val);
			}
		}
	}
}

void fun()
{
	int id = 0;
	memset(color,-1,sizeof color);
	for(int i = 0; i < n; ++i)
		if(color[i] == -1)
			dfs(i,++id);
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n>>m;
	int s,e;
	for(int i = 0; i < m; ++i)
	{
		cin>>s>>e;
		v[s].push_back(e);
		v[e].push_back(s);
	}
	fun();
	cin>>q;
	while(q--)
	{
		cin>>s>>e;
		if(color[s] == color[e] && color[s]!=-1)
			cout<<"yes\n";
		else
			cout<<"no\n";
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}

    d[j]=h;
    d[a]=h;
    d[b]=h;
d[d[a]]=h;
d[d[b]]=h;

}

cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define N 100000
#define NIL -1

int n, m;
vector<int> G[N];
int color[N];

queue<int> Q;

void bfs(int x, int c){
    if(color[x] != NIL) return;
    Q.push(x);
    color[x] = c;

    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        for(int i = 0; i < G[u].size(); i++){
                int v = G[u][i];
                if(color[v] == NIL)
                Q.push(v);
                color[v] = c;
        }
    }
}

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


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

    rep(i,m){
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    assignColor();

    int q;
    cin >> q;
    rep(i,q){
        int x, y;
        cin >> x >> y;
        if(color[x] == color[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n) ; i++)
constexpr auto INF = 2147483647;
typedef long long ll;
constexpr int MAX = 100000;

vector<int> Group_list[MAX];
const int NIL = -1;
int Check_list[MAX];
int n;

void Checking(){
  //init
  rep(i,n) Check_list[i] = NIL;
  stack<int> stk;
  int check_cnt = 0;
  //solve
  rep(i,n){
    if(Check_list[i] == NIL) {
      stk.push(i);
      check_cnt++;
      Check_list[i] = check_cnt;
    }
    int now, next;
    while(!stk.empty()){
        now = stk.top();
        stk.pop();
        rep(j, Group_list[now].size()){
          next = Group_list[now][j];
          if(Check_list[next] == NIL) {
            Check_list[next] = check_cnt;
            stk.push(next);
          }
        }
    }
  }
}

int main(){
  int m,q;
  int from, to;
  cin >> n >> m;
  rep(i,m){
    cin >> from >> to;
    Group_list[from].push_back(to);
    Group_list[to].push_back(from);
  }

  Checking();

  cin >> q;
  rep(i,q){
    cin >> from >> to;
    if(Check_list[from] == Check_list[to] && Check_list[from] != NIL){
      cout << "yes" << endl;;
    }else{
      cout << "no" << endl;
    }
  }
  

  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

void set_color(const vector<vector<int> >& u, int p, vector<int>& color, int c) {
    if (color[p] != -1) return;
    color[p] = c;
    for (int i = 0; i < u[p].size(); i++) {
        set_color(u, u[p][i], color, c);
    }
}

bool is_friend(const vector<vector<int> >& u, int s, int t){
    static vector<int> color(u.size(), -1);
    static int c = 0;
    set_color(u, s, color, c++);
    return color[s] == color[t];
}

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;
        if (is_friend(u, s, t)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
using namespace std;
int count = 0;
class edge;

class vertex{
    public:
        int member;
        vector<edge*> edges;
        int label;
        vertex(){
            member = -1;
        }
        void add_member(int n){
            member = n;
        }
};

class edge{
    public:
        vertex *to,*from;
        edge(vertex *_to){
            to = _to;
        }
};

void check_member_dfs(vertex *v){
    v->member = count;
    for(int i = 0; i < v->edges.size(); i++){
        vertex *to = v->edges[i]->to;
        if (to->member >= 0){
            continue;
        }else{
            check_member_dfs(to);
        }
    }
    return;
}

int main(){
    int n,m,temp1,temp2,q;
    cin >> n >> m;
    vertex *V[n];
    for(int i = 0; i < n ; i++) V[i] = new vertex();
    for(int i = 0; i < m; i++){
        cin >> temp1 >> temp2;
        V[temp1]->edges.push_back(new edge(V[temp2]));
        V[temp2]->edges.push_back(new edge(V[temp1]));
    }
    for(int i = 0; i < n; i++){
        if(V[i]->member >= 0){
            continue;
        }else{
            check_member_dfs(V[i]);
            count++;
        }
    }
    cin >> q;
    string result[q];
    for(int i = 0; i < q; i++){
        cin >> temp1 >> temp2;
        if(V[temp1]->member == V[temp2]->member){
            result[i] = "yes";
        }else{
            result[i] = "no";
        }
    }
    for(int i = 0; i < q; i++) cout << result[i] << endl;
}