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