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<bits/stdc++.h>
using namespace std;
vector<int> graph[100000];

int fin[100000];

void dfs(int id,int group){
    fin[id] = group;
    for(int i = 0;i < graph[id].size();i++){
        if(fin[graph[id][i]] == 0)
            dfs(graph[id][i],group);
    }
}

int main(){
    int n,m;

    scanf("%d%d",&n,&m);

    for(int i = 0;i < m;i++){
        int a,b;
        scanf("%d%d",&a,&b);

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for(int i = 0;i < n;i++){
        sort(&graph[i][0],&graph[i][graph[i].size()]);
    }

    int group = 1;
    while(true){
       int iii;
       for(iii = 0;iii < n;iii++){
           if(fin[iii] == 0){
               dfs(iii,group);
               break;
           }
       }
       if(iii == n)
          break;
       group++; 
    }

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

    for(int i = 0;i < q;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        if(fin[a] == fin[b])
            printf("yes\n");
        else
            printf("no\n");
    }
}

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

bool dfs(std::vector<std::vector<int> > &n, int from, int to) {
	std::stack<int> s;
	std::vector<int> went(n.size(), 0);

	s.push(from);
	while (s.size() != 0){
		int num = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
			if (*it == to) return true;
			if (went[*it] == 0) {
				s.push(*it);
				went[*it] = 1;
			}
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (dfs(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

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

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)

#define SORT(c) sort((c).begin(),(c).end())
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int MAX = 100000;
const int NIL = -1;

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m,q;
VI 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();
    REP0( i, G[u].size() ){
      int v = G[u][i];
      if ( color[v] == NIL ) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}


void assignColor(){
  int id=1;
  REP0(i,n) color[i]=NIL;
  REP0(u,n) if (color[u]==NIL) dfs(u, id++);
  //連結成分ごとにdfsしcoloring
}


void input(){
  std::cin >> n >> m;
  int s,t;
  REP0(i,m){
    std::cin >> s >> t;
    G[s].PB(t);
    G[t].PB(s);
  }
}


void output() {
  std::cin >> q; int s,t;
  REP0(i,q){
    std::cin >> s >> t;
    if (color[s]==color[t]) {
      std::cout << "yes" << '\n';
    }else{
      std::cout << "no" << '\n';
    }
  }
}


int main(int argc, char const *argv[]) {
  input();
  assignColor();
  output();
  return 0;
}

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

int color[MAX];
int n,m,s,t,q;
vector<int> G[MAX];

void dfs(int x, int y) {
	stack<int> S;
	S.push(x);
	color[x]=y;
	while(!S.empty()) {
		int tmp=S.top();
		S.pop();
		for(int i=0;i<G[tmp].size();i++) {
			int l=G[tmp][i];
			if(color[l]==NIL) {
				color[l]=y;
				S.push(l);
			}
		}
	}
}

void assigncolor() {
	int num=0;
	for(int i=0;i<n;i++) color[i]=NIL;
	for(int i=0;i<n;i++) {
		if(color[i]==NIL) dfs(i,num++);
	}
}

int main() {
	int i,j;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++) {
		scanf("%d %d",&s,&t);
		G[s].push_back(t);
		G[t].push_back(s);
	}
	
	assigncolor();
	
	scanf("%d",&q);
	for(i=0;i<q;i++) {
		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<stack>
using namespace std;

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

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

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

    while(!S.empty()){
        int x = S.top(); S.pop();
        for(int i = 0; i < G[x].size(); i++){
            int v = G[x][i];
            if(color[v] == NIL){
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void solve(){
    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 q, s, t;
    cin >> n >> q;

    while(q-- > 0){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    solve();

    cin >> q;
    while(q-- > 0){
        cin >> s >> t;
        if(color[s] == color[t])    cout << "yes" << endl;
        else                        cout << "no" << endl;
    }

    return 0;
}