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 <algorithm>
#include <vector>
using namespace std;

struct UnionFind{
    vector<int> parent;
    UnionFind(int size): parent(size, -1) {}
    
    int root(int x){
        return parent[x]<0? x: parent[x] = root(parent[x]);
    }
    void uniteSet(int x, int y){
        x = root(x);
        y = root(y);
        if (x==y) return;
        if (parent[x]>parent[y]) swap(x,y);
        //parent[x] += parent[y];
        parent[y] = x;
    }
    bool findSet(int x, int y){
        return root(x) == root(y);
    }
};

int main(){cin.tie(0);ios::sync_with_stdio(false);
    
    int nb_v, nb_lines;
    cin >>nb_v
        >>nb_lines;
    UnionFind G(nb_v);
    for(int i=0; i<nb_lines; ++i){
        int v1, v2;
        cin >>v1
            >>v2;
        G.uniteSet(v1,v2);
    }

    cin >>nb_lines;
    for(int i=0; i<nb_lines; ++i){
        int v1, v2;
        cin >>v1
            >>v2;
        cout<<(G.findSet(v1, v2)? "yes\n":"no\n");
    }    
    for(auto& a:G.parent) cout<<a<<" ";
    
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int MAX=100000;
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;
    }
}

C++: #include<stdio.h>
#include<vector>
#include<string>
using namespace std;

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

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(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(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        if(s1!=s2)return 1;
        printf((color[s]==color[t]?"yes\n":"no\n"));
    }
    return 0;
}

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

int main()
{
  int n, m, v1, v2;
  scanf("%d%d", &n, &m);
  vector<int> v[100000];

  for (int i = 0; i < m; i++)
  {
    scanf("%d%d", &v1, &v2);
    v[v1].push_back(v2);
    v[v2].push_back(v1);
  }

  int group[n];
  for (int i = 0; i < n; i++)
    group[i] = -1;

  for (int i = 0, id = 0; i < n; i++, id++)
  {
    queue<int> que;
    if (group[i] == -1)
      que.push(i);
    while (!que.empty())
    {
      int x = que.front();
      que.pop();
      group[x] = id;
      for (int j = 0; j < v[x].size(); j++)
        if (group[v[x][j]] == -1)
          que.push(v[x][j]);
    }
  }

  int q;
  scanf("%d", &q);

  for (int i = 0; i < q; i++)
  {
    scanf("%d%d", &v1, &v2);
    if (group[v1] != -1 && group[v1] == group[v2])
      printf("yes\n");
    else
      printf("no\n");
  }

  return 0;
}

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[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>
#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 ( 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;
    
    cin >> n >> m;
    
    for ( i = 0; i < m; i++ ){
        con >> s >> t;
        G.push_back(t);
        G.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;
}