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 <stdio.h>
#include <stack>
#include <iostream>
using namespace std;
int G[100000][100000];
int vis[100000];
int n;

bool dfs(int s, int t)
{
    if (G[s][t] == 1 || G[t][s] == 1) return true;
    vis[s] = 1;
    for (int i = 0; i < n; i++)
    {
        if (G[s][i] == 1 && vis[i] == 0)
            {
                bool a = (dfs(i,t)||dfs(t,i)); 
                vis[s] = 0;
                return a;
            }

    } 
    vis[s] = 0;
    return false;
}

int main()
{
    //freopen("in3.txt","r",stdin);
    int m;
    scanf("%d %d",&n, &m);
    for (int i = 0; i < n; i++)
    {
        vis[i] = 0; 
    } 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            G[i][j] = 0; 
        }  
    } 
    for (int i = 0; i < m; i++)
    {
        int s, t;
        scanf("%d %d",&s,&t); 
        G[s][t] = 1;
        G[t][s] = 1;
    } 
    int q,s,t;
    scanf("%d",&q);
    for (int i = 0; i < q; i++)
    {
        scanf("%d %d",&s,&t);
        if(dfs(s,t)||dfs(t,s)) printf("yes\n");
        else printf("no\n");
    } 
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>

using namespace std;

static const int N = 100000;
const int NIL = -1;

int n, m;
vector<int> G[N];
int color[N];

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

	while (!q.empty()) {
		int u = q.front(); q.pop();

		for (int i = 0; i < G[u].size(); ++i)
		{
			int v = G[u][i];
			if (color[v] == NIL){
				color[v] = c;
				q.push(v);
			}
		}
	}
}

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++);
	}
}


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

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

	assignColor();
	int q; cin >> q;
	for (int i = 0; i < q; ++i)
	{
		int s, t; cin >> s >> t;
		cout << ((color[s] == color[t]) ? "yes" : "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 (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 <algorithm>
#include <cstdio>
using namespace std;

int par[100005];
int Rank[100005];

void init(int n){
  for(int i=0;i<n;i++){
    par[i]=i;
    Rank[i]=0;
  }
}

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

void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y)return;
  if(Rank[x]<Rank[y]){
    par[x]=y;
  }else{
    par[y]=x;
    if(Rank[x]==Rank[y])Rank[x]++;
  }
}

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

int main(){
  int n,m,q,s,t;
  scanf("%d%d",&n,&m);
  init(n);
  for(int i=0;i<m;i++){
    scanf("%d%d",&s,&t);
    unite(s,t);
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++){
    scanf("%d%d",&s,&t);
    if(same(s,t))printf("yes\n");
    else printf("no\n");
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int MAX=100000;
static const int NUL=-1;
int n;
vector<int>g[MAX]; //頂点数を100000とする
                   //隣接リスト
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){
      //頂点uに隣接する頂点uを探索
      int v=g[u][i];
      if(color[v]==NUL){
	color[v]=c;
	s.push(v);	
      }
    }
  }
}
int main(){
  int s,t,m,q;
  scanf("%d %d",&n,&m);
  for(int i=0;i<m;i++){
    scanf("%d %d",&s,&t);
    g[s].push_back(t); //sからtへ辺
    g[t].push_back(s); //tからsへ辺
  }
  int id=1;
  for(int i=0;i<n;i++){
    color[i]=NUL;
  }
  for(int u=0;u<n;u++){
    if(color[u]==NUL)
      dfs(u,id++); //id(色)にかぶりがないように
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++){
    scanf("%d %d",&s,&t);
    if(color[s]==color[t]) printf("yes\n");
    else printf("no\n");
  }
  return 0;
}