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<bits/stdc++.h>
using namespace std;

int n,m,q,s,t;
int tree[100002];
int sn[100002];
list<int>l[100002];

int slv(int x,int id);

int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    l[s].push_back(t);
    l[t].push_back(s);
  }

  for(int i=0;i<n;i++){
    tree[i]=-1;
    sn[i]=0;
  }
  int id=1;
  for(int i=0;i<n;i++){
    if(tree[i]==-1){
      slv(i,id);
      id++;
    }
  }

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

  return(0);
}

int slv(int x,int id){
  tree[x]=id;
  sn[x]=1;
  for(int i=0;i<l[x].size();i++){
    int y=l[x].front();
    l[x].pop_front();
    l[x].push_back(y);
    if(sn[y]==0){
      slv(y,id);
    }
  }
  return(0);
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int grey=1;
const int white=0;
const int black=-1;

const int maxn=100005;
int head[maxn+5],pos=0;
int color[maxn+5],id[maxn+5];
int n,m;

struct node
{
    int k;
    int next;
};
struct node G[2*maxn+5];

void add(int u,int v)
{
    G[++pos].k=v;
    G[pos].next=head[u];
    head[u]=pos;
}

void bfsvisit(int u,int k)
{
    color[u]=black;
    queue<int> q;
    q.push(u);
    id[u]=k;
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int j=head[r];j;j=G[j].next)
        {
            int v=G[j].k;
            if(color[v]==white)
            {
                q.push(v);
                color[v]=black;
                id[v]=k;
            }
        }
    }
}

void bfs()
{
    memset(id,0,sizeof(id));
    memset(color,0,sizeof(color));
    int k=0;

    for(int i=0;i<n;i++)
    {
        if(color[i]==white)
        {
            bfsvisit(i,k++);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }

    bfs();
    int q,s,t;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&s,&t);
        if(id[s]==id[t])
            printf("yes\n");
        else printf("no\n");
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX=100000;
static const int NIL=-1;

int n,color[MAX];
vector<int>G[MAX];

void dfs(int r,int c){
  stack<int> S;
  int u,i,v;
  
  S.push(r);
  color[r]=c;

  
  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=1;
  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;
  int 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 <bits/stdc++.h>
using namespace std;

int n, m;

bool frend(vector<vector<int>> &M, int i, int t) {
  for (int z = i + 1; z < n; z++) {
    if (M[i][z] == 0) continue;
    if (M[i][z] == 1 && z == t) return true;
    if (frend(M, z, t)) {
      return true;
    } else {
      return false;
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n, vector<int>(n));

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id][f] = 1;
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  for (int i = 0; i < qn; i++) {
    bool res = frend(M, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include <bits/stdc++.h>

using namespace std;
bool frag;

vector<vector<int>> makelist(int n, int m) {
	vector<vector<int>> Adjlist(n);
	int a, b;

	for(int i = 0; i < m; i++) {
		cin >> a >> b;
		Adjlist[a].push_back(b);
		Adjlist[b].push_back(a);
	}

	return Adjlist;
}

void dfs_run(int a, int b, vector<vector<int>>& adj, vector<bool>& checker) {
	//cout << a << endl;
	while(!adj[a].empty()) {
		int tmp;
		tmp = adj[a].back();
		adj[a].pop_back();	
		if(checker[tmp] == false) {
			continue;
		}
		checker[tmp] = false;
		if(tmp == b) {
			frag = true;
		}else{
			if(frag == false) {
				dfs_run(tmp, b, adj, checker);
			}
		}
	}
}

bool dfs(int n, int a, int b, vector<vector<int>> adj) {
	vector<bool> checker(n, true);

	checker[a] = false;
	dfs_run(a, b, adj, checker);
	return frag;
}

int main(void)
{
	int n, m, q;

	cin >> n >> m;
	vector<vector<int>> Adjlist = makelist(n, m);

	cin >> q;
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;

		frag = false;
		if(dfs(n, a, b, Adjlist)) {
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}

	return 0;
}