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;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define IN(v) int v;cin>>v;
#define OUT(v) cout<<v<<endl

vector<int> parent;
int root(int x) {
  if (parent[x] == x)
    return x;
  return parent[x] = root(parent[x]);
}
bool same(int x, int y) {
  return root(x) == root(y);
}
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  parent[x] = y;
}
int main() {
  IN(N); IN(M);
  parent.resize(N);
  REP(i, N)
    parent[i] = i;

  REP(i, M) {
    IN(s); IN(t);
    unite(s, t);
  }
  IN(Q);
  REP(i, Q) {
    IN(s); IN(t);
    OUT((same(s, t) ? "yes" : "no"));
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#define white 0
#define grey 1
#define nil -1
#define maxn 100000
using namespace std;

vector<int> g[maxn+5];
int color[maxn+5];
int n,m;

void dfs(int s,int t)
{
    stack<int>q;
    q.push(s);
    color[s]=t;
    while(!q.empty())
    {
        int v=q.top();
        q.pop();
        for(int j=0;j<g[v].size();j++)
        {
            int u=g[v][j];
            if(color[u]==nil)
            {
                q.push(u);
                color[u]=t;
            }
        }
    }
}

void work()
{
    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()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    work();

    int q;
    scanf("%d",&q);
    for(int i=1;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>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <set>
#include <map>
using namespace std;

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

vector<bool>visited;
bool dfs(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(dfs(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    cin>>n>>m;
    g.resize(n);
    
    for(int i=0;i<m;i++){
        int s,t;
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    cin>>q;
    if(q>5)return 1;
    for(int i=0;i<q;i++){
        visited.resize(n,false);
        int s,t;
        cin>>s>>t;
        string ret =(dfs(s,t)?"yes":"no");
        cout<<ret<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 100100 ;
inline int read()
{
	int f = 1, x = 0; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + c - '0'; c = getchar();}
	return f * x;
}
int f[MAXN], n, m, q;
int getfa(int x)
{
	if (f[x] == x) return f[x];
	return f[x] = getfa(f[x]);
}
int main()
{
	n = read(); m = read();
	for (int i = 0; i < n; ++i) f[i] = i;
	for (int i = 1, u, v; i <= m; ++i)
	{
		u = read(); v = read();
		int fa1 = getfa(u);
		int fa2 = getfa(v);
		if (fa1 != fa2)
			f[fa1] = fa2;
	}
	q = read();
	for (int i = 1; i <= q; ++i)
	{
		int u, v;
		u = read(); v = read();
		int fa1 = getfa(u), fa2 = getfa(v);
		if (fa1 == fa2) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, m;
vector< list<int> > v;
vector<bool> used;

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

  cin >> n >> m;
  v = vector< list<int> >(n, list<int>(0));
  used = vector<bool>(n, false);
  for(int i = 0; i < m; i++){
    int a, b; cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int q; cin >> q;
  for(int i = 0; i < q; i++){
    used = vector<bool>(n, false);
    int a, b; cin >> a >> b;
    bool flg = false;
    queue<int> que;
    que.push(a);
    while(!que.empty()){
      int q = que.front(); que.pop();
      for(list<int>::iterator it = v[q].begin(); it != v[q].end(); it++){
        if(*it == b){
          flg = true;
          break;
        }
        if(used[*it]) continue;
        used[*it] = true;
        que.push(*it);
      }
      if(flg) break;
    }
    if(flg) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
using namespace std;
int main(){
  int n,m,t1,t2,c[100000]={}; map <int,bool> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;g[t1][t2]=1;g[t2][t1]=1;
  }
  int l;cin>>l;
  for(int i=0;i<l;i++){
    queue <int> q;
    bool f[100000]={},find=false;
    cin>>t1>>t2;
    if(c[t1]){
      if(c[t1]==c[t2]) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
      continue;
    }
    q.push(t1);f[t1]=true;c[t1]=i+1;
    for(;!q.empty();){
      for(int j=0;j<n;j++) if(g[q.front()][j]&&!f[j]){
          if(j==t2) find=true;
          f[j]=true;c[t2]=i+1;
          q.push(j);
	}
      q.pop();
      if(find) break;
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 100000
#define NIL -1

vector<int> G[MAX];
int c, top, S[MAX], color[MAX];

void dfs_init(int n){
  top = 0;
  for(int i=0; i<n; i++){
    color[i] = NIL;
  }
}

void dfs(int s){
  int v;
  color[s] = c;
  S[++top] = s;
  while(top > 0){
    s = S[top];
    top--;
    for(int i = 0; i<G[s].size(); i++){
      v = G[s][i];
      if(color[v] == NIL){
        S[++top] = v;
        color[v] = c;
      }
    }
  }
}

int main(void){
  int n, m, s, t, q;
  cin >> n >> m;
  for(int i=0; i<m; i++){
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  dfs_init(n);
  for(int i=0; i<n; i++){
    if(color[i] == NIL){
      dfs(i);
      c++;
    }
  }

  c = 0;
  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;
}