Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;
#define MAXN 100000
#define NIL -1

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

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 cid = 1;
	memset(color, -1, sizeof(color));
	for(int i = 0; i < n; i++)
	{
		if(color[i] == NIL)
		{
			dfs(i, cid++);
		}
	}
	
}

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

C++: #include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,m,q,s,t,leng[100000],par[100000],i,j;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)	{
		leng[i]=1;	par[i]=i;
	}
	for(i=0;i<m;i++)	{
		scanf("%d %d",&s,&t);
		while(par[s]!=s)	s=par[s];
		while(par[t]!=t)	t=par[t];
		if(s!=t)	{
			if(leng[s]>leng[t])	par[t]=s;
			else if(leng[s]<leng[t])	par[s]=t;
			else {
				par[s]=t;	leng[t]++;
			}
		}
	}
	scanf("%d",&q);
	for(i=0;i<q;i++)	{
		scanf("%d %d",&s,&t);
		while(par[s]!=s)	s=par[s];
		while(par[t]!=t)	t=par[t];
		if(s==t)	printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

C++: #include<iostream>
#include<vector>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, color = 0;
	bool answer_yes = false;

	// enter
	cin >> n >> m;
	vector< vector<int> > Adj(n);
	vector<int> Stack;
	vector<int> Check(n, 0);
	vector<int>::iterator it, itend;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].push_back(t);
		Adj[t].push_back(s);
	}

	// calculate
	for (i = 0; i < n; i++) {
		if (Check[i] == 0) {
			color++;
			Stack.push_back(i);
			Check[i] = color;
			while (!Stack.empty()) {
				id = Stack.back();
				itend = Adj[id].end();
				for (it = Adj[id].begin(); it != itend; it++) {
					if (!Check[*it]) {
						Stack.push_back(*it);
						Check[*it] = color;
						break;
					}
				}
				if (it == itend) {
					Stack.pop_back();
				}
			}
		}
	}

	//find
	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;		
		if (Check[s] == Check[t]) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}
	
	return 0;
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX1 = 1e5;
const int MAX2 = 1e4;
struct node {
    int id;
    node* next;
};
node* g[MAX1];
int c;
int color[MAX1];
bool visit[MAX1];
int n,m;
void dfs(int root, int c) {
    stack<int> s;
    s.push(root);
    visit[root] = true;
    color[root] = c;
    while(!s.empty()) {
        int x = s.top();
        node* p = g[x]->next;
        int flag = 0;
        while(p != NULL) {
            if(visit[p->id]==false) {
                flag = 1;
                s.push(p->id);
                visit[p->id] = true;
                color[p->id] = c;
                break;
            }
            p = p->next;
        }
        if(flag == 0) s.pop();

    }

}

bool reach(int s, int t) {
    return color[s] == color[t];
}
int main() {
    c = 0;// 代表颜色
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        visit[i] = false;
    }
    for(int i = 0; i < n; i++) {
        g[i] = (node*)malloc(sizeof(node));
        g[i]->next = NULL;
    }
    for(int i = 0; i < m; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        node* l = (node*)malloc(sizeof(node));
        l->id = t;
        l->next = NULL;
        node* p = g[s];
        while(p->next!=NULL) p = p->next;
        p->next = l;

        node* l2 = (node*)malloc(sizeof(node));
        l2->id = s;
        l2->next = NULL;
        node* p2 = g[t];
        while(p2->next!=NULL) p2 = p2->next;
        p2->next = l2;
    }
    for(int i = 0; i < n; i++) {
        if(visit[i] == false) {
            dfs(i, c);
            c++;
        }
    }
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        if(reach(s, t)) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
    return 0;
}