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 <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

bool canReach(int s, int d, vector<int> &h){
	h.push_back(s);
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(find(h.begin(), h.end(), nodes[s].rel[i]) == h.end()){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		vector<int> h;
		h.clear();
		printf("%s\n", canReach(l, r, h) ? "yes" : "no");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define NIL -1

vector<int> G[100003];
int color[100003];
int n;

int nextPoint(int curPoint){
    int next;
    for(int i=0;i<G[curPoint].size();++i){
        next = G[curPoint][i];
        if(color[next] == NIL){
            return next;
        }
    }
    return -1;
}

void dfs(int point, int value){
    color[point] = value;
    while(1){
        int next = nextPoint(point);
        if(next != -1){
            dfs(next,value);
        }
        else{
            color[point] = value;
            return;
        }
    }
}

int main(){
    int m, s, t;
    scanf("%d %d",&n, &m);
    for(int i=0;i<m;++i){
        scanf("%d %d",&s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
    for(int i=0;i<n;++i){
        color[i] = NIL;
    }
    int id = 0;
    for(int i=0;i<n;++i){
        if(color[i]==NIL){
            id += 1;
            dfs(i,id);
        }
    }
    int q;
    scanf("%d",&q);
    for(int i=0;i<q;++i){
        scanf("%d %d",&s, &t);
        if(color[s]==color[t]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

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

void bfs(int s);

int n_user,m,*deg;
vector<int> *G; 

int main(void){
	cin>>n_user>>m;
	deg = new int[n_user];
	G = new vector<int>[n_user];
	for(int i=0;i<n_user;i++)
		deg[i] = -1;

	int user1,user2;
	for(int i=0;i<m;i++){
		cin>>user1>>user2;
		G[user1].push_back(user2);
		G[user2].push_back(user1);
	}

	for(int i=0;i<n_user;i++) bfs(i);

	int question,s,t;
	cin>>question;
	for(int i=0;i<question;i++){
		cin>>s>>t;
		if(deg[s]!=-1){
			if(deg[s]==deg[t])
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	delete [] G; delete [] deg;
}

void bfs(int s){
	queue<int> q;
	q.push(s);
	if(deg[s]==-1) deg[s] = s;
	else return ;

	int u;
	while(!q.empty()){
		u = q.front();
		q.pop();
		for(int v=0;v<G[u].size();v++){
			if(deg[G[u][v]]==-1){
				deg[G[u][v]] = deg[u];
				q.push(G[u][v]);
			}
		}
	}
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

int n,m,q,from,to;
int rank[100000],parent[100000];

int find(int x){
	if(x == parent[x])return x;
	else{
		return parent[x] = find(parent[x]);
	}
}

bool isSame(int x,int y){
	return find(x) == find(y);
}

void unite(int x,int y){
	x = find(x);
	y = find(y);

	if(x == y)return;

	if(rank[x] < rank[y]){
		parent[x] = y;
	}else{
		parent[y] = x;
		if(rank[x] == rank[y])rank[x]++;
	}
}

int main(){

	scanf("%d %d",&n,&m);

	for(int i = 0; i < n; i++){
		rank[i] = 0;
		parent[i] = i;
	}

	for(int i = 0; i < m; i++){
		scanf("%d %d",&from,&to);

		if(isSame(from,to))continue;
		unite(from,to);
	}

	scanf("%d",&q);

	int left,right;

	for(int i = 0; i < q; i++){
		scanf("%d %d",&left,&right);

		if(isSame(left,right))printf("yes\n");
		else{
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <list>
using namespace std;

class Node
{
private:
public:
	Node();
	int id;
	list<int> next_node_list;
	int next_idx;
	void AddNextNode(int id);
	void SetGroup(int prev_id, int num);
	int group;
	void Init(int id);
};

Node::Node()
{
	group = 0;
	next_idx = 0;
}

void Node::AddNextNode(int id)
{
	next_node_list.push_back(id);
}

Node *Node_list;

void Node::SetGroup(int prev_id, int num)
{
	group = num;

	list<int>::iterator itr;
	itr = next_node_list.begin();
	for (int i = 0; i < next_idx; i++) {
		itr++;
	}
	
	for(; itr != next_node_list.end(); itr++){
		next_idx++;
		if (prev_id != *itr) {
			Node_list[*itr].SetGroup(id, num);
		}
	}
}

void Node::Init(int id)
{
	this->id = id;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n]();
	for (int i = 0; i < n; i++) {
		Node_list[i].Init(i);
	}
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}

	//group
	int group = 1;
	for (int i = 0; i < m; i++) {
		if (Node_list[i].group) continue;
		Node_list[i].SetGroup(-1, group);
		group++;
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		if (Node_list[s].group == Node_list[t].group && Node_list[s].group != 0) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}