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

bool search(vector<int> member,int x){
  for(int i=0; i<member.size(); i++){
    if(member[i] == x)
      return true;
  }
    return false;
}
vector<int> update(vector<int> mother,vector<int> children,int join[],int a,int b){
  mother.push_back(b);
  join[b] = join[a];
  for(int i=0; i<children.size(); i++){
    int num=children[i];
    mother.push_back(num);
    join[num] = join[a];
  }

  return mother;
}
int main(){
  int n,m,join[100000],s,t;
  vector<int> group[100000];
  cin >> n >> m;

  for(int i=0; i<n; i++)
    join[i] = i;
    
  for(int i=0; i<m; i++){
    cin >> s >> t;
    bool flag = search(group[join[t]],s);

    if(!flag){
      int x = join[t];
      int y = join[s];

      if(x!=t || !group[x].empty()){
	group[x] = update(group[x],group[y],join,x,y);
	group[y].clear();
      }

      else{
	group[y].push_back(t);
	join[x] = y;
      }
    }

  }
 
  int q;
  cin >> q;
  
  bool answer[10000];
  for(int i=0; i<q; i++){
    cin >> s >> t;
    
    if(join[s] == join[t])
      answer[i]=true;
    else
      answer[i]=false;
  }

  for(int i=0; i<q; i++){
    if(answer[i])
      cout << "yes\n";
    else
      cout << "no\n";
  }
}

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

void G_find(vector<vector<int> > A, vector<int> &v, int u){
	v[u] = 1;
	for(int i=0;i<v.size();i++)
		if((A[u][i] == 1) && (v[i] == 0))
			G_find(A, v, i);
}

int main(){
	int n, m, q, s, t;
	vector<vector<int> > A;
	vector<int> v;

	cin>>n>>m;
	A.resize(n);
	v.resize(n);
	for(int i=0;i<n;i++)
		A[i].resize(n);

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			A[i][j] = 0;

	for(int i=0;i<m;i++){
		cin>>s>>t;
		A[s][t] = 1;
		A[t][s] = 1;
	}

	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s>>t;
		for(int j=0;j<n;j++)
			v[j] = 0;

		G_find(A, v, s);

		if(v[s] == v[t])
			cout<<"yes"<<endl;
		else
			cout<<"no"<<endl;
	}

	return 0;
}

C++: 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<vector>

#define NIL -1
int color[100000];
std::vector<int>V[100005];

void dfs(int i,int id){
    std::stack<int>S;
    S.push(i);
    color[i]=id;
    int j;
    while(!S.empty()){
        int top=S.top();
        S.pop();
        for(j=0;j<V[top].size();j++){
            if(color[V[top][j]]==NIL){
                color[V[top][j]]=id;
                S.push(V[top][j]);
            }
        }
    }
}
int main(){
    int n,m,i;
    scanf("%d %d",&n,&m);
    
    for(i=1;i<=m;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        V[s].push_back(t);
        V[t].push_back(s);
    }
    for(i=0;i<=n-1;i++)color[i]=NIL;
    int id=1;
    for(i=0;i<=n-1;i++){
        if(color[i]==NIL){
            id++;
            dfs(i,id);
        }
    }
    
    int q;
    scanf("%d",&q);
    for(i=1;i<=q;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        if(color[s]==color[t])printf("yes");
        else printf("no");
        printf("\n");
    }
    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,vector<int> &c,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,c,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));
	vector<int> ft(n,0);
	vector<int> ct(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;
		if(depth(v,ft,ct,b.x,b.y)){
			ans[i]="yes";
		}else{
			ans[i]="no";
		}
	}
	for(int i=0; i<l; i++){
		cout<<ans[i]<<endl;
	}
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <unordered_set>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< unordered_set<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< unordered_set<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).insert(t);
		friendGraphList.at(t).insert(s);
	}
}

bool checkFriendAccesibility(const vector<unordered_set<int>>& graphList, const int s, const int t) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	//vector<int> distanceList(n, -1);//
	vector<bool> isVisitList(n, false);//距離に興味はないのでbool
	nodeQueue.push(s);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		isVisitList.at(id).flip();//trueにする
		const auto& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			if (isVisitList.at(adjID))continue;
			if (graphList.at(adjID).find(id) == graphList.at(adjID).end())continue;
			if (adjID == t)return true;
			nodeQueue.push(adjID);
		}
		nodeQueue.pop();
	}
	return false;
}

template<typename T>
void execute(T& cin) {
	vector< unordered_set<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << (checkFriendAccesibility(graphList, s, t) ? "yes" : "no") << endl;
	}
}

int main()
{
	execute(cin);
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>

typedef struct{
    int height;
    int parent;
}node;

node* nd;

int top(int key){
    if(nd[key].parent == -1)return key;
    else return top(nd[key].parent);
}

void join(int s, int t){
    int a = top(s);
    int b = top(t);
    if(a==b)return;
    if(nd[a].height<nd[b].height){
        nd[a].parent = b;
    }else{
        nd[b].parent = a;
        if(nd[a].height==nd[b].height)nd[a].height++;
    }
    return;
}

bool isFriend(int s, int t){
    if(top(s)==top(t))return true;
    else return false;
}

int main(){
    int i, n, m, s, t, q;
    scanf("%d %d",&n,&m);

    nd = (node *)malloc(sizeof(node)*n);

    for(i=0;i<n;i++){
        nd[i].height = 1;
        nd[i].parent = -1;
    }

    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        join(s,t);
    }

    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(s,t))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}