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<vector>
#include<stack>
#include<queue>
#include<algorithm>

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<int> d(n.size()), dd(n.size(), 0);
	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
					std::vector<int>::iterator itt= std::find(n[*it].relation.begin(), n[*it].relation.end(), num);
					if (itt != n[*it].relation.end()) {
						n[*it].relation.erase(itt);
					}
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
		if (from == to) return true;
	}
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

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

	dfs(n);
	makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
	std::cout << i << " " << n[i].parent << std::endl;
	}
	*/
	/*
	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	//*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

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[10000],s,t;
  vector<int> group[10000];
  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];

      if(x!=t || !group[x].empty())
	group[x] = update(group[x],group[s],join,x,s);


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

  }
 
  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++: #include<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int MAX=1e5+1;
const int NIL=-1;
int n,color[MAX];
vector< int > graph[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(i,graph[u].size()){
            int v=graph[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}
int main(){
    int s,t,m,q;
    scanf("%d%d",&n,&m);
    REP(i,m){
        scanf("%d%d",&s,&t);
        graph[s].push_back(t);
        graph[t].push_back(s);
    }
    int id=1;
    fill(color,color+MAX-1,NIL);
    REP(u,n){
        if(color[u]==NIL) dfs(u,id++);
    }
    scanf("%d",&q);
    REP(i,q){
        scanf("%d%d",&s,&t);
        printf("%s\n",color[s]==color[t]?"yes":"no");
    }
    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];
}
if(d[j]==j){
	while(d[h]!=h){
		h=d[h];
	}
    d[j]=h;
d[d[a]]=h;
    d[a]=h;
d[b]=h;
}
else{
	while(d[j]!=j){
		j=d[j];
	}
  d[h]=j;
    d[b]=j;
d[a]=j;
}
}
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;
}