Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: #include <stdio.h>
#define N 100000

int A[N] = {0};

int serch(int x){
  if(x == A[x])
    return x;
  
  return A[x] = serch(A[x]);
}

int main(){
  int i, m, n, x, y, z;
  
  for(i=0; i<N; i++)
    A[i] = i;
  
  scanf("%d %d", &m, &n);
  
  for(i=0; i<n; i++){
    scanf("%d %d", &x, &y);
    if(serch(x) != serch(y))
      A[serch(y)] = serch(x);
  }
  
  scanf("%d", &z);
  
  for(i=0; i<z; i++){
    scanf("%d %d", &x, &y);
    if(serch(x) == serch(y))
      printf("yes\n");
    else
      printf("no\n");
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int friendid[100000]={0};


int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	vector< vector<int> > v;
	vector<int> u;
	int n,m,r,l,id=0,k;
	cin >> n >> m;
	for(int i=0;i<n;i++){
		u.push_back(i);
		u.push_back(0);
		v.push_back(u);
		u.pop_back();
		u.pop_back();
	}
	for(int i=0;i<m;i++){
		cin >> r >> l;
		++v[r][1];
		++v[l][1];
		v[r].push_back(l);
		v[l].push_back(r);
	}

	queue<int> q;
	for(int i=0;i<n;i++){

		if(friendid[i]==0){


			q.push(i);
			++id;
			friendid[i]=id;
			while(q.size()){
				k = q.front();
				q.pop();
				for(int j=0;j<v[k][1];j++){
					if(friendid[v[k][j+2]]==0){
						friendid[v[k][j+2]]=id;
						q.push(v[k][j+2]);
					}
				} 
			}
		}
	}


	int qn;
	cin >> qn;
	for(int i=0;i<qn;i++){
		cin>>r>>l;
		if(friendid[r]==friendid[l])cout << "yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

class Union_Find{
  public:
  Union_Find(){};
  static const ll MAX_N = 100000*2+1;
  int par[MAX_N];
  int ranks[MAX_N];
  void init(int n){
    rep(i,MAX_N){
      par[i] = i;
      ranks[i] = 0;
    }
  }
  int find(int x){
    if(par[x] == x){
      return x;
    }
    else{
      return par[x] = find(par[x]);
    }
  }
  void Unite(int a, int b){
    a = find(a);
    b = find(b);
    if(a == b) return ;
    if(ranks[a] < ranks[b]){
      par[a] = b;
    }
    else{
      par[b] = a;
      if(ranks[a] == ranks[b]) ranks[a]++;
    }
  }
  bool same(int a,int b){
    return find(a) == find(b);
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  Union_Find uf;
  ll n,m;
  cin >> n >> m;
  uf.init(n);
  ll s,t;
  rep(i,m){
    cin >> s >> t;
    uf.Unite(s,t);
  }
  ll q;
  cin >> q;
  rep(i,q){
    cin >> s >> t;
    if(uf.same(s,t)){
      cout << "yes" << endl;
    }
    else{
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector< vector<int> > M;
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

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

  cin >> n >> m;
  REP(i, n) {
    vector<int> vc;
    M.push_back(vc);
  }
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

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

int N,M;
const int maxn = 100000+5;
const int WHITE = 0;
vector<int> G[maxn];
vector<int> nodeColor;

void DFS(int u,int color){
    stack<int> nodeStack;
    nodeStack.push(u);
    while(!nodeStack.empty()){
        int u = nodeStack.top();
        nodeStack.pop();
        nodeColor[u] = color;
        for(int i = 0;i<G[u].size();i++){
            int v = G[u][i];
            if(nodeColor[v]==0) nodeStack.push(v);
        }
    }
}

int main(){
    //freopen("data2.txt","r",stdin);
    cin >> N >> M;
    nodeColor.resize(N,0);
    int u,v;
    for(int i = 0;i<M;i++){
        scanf("%d %d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int color = 1;
    for(int i = 0;i<N;i++) if(nodeColor[i]==0) DFS(i,color++);
    int q;
    scanf("%d",&q);
    for(int i = 0;i<q;i++){
        scanf("%d %d",&u,&v);
        if(nodeColor[u]==nodeColor[v]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}