Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    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++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        for(int j=0;j<n;j++)visited[i]=false;
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        if(s1!=s2&&s1=="yes")return 1;
        cout<<s2<<endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<queue>
#include<vector>
#define N_MAX 100005
using namespace std;
struct Node{
    vector<int> vct;
    char type;
    char state;//0:undiscovered
};
vector<Node> nodes;
int n;
void bfs(){
    queue<int> Q;
    char type=1;
    for(int i=0;i<n;i++){
        if(nodes[i].state==0){
            Q.push(i);
            nodes[i].type = type;
            nodes[i].state = 1;
            while(!Q.empty()){
                int active = Q.front();
                int total = nodes[active].vct.size();
                for(int i=0;i<total;i++){
                    int newIndex=nodes[active].vct[i];
                    if(nodes[newIndex].state==0){
                        Q.push(newIndex);
                        //printf("== %d -> %d ==\n", active, newIndex);
                        nodes[newIndex].type = type;
                        nodes[newIndex].state = 1;
                    }
                }
                Q.pop();
            }
            type++;
        }
    }
}
int main(){
    int rel;
    scanf("%d%d", &n, &rel);
    for(int i=0;i<n;i++){//initial
        Node init;
        init.type=0;
        init.state=0;
        nodes.push_back(init);
        //printf("initial info: %d %d\n", init.type, init.state);
    }
    for(int i=0;i<rel;i++){
        int maj, sub;
        scanf("%d%d", &maj, &sub);
        nodes[maj].vct.push_back(sub);
        nodes[sub].vct.push_back(maj);//Undirected
    }
    bfs();
    short ques;
    scanf("%d", &ques);
    for(short i=0;i<ques;i++){
        int maj, sub;
        scanf("%d%d", &maj, &sub);
        if(nodes[maj].type==nodes[sub].type){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }

    /*printf("\n============ now testing ================\n");
    for(int i=0;i<n;i++){
        printf("\t-- %d: type:%d state:%d --\n", i, nodes[i].type, nodes[i].state);
    }*/
    return 0;
}

C++: #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;
    int i, u, v;
    while ( !S.empty() ) {
        u = S.top();
        S.pop();
        for ( i = 0; i < G[u].size(); i++ ) {
            v = G[u][i];
            if ( color[v] == NIL ) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}
  
void assignColor() {
    int id = 0;
    int i, u;
    for ( i = 0; i < n; i++ ) color[i] = NIL;
    for ( u = 0; u < n; u++ ) {
        if ( color[u] == NIL ) dfs(u, id++);
    }
}
  
int main() {
    int s, t, m, q, i;
  
    cin >> n >> m;
  
    for ( i = 0; i < m; i++ ) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
  
    assignColor();
  
    cin >> q;
  
    for ( i = 0; i < q; i++ ) {
        cin >> s >> t;
        if ( color[s] == color[t] ) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
  
    return 0;
}

C++: #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;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 100005

using namespace std;
int n, m, q;
vector<int> A[Lens];
queue<int>	Q;
char c[Lens], d[Lens];

int Find(int s, int t)
{
	Q.push(s);
	c[s] = 'g';
	while (!Q.empty())
	{
		s = Q.front();
		Q.pop();
		c[s] = 'g';
		for (int j = 0; j < A[s].size(); j++)
		{
			if (c[A[s][j]] == 'w')
				Q.push(A[s][j]);
			if (A[s][j] == t)
			{
				return 1;
			}
		}
	}
return 0;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		d[s] = d[t] = 'w';
		A[s].push_back(t);
		A[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		for (int j = 0; j < n; j++)
			c[j] = d[j];
		int s, t;
		cin >> s >> t;
		if (Find(s, t))
			cout << "yes" << endl;

		else
			cout << "no" << endl;
	}
	return 0;
}