Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//_____
#define pb push_back
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
//_______

vector <int> G[100005];
int group[100005];
int color = 1;

void dfs(int u)
{
   if(group[u] != 0)return;
   group[u] = color;

   rep(i, G[u].size())
      dfs( G[u][i] );
}

signed main()
{
   FAST();
   int N, M;
   cin >> N >> M;

   rep(i, M){
      int a,b;
      cin >> a >> b;
      G[a].pb(b);
      G[b].pb(a);
   }

   rep(i, N){
      dfs(i);
      ++color;
   }

   int Q;
   cin >> Q;
   while(Q--){
      int a,b;
      cin >> a >> b;
      outl( group[a] == group[b] ? "yes" : "no" );
   }
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <set>

#define NMAX 100000
using namespace std;

struct Node {
  vector<int> friends;
};
int group[NMAX];

void bfs(vector<Node>& nodes, int id) {
  static int groupno = 1;
  queue<int> q;
  q.push(id);
  set<int> st;

  group[id] = groupno;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < nodes[u].friends.size(); i++) {
      int fid = nodes[u].friends[i];
      if (group[fid] == -1) {
        q.push(fid);
        group[fid] = groupno;
      }
    }
  }
  groupno++;
}

int main() {
  vector<Node> nodes;
  int nrel, nuser;

  cin >> nuser;
  cin >> nrel;
  nodes.resize(nuser);
  int tmp1, tmp2;
  for (int i = 0; i < nrel; i++) {
    cin >> tmp1 >> tmp2;
    nodes[tmp1].friends.push_back(tmp2);
    nodes[tmp2].friends.push_back(tmp1);
  }
  for (int i=0; i<nodes.size(); i++) {
    group[i] = -1;
  }

  int q, s, t;
  cin >> q;

  for (int i = 0; i < q; i++) {
    scanf("%d%d", &s, &t);
    if (group[s] != -1 && group[s] == group[t]) {
      printf("yes\n");
      continue;
    }
    bfs(nodes, s);
    printf("%s\n", group[s] == group[t] ?  "yes" : "no");
  }
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;

void solve(){
  int n, m; cin >> n >> m;
  vector<vector<int> > v(n);
  for(int i = 0; i < m; i++){
    int u, x; cin >> u >> x;
    v[u].push_back(x);
    v[x].push_back(u);
  }
  queue<int> q;
  vector<int> c(n, 0);
  int t = 1;
  for(int j = 0; j < n; j++){
    if(c[j] == 0){
      q.push(j);
      c[j] = t;
      while(q.size() != 0){
        int x = q.front();
        q.pop();
        for(int i = 0; i < v[x].size(); i++){
          if(c[v[x][i]] == 0){
            q.push(v[x][i]);
            c[v[x][i]] = t;
          }
        }
      }
    }
    t++;
  }
  int qu; cin >> qu;
  vector<string> ans(qu);
  for(int i = 0; i < qu; i++){
    int a, b; cin >> a >> b;
    if(c[a] == c[b]) ans[i] = "yes";
    else ans[i] = "no";
  }
  for(int i = 0; i < qu; i++){
    cout << ans[i] << endl;
  }
  return;
}

int main(){
  solve();
  return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

using namespace std;

static const int MAX=1000000;
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;
    rep(i,n)color[i]=nil;
    rep(i,n){
        if(color[i]==nil){
            dfs(i,id);
            id++;
        }
    }
    
    
}

int main(){
    int s,t,m,q;
    cin>>n>>m;
    rep(i,m){
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    assignColor();
    
    cin>>q;
    
    rep(i,q){
        cin>>s>>t;
        if(color[s]==color[t]){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
    }
  /*  rep(i,n){
        cout<<i<<" "<<color[i]<<endl;
    }*/
    

return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct UnionFind{
  int n;
  vector<int> p,r;
  UnionFind(){}
  UnionFind(int n):n(n){init();}
  void init(){
    p.clear();
    r.clear();
    p.resize(n);
    r.resize(n,1);
    for(int i=0;i<n;i++) p[i]=i;
  }
  int find(int x){
    return (p[x]==x?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
    x=find(x);y=find(y);
    return x==y;
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    p[y]=x;
    r[x]+=r[y];
  }
};
signed main(){
  int n,m;
  cin>>n>>m;
  UnionFind uf(n);
  for(int i=0;i<m;i++){
    int s,t;
    cin>>s>>t;
    uf.unite(s,t);
  }
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int s,t;
    cin>>s>>t;
    cout<<(uf.same(s,t)?"yes":"no")<<endl;
  } 
  return 0;
}