Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if root(s, &group) == root(t, &group) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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


}

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[s].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>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};

void dfs (int v, int marker){
	if(c[v]!=0){
		return;
	}
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		if (c[i]==0){
			dfs(i,marker);
			marker++;
		}
	}
	
	
	cin >> q;
	int d[2][q];
	for (int i=0;i<q;i++){
		cin >> d[i][0];
		cin >> d[i][1]; 
	}
	for (int i=0;i<q;i++){
		if(c[d[i][0]]==c[d[i][1]]){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int NIL=-1;
const int MAX=100000;

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

void dfs(int v, int id){
    color[v]=id;

    for(int i=0; i<G[v].size(); i++){
        if(color[G[v][i]]==NIL){
            dfs(G[v][i], id);
        }
    }
}

void assignColor(){
    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); id++;
        }
    }
}

int main(void){
    int m; cin >> n >> m;

    for(int i=0; i<m; i++){
        int s,t; scanf("%d %d", &s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor();

    int q; cin >> q;
    for(int i=0; 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>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
	while(d[h]!=h){
		h=d[h];
	}
    d[j]=h;
    d[a]=h;
d[b]=h;
}
else{
	while(d[j]!=j){
		j=d[j];
	}
  d[h]=j;
    d[b]=j;
d[a]=j;
}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}