Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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;
}