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<bits/stdc++.h>

using namespace std;
#define WHITE 0 //未到達
#define GLAY 1 //訪問中
#define BLACK 2 //訪問完了
#define INF 1000000001


void dfs(const vector<vector<int> > &adj_list,
     const int id, vector<int> &color,const int color_id){
    color[id] = color_id;
    for(auto itr = adj_list[id].begin();itr != adj_list[id].end(); itr++){
        int v = *itr;
        if(color[v] == -1){
            color[v] = color_id;
            dfs(adj_list,v,color,color_id);
        }
    }
    return;
}

void assignColor(const vector<vector<int> > &adj_list, vector<int> &color,const int n){
    int color_id = 0;
    for(int u = 0; u < n; u++){
        if(color[u] == -1){
            dfs(adj_list,u,color,color_id);
            color_id++;
        }
    }
}


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

    cin >> n >> m;
    vector<vector<int> > adj_list(n);
    vector<int> color(n,-1);


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

    assignColor(adj_list,color,n);

    cin >> q;
    for(int i = 0; i < q; i++){
        cin >> s >> t;
        if(color[s] == color[t]) cout << "yes\n";
        else cout << "no\n";
    }

    return 0;
}

C++: # include <iostream>
# include <cstdio>
using namespace std;
int *a;
int find_top(int i)
{
	while(a[i] != i) i = a[i];
	return i;
}
void union_find(int x,int y)
{
	a[x] = find_top(x);
	a[y] = find_top(y);
	a[a[x]] = a[y];
}
int main(void)
{
	int n,m,u,v;
	scanf("%d %d",&n,&m);
	a = new int [n];
	for(int i = 0;i < n;i++) a[i] = i;
	while(m--)
	{
		scanf("%d %d",&u,&v);
		union_find(u,v);
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d %d",&u,&v);
		a[u] = find_top(u); a[v] = find_top(v);
		printf("%s\n",a[u] == a[v]?"yes":"no");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int CRL = -1;
int v[N]={0};
void ketugou(int k,int l,int n){
	int x,mn;
	x = max(k,l);
	mn = min(k,l);
	for(int i=0;i<n;i++){
		if(v[i]==x)v[i]=mn;
	}
}

int main(){
	int n,m,q;
	memset(v, CRL, sizeof(v));
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL && v[l]==CRL){
			v[k] = v[l] = min(k,l);
		}else if(v[k]!=CRL && v[l]!=CRL){
			ketugou(k,l,n);
		}else if(v[k]==CRL){
			v[k]=v[l];
		}else{
			v[l]=v[k];
		}
	}
	cin >> q;
	for(int i=0;i<q;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL || v[l]==CRL) cout << "no" << endl;
		else if(v[k]==v[l])cout << "yes" << endl;
		else cout << "no" << endl;
	}
    return 0;
}

C++: /**
 */

/*
 * test case 1
 2 1 1 2 1 1 2  
 test case 2
 4 4 1 2 1 3 2 4 3 4 1 1 2   
 */


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector< vector<int> > rel;
vector<bool> visited;
int n, m, q;
int dest = 0;


bool fre1(int j) 
{
	for (int i=0;i<rel[j].size();i++)
	{
		int kk = rel[j][i];
		if (kk==dest) return true;
		if (visited[kk])continue;
		visited[kk]=true;
		if(fre1(kk))return true;
	}
	return false;
}

/**
 * preserve a priority que of nodes with the most connections. 
 * if there is a node with only 2 connections go look for the other one.
 */

priority_queue< pair<int, int> > pq;


bool fre3(int start) 
{
	for (int i=0;i<rel[start].size();i++)
	{
		int cand = rel[start][i];
		if (cand==dest) return true;
		if (visited[cand])continue;
		visited[cand]=true;
		int point = rel[cand].size();
		if (point == 1) continue;
		if (point == 2) {
			if (fre3(cand)) return true;
		} else {
			pq.push(pair<int, int>(point, cand));
		}
	}
	return false;
}


bool fre2(int start) 
{
	pq.push(pair<int,int>(0, start));
	while (pq.size())
	{
		pair<int, int> p = pq.top();
		pq.pop();
		start = p.second;
		if (visited[start])continue;
		if (fre3(start)) return true;
	}
	return false;
}


int main()
{
	cin >> n;
	rel.resize(n);
	cin >> m;
	for (int i=1;i<=m;i++){
		int j, k;
		cin >> j >> k;
		rel[j].push_back(k);
		rel[k].push_back(j);
	}

	cin >> q;
	for (int i=1;i<=q;i++){
		int start;
		cin >> start >> dest;
		visited.clear();
		visited.resize(n, false);
		cout << (fre2(start) ? "yes" : "no") << endl;
		while (pq.size()) pq.pop(); // forgot this.
	}

	return 0;
}

C++: #include "bits/stdc++.h"
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;
	while(!S.empty()){
		int u=S.top();S.pop();
		for (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++: //ALDS1_11D
#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;
 while(!S.empty()){
  int u=S.top(); S.pop();
  for(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;
}