Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 100100;
const int NO = -1;
const int WHITE = 0, GRAY = 1, BLACK = 2;

int n, m;
vector<int> adl[MAX_N]; // 隣接リスト
int color[MAX_N], group[MAX_N];
queue<int> Q;

void init(){
    rep(i,MAX_N){
        color[i] = WHITE;
        group[i] = NO;
    }
}

void bfs(int u, int c){
    color[u] = GRAY;
    group[u] = c;
    Q.push(u);

    while(!Q.empty()){
        u = Q.front(); Q.pop();
        for(auto v: adl[u]){
            if(color[v] == WHITE){
                color[v] = GRAY;
                group[v] = c;
                Q.push(v);
            }
        }
        color[u] = BLACK;
    }
}

void bfs_visit(){
    rep(i,n){
        if(group[i] == NO) bfs(i, i);
    }
}

int main()
{
    cin >> n >> m;
    init();
    rep(i,m){
        int s, t; cin >> s >> t;
        adl[s].push_back(t);
        adl[t].push_back(s);
    }
    bfs_visit();
    int q; cin >> q;
    rep(_,q){
        int s, t; cin >> s >> t;
        if(group[s] == group[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    //rep(i,n) cout << group[i] << endl;
}

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

static const int INF=(1<<21);

int n,M[101][101];
int d[101];//最短距離を記録（到達不可能な時は、INf)

void bfs(int s){
  queue<int>q;
  q.push(s);//始点sを入れる（訪問）

  for(int i=0;i<n;i++) d[i]=INF;

  d[s]=0;
  while(!q.empty()){//空でない
    int u=q.front();//頂点を取り出す
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0) continue;//辺がない
      if(d[v]!=INF) continue; //訪問したことがある
      d[v]=d[u]+1;//vは未訪問の頂点　隣接しているためd[u]に1を足す
      q.push(v);//vを始点に
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF) ? (-1):d[i] )<<endl;
  }
}

int main(){
  int u,k,v;

  cin>>n;

  for(int i=0;i<n;i++){
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      cin>>v;
      v--;
      M[u][v]=1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //setup
    int nb_v; cin>>nb_v;
    int nb_e; cin>>nb_e;
    vector<int> union_tree( nb_v, -1 );
    for(int i=0; i<nb_e; ++i){
        int v1, v2; cin>>v1>>v2;
        if(v1>v2) swap(v1,v2);
        union_tree[v2] = v1;
    }
    
    //solve
    int times; cin>>times;
    for(int i=0; i<times; ++i){
        int from_v; cin>>from_v;
        int to_v; cin>>to_v;
        if (from_v<to_v) swap(from_v, to_v);
        
        int now_v = from_v;
        bool isPossible = false;
        while (now_v!=-1){
            if (union_tree[now_v] == to_v){
                isPossible = true;
                break;
            }
            now_v = union_tree[now_v];
        }
        cout<<(isPossible? "yes\n":"no\n");
    }    
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define  DFS_WHITE -1
#define  DFS_BLACK 1
using namespace std;
typedef vector<int> vi;

vector<vi> graph;
vi dfs_num;

void readGraph()
{
    string edge;
    int node1, node2;
    getline(cin, edge);

    while(getline(cin,edge))
    {
        if(edge.size() == 0)
           break;
        node1 = edge[0] - 65;
        node2 = edge[1] - 65;
        graph[node1].push_back(node2);
        graph[node2].push_back(node1);
    }
}

void print()
{
    for(int x=0; x<graph.size(); x++)
    {cout<<"Nodo : "<<x<<endl;
        for(int y=0; y<graph[x].size(); y++)
            cout<<graph[x][y]<<" ";
        cout<<endl;
    }
}

void dfs(int node)
{
    dfs_num[node] = DFS_BLACK;
    for(int c=0; c<graph[node].size(); c++)
        if(dfs_num[graph[node][c]] == DFS_WHITE)
          dfs(graph[node][c]);
}

int main()
{
string nodes;
int casos, CC, flag=0;
scanf("%d", &casos);
for(int x=0; x<casos; x++)
{
    cin>>nodes;
    graph.assign(nodes[0] - 64, vi());
    dfs_num.assign(nodes[0] - 64, DFS_WHITE);
    readGraph();
    //print();
    CC = 0;
    for(int z=0; z<dfs_num.size(); z++)
    {
        if(dfs_num[z] == DFS_WHITE)
        {
            dfs(z);
            CC++;
        }
    }
    if(flag == 0)
       flag++;
    else
        printf("\n");
    printf("%d\n", CC);
}
return 0;
}

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

int fri[100010];

int find(int x){
  if( fri[x] == x ) return x;
  else return fri[x] = find(fri[x]);
}

bool same(int x, int y){
  return find(x) == find(y);
}

void uf_union(int x, int y){
  x = find(x);
  y = find(y);
  
  fri[x] = y;

}

int main(){

  int n,m,q;
  cin >> n >> m;

  // init
  for(int i=0; i<n; i++) fri[i] = i;

  //
  for(int i=0; i<m; i++){
    int u,v; scanf("%d %d",&u,&v);
    uf_union(u,v);
  }

  cin >> q;
  for(int i=0; i<q; i++){
    int u,v; scanf("%d %d",&u,&v);
    if( find(u) == find(v) ) cout << "yes\n";
    else cout << "no\n";
  }


}