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 <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX=100000;
static const int NIL=-1;

int n;
int color[MAX];
vector<int> G[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);
      }
    }
  }
}

int main(){
    int m,s,t,q;

    cin>>n>>m;

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

    for(int i=0;i<n;i++){
      color[i]=NIL;
    }

    int id=1;

    for(int u=0;u<n;u++){
       if(color[u]==NIL){
         dfs(u,id++);
       }
    }


    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<stack>
#include<vector>
using namespace std;
int n, color[100001];
vector<int> G[100001];
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]==-1){
				color[v]=c;
				s.push(v);
			}
		}
	}
}

void assignColor(){
	int id=1;
	for(int i=0; i<n; i++)
		color[i]=-1;
	for(int u=0; u<n; u++){
		if(color[u]==-1){
			dfs(u, id);
			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 <vector>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define IN(v) int v;cin>>v;
#define OUT(v) cout<<v<<endl

vector<int> parent;
int root(int x) {
  if (parent[x] == x)
    return x;
  return parent[x] = root(parent[x]);
}
bool same(int x, int y) {
  return root(x) == root(y);
}
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  parent[x] = y;
}
int main() {
  IN(N); IN(M);
  parent.resize(N);
  REP(i, N)
    parent[i] = i;

  REP(i, M) {
    IN(s); IN(t);
    unite(s, t);
  }
  IN(Q);
  REP(i, Q) {
    IN(s); IN(t);
    OUT((same(s, t) ? "yes" : "no"));
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#define white 0
#define grey 1
#define nil -1
#define maxn 100000
using namespace std;

vector<int> g[maxn+5];
int color[maxn+5];
int n,m;

void dfs(int s,int t)
{
    stack<int>q;
    q.push(s);
    color[s]=t;
    while(!q.empty())
    {
        int v=q.top();
        q.pop();
        for(int j=0;j<g[v].size();j++)
        {
            int u=g[v][j];
            if(color[u]==nil)
            {
                q.push(u);
                color[u]=t;
            }
        }
    }
}

void work()
{
    int id=1;
    for(int i=0;i<n;i++)
        color[i]=nil;
    for(int i=0;i<n;i++)
        if(color[i]==nil)
           dfs(i,id++);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    work();

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

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <set>
#include <map>
using namespace std;

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

vector<bool>visited;
bool dfs(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(dfs(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    cin>>n>>m;
    g.resize(n);
    
    for(int i=0;i<m;i++){
        int s,t;
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    cin>>q;
    if(q>5)return 1;
    for(int i=0;i<q;i++){
        visited.resize(n,false);
        int s,t;
        cin>>s>>t;
        string ret =(dfs(s,t)?"yes":"no");
        cout<<ret<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 100100 ;
inline int read()
{
	int f = 1, x = 0; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + c - '0'; c = getchar();}
	return f * x;
}
int f[MAXN], n, m, q;
int getfa(int x)
{
	if (f[x] == x) return f[x];
	return f[x] = getfa(f[x]);
}
int main()
{
	n = read(); m = read();
	for (int i = 0; i < n; ++i) f[i] = i;
	for (int i = 1, u, v; i <= m; ++i)
	{
		u = read(); v = read();
		int fa1 = getfa(u);
		int fa2 = getfa(v);
		if (fa1 != fa2)
			f[fa1] = fa2;
	}
	q = read();
	for (int i = 1; i <= q; ++i)
	{
		int u, v;
		u = read(); v = read();
		int fa1 = getfa(u), fa2 = getfa(v);
		if (fa1 == fa2) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}