Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include<iostream>
#include<vector>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, color = 0;
	bool answer_yes = false;

	// enter
	cin >> n >> m;
	vector< vector<int> > Adj(n);
	vector<int> Stack;
	vector<int> Check(n, 0);
	vector<int>::iterator it, itend;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].push_back(t);
		Adj[t].push_back(s);
	}
	/*for (i = 0; i < n; i++) {
		cout << "\n" << i << " : " << flush;
		for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	// calculate
	for (i = 0; i < n; i++) {
		if (Check[i] == 0) {
			color++;
			Stack.push_back(i);
			Check[i] = color;
			while (!Stack.empty()) {
				id = Stack.back();
				//cout << "\nid : " << id << " color : " << color << endl;
				itend = Adj[id].end();
				for (it = Adj[id].begin(); it != itend; it++) {
					if (!Check[*it]) {
						Stack.push_back(*it);
						Check[*it] = color;
						break;
					}
				}
				if (it == itend) {
					Stack.pop_back();
				}
			}
		}
	}

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

C++: #include <stdio.h>
int min(int a,int b){
    if(a>b)return b;
    return a;
}
int max(int a,int b){
    if(a>b)return a;
    return b;
}
int main(){
    int n,m,q,x,y,tree[100000],temp,a;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)tree[i]=i;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        temp=max(tree[x],tree[y]);
        a=min(tree[x],tree[y]);
        for(int i=0;i<n;i++){
            if(tree[i]==temp)tree[i]=a;
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d%d",&x,&y);
        if(tree[x]==tree[y])printf("yes\n");
        else printf("no\n");
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

using Lint=long long;
using Graph=vector<vector<int>>;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N,M; cin>>N>>M;
    Graph G(N);
    for(int i=0;i<M;i++){
        int a,b; cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<int> color(N,-1);
    int c=0;
    function<void(int)> dfs=[&](int v)
    {
        color[v]=c;
        for(int u:G[v]) if(color[u]==-1){
            dfs(u);
        }
    };
    for(int i=0;i<N;i++) if(color[i]==-1) dfs(i),c++;

    int Q; cin>>Q;
    while(Q--){
        int s,t; cin>>s>>t;
        cout<<(color[s]==color[t]? "yes":"no")<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

static const int N = 100;

int n, e, m[N][N];
int color[N], dist;


void dfs(int i, int c){
	queue<int> q; q.push(i);
	color[i] = c;

	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int v = 0; v < n; ++v)	{
			if (-1 == color[v] && m[u][v]){
				color[v] = c;
				q.push(v);
			}
		}
	}
}


void search(){
	for (int i = 0; i < n; ++i) color[i] = -1;

	int c = 0;
	for (int i = 0; i < n; ++i)	{
		if (color[i] == -1) dfs(i, c++);
	}
}


int main()
{
	cin >> n >> e;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) m[i][j] = 0;
	}

	for (int i = 0; i < e; ++i) {
		int u, v; cin >> u >> v;
		m[u][v] = 1;
	}

	search();

	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, v; cin >> u >> v;
		if (color[u] == color[v]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int pa[MAX];
int ra[MAX];
void init(){
  for(int i=0;i<MAX;i++){
    pa[i]=i;
    ra[i]=0;
  }
}
 
int find(int x){
  if(pa[x]==x)return x;
  else return pa[x]=find(pa[x]);
}
 
void unite(int x,int y){
  x=find(x),y=find(y);
  if(x==y)return;
  if(ra[x]<ra[y])swap(x,y);
  pa[y]=x;
  if(ra[x]==ra[y])ra[x]++;
}
 
bool same(int x,int y){
  return (find(x)==find(y));
}
 
int n,m,q,a,b;
int main(){
  init();
  scanf("%d %d",&n,&m);
  for(int i=0;i<m;i++){
    scanf("%d %d",&a,&b);
    unite(a,b);
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++){
    scanf("%d %d",&a,&b);
    cout<<(same(a,b)?"yes":"no")<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector< vector<int> > list;
vector<int> group;

void bfs(int v, int g) {
  if (group[v] != 0) {
    return;
  }
  group[v] = g;
  for (int i = 0; i < list[v].size(); i++) {
    bfs(list[v][i], g);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

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

  list = vector< vector<int> >(n, vector<int>());
  group = vector<int>(n,0);
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  int g = 1;
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      bfs(i, g);
      g++;
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (group[s] == group[t]) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  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(j!=h){
    d[j]=h;
    d[a]=h;
    d[b]=h;
d[d[a]]=h;
d[d[b]]=h;
}
}
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;
}

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

const int N = 2e5;
int used[N];
int head[N];
vector<int> fr[N];
int n, m, a, b;

void dfs(int rt, int now){
	used[now] = 1;
	head[now] = rt;
	for(int i=0; i<fr[now].size(); i++){		
		if(used[fr[now][i]] == 0){
			dfs(rt, fr[now][i]);
		}		
	}
}

void Sovle(){
	for(int i=0; i<n; i++){
		if(used[i]==0)dfs(i,i);
	}
}

void Output(){
	cin>>m;
	for(int i=0; i<m; i++){
		cin>>a>>b;
		if(head[a]==head[b])cout<<"yes\n";
		else cout<<"no\n";
	}
}

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

int main(){
	Input();
	Sovle();
	Output();
	return 0;
}