Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int from[100000];
int to[100000];

bool checked[100000];

bool judge(int f, int t,int n)
{
	if (checked[f])
	{
		return false;
	}

	checked[f] = true;

	bool isFriend = false;
	for (int i = 0; i < n; i++)
	{
		if (from[i] == f)
		{
			if (to[i] == t)
			{
				isFriend = true;
			}
			else
			{
				isFriend = judge(to[i], t, n);
			}
			if (isFriend)
			{
				break;
			}
		}
		if (to[i] == f)
		{
			if (from[i] == t)
			{
				isFriend = true;
			}
			else
			{
				isFriend = judge(from[i], t, n);
			}
			if(isFriend)
			{
				break;
			}
		}
	}
	return isFriend;
}

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

	for (int i = 0; i < m; i++)
	{
		int f, t;
		cin >> f >> t;

		from[i] = f;
		to[i] = t;
	}

	int q;
	cin >> q;

	for (int i = 0; i < q; i++)
	{
		int f, t;
		cin >> f >> t;

		for (int i = 0; i < n; i++)
		{
			checked[i] = false;
		}

		printf(judge(f, t, n) ? "yes\n" : "no\n");
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t; vector<int>a[100000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	while (!Q.empty()) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			if (x[a[Q.front()][k]] == 0) { Q.push(a[Q.front()][k]); x[a[Q.front()][k]] = 1; }
		}
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (bfs(s, t)) { cout << "yes" << endl; }
		else { cout << "no" << endl; }
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int color[(int)1e5 + 1];
vector<vector<int> > edge;
int cl;
void dfs(int v){
	color[v] = cl;
	REP(i,edge[v].size()){
		if(not color[edge[v][i]]){
			dfs(edge[v][i]);
		}
	}
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n,m,q;
	cin>>n>>m;
	edge.resize(n,vector<int>());
	REP(i,m){
		int u,v;cin>>u>>v;
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	REP(i,n){
		cl = i+1;
		if(not color[i]){
			dfs(i);
		}
	}
	cin>>q;
	REP(i,q){
		int u,v;cin>>u>>v;
		if(color[u] == color[v]){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q;
int COLOR[MAX_N], N, M, Q1, A, B;
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); X[B].push_back(A); }
	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (COLOR[i] == 0) {
			Q.push(i); cnt++; COLOR[i] = cnt;
			while (!Q.empty()) {
				int a = Q.front(); Q.pop();
				for (int j = 0; j < X[a].size(); j++) {
					int to = X[a][j];
					if (COLOR[to] == 0) {
						COLOR[to] = cnt;
						Q.push(to);
					}
				}
			}
		}
	}
	cin >> Q1;
	for (int i = 0; i < Q1; i++) {
		cin >> A >> B;
		if (COLOR[A] == COLOR[B]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include<iostream>    // ???????????????????§?????????£????????????
using namespace std;
#include<stdio.h>

typedef struct fox { int val; fox *prev, *next; } fox_t;

fox_t* init()
{
	fox_t* head = new fox_t;
	head->val = -1;
	head->next = NULL;
	head->prev = NULL;
	return head;
}

fox_t* make_tail(fox_t* head)
{
	fox_t* tail = new fox_t;
	tail->val = -1;
	head->next = tail;
	tail->next = NULL;
	tail->prev = head;
	return tail;
}

bool isEmpty(fox_t* head, fox_t* tail)
{
	return (head->next == tail ? true : false);
}

fox_t* b_insert(fox_t* tail, int data)
{
	fox_t* fox = new fox_t;
	fox->val = data;
	fox->prev = tail->prev;
	fox->next = tail;
	tail->prev->next = fox;
	tail->prev = fox;
	return fox;
}
// docking.
void unionfox(fox_t* head1, fox_t* tail1, fox_t* head2, fox_t* tail2)
{
	if(isEmpty(head1, tail1) || isEmpty(head2, tail2)) return;
	fox_t* fox = tail1->prev;
	cout << tail1->prev << endl;
	cout << head2->next << endl;
	fox->next = head2->next;
	head2->next->prev = fox;
	tail2->prev->next = tail1;
	tail1->prev = tail2->prev;
	delete tail2;
	delete head2;
}

int main()
{
	int color[100001];
	int c_num = 0;
	fox_t* foxhead[100001];
	fox_t* foxtail[100001];
	int i, n, m, q;
	int a, b;
	cin >> n >> m;
	for(i = 0; i < n; i++){
		color[i] = -1;
		foxhead[i] = init();
		foxtail[i] = make_tail(foxhead[i]);
	}
	int c_a, c_b, val;

	for(i = 0; i < m; i++){
		cin >> a >> b;
		c_a = color[a];
		c_b = color[b];
		if(c_a < c_b){ swap(c_a, c_b); swap(a, b); }  // color[a] >= color[b].
		if(c_a < 0 && c_b < 0){
			color[a] = c_num; color[b] = c_num;
			b_insert(foxtail[c_num], a);
			b_insert(foxtail[c_num], b);
			c_num++;
		}else if(c_b < 0){
			color[b] = c_a;
			b_insert(foxtail[c_a], b);
		}else if(c_a == c_b){
			continue;
		}else{ // color[a] != color[b].
			fox_t* fox = foxhead[c_b]->next;
			while(fox != foxtail[c_b]){
				val = fox->val;
				color[val] = c_a;
				fox = fox->next;
			};
			unionfox(foxhead[c_a], foxtail[c_a], foxhead[c_b], foxtail[c_b]);
		}
	}
	cin >> q;
	for(i = 0; i < q; i++){
		cin >> a >> b;
		if(color[a] >= 0 && (color[a] == color[b])){
			printf("yes\n"); }else{ printf("no\n"); 
			}
	}
	return 0;
}