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<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;

  string echo[q];

  for(int i = 0; i < q; i++){
    cin >> s >> t;
    if(color[s] == color[t]) echo[i] = "yes";
    else echo[i] = "no";
  }
  for(int i = 0; i < q; i++) cout << echo[i] << endl;

  return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define str string
#define ld long double
#define vec vector
#define vll vec<ll>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define ALL(a) a.begin(),a.end()
#define rALL(a) a.rbegin(),a.rend()
#define kai "\n"
#define yuque priority_queue
#define ryuque(t) priority_queue<t,vec<t>,greater<t>>
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mie min_element
#define mae max_element
#define tos to_string
#define sep setprecision
#define MOD 1000000007ll
using namespace std;
void solve();
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed;
	solve();
}

//nCk(mod p)
const ll NUM=2;
ll fac[NUM],finv[NUM],inv[NUM];
void cmodf(ll p=MOD){
	fac[0]=fac[1]=1;
	finv[0]=finv[1]=1;
	inv[1]=1;
	rep(i,2,NUM){
		fac[i]=fac[i-1]*i%p;
		inv[i]=p-inv[p%i]*(p/i)%p;
		finv[i]=finv[i-1]*inv[i]%p;
	}
}
ll cmod(ll n,ll k,ll p=MOD){
	return fac[n]*(finv[k]*finv[n-k]%p)%p;
}

//最大公約数
ll gcd(ll n,ll m){
	ll k=n%m;
	while(k!=0){n=m;m=k;k=n%m;}
	return m;
}
ll gcd(vector<ll> a){
	rep(i,1,a.size()){
		ll k=a[i-1]%a[i];
		while(k!=0){
			a[i-1]=a[i];
			a[i]=k;
			k=a[i-1]%a[i];
		}
	}
	return a.back();
}

//最小公倍数
ll lcm(ll n,ll m){
	ll t=n*m,k=n%m;
	while(k!=0){n=m;m=k;k=n%m;}
	return t/m;
}
ll lcm(vector<ll> a){
	ll n;
	rep(i,1,a.size()){
		n=a[i-1]*a[i];
		ll k=a[i-1]%a[i];
		while(k!=0){
			a[i-1]=a[i];
			a[i]=k;
			k=a[i-1]%a[i];
		}
		n/=a[i];
		a[i]=n;
	}
	return n;
}

//keyの小さい方から探していった時に初めてvalueがjoken以上になる時のkey（keyが増えたらvalueも増えるか同じになるとき）
ll value(ll key) {
	/*変更*/
	return 0;
}
ll mink(ll lkey,ll rkey/*ここも調べる*/,ll joken){
	ll mkey;
	while(lkey!=rkey){
		mkey=(lkey+rkey)/2;
		if(value(mkey)>=joken)rkey=mkey;
		else lkey=mkey+1;
	}
	return rkey;
}

//ペア配列の整理
struct pea{
	ll f,s;
	bool operator<(const pea &c)const{
		return f!=c.f?f<c.f:s<c.s;
	};
};

//変数定義

//再帰関数

//アルゴリズム
void solve(){
	ll n,m;
	cin>>n>>m;
	vec<vll> a(n);
	rep(i,0,m){
		ll s,t;cin>>s>>t;
		a[s].pub(t);
		a[t].pub(s);
	}
	vll c(n,-1);
	ll k=0;
	rep(i,0,n){
		if(c[i]==-1){
			deque<ll> b;
			b.pub(i);
			c[i]=k;
			while(b.size()!=0){
				rep(j,0,a[b.front()].size()){
					if(c[a[b.front()][j]]==-1){
						b.pub(a[b.front()][j]);
						c[a[b.front()][j]]=k;
					}
				}
				b.pof();
			}
			k++;
		}
	}
	ll q;cin>>q;
	rep(i,0,q){
		ll s,t;cin>>s>>t;
		if(c[s]==c[t])cout<<"yes"<<kai;
		else cout<<"no"<<kai;
	}
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <string.h>
#include <set>
#include <map>

using namespace std;
vector< vector<int> > v(100001);
int n, m;
int f[100001] = {0};
int r = 0;

void dfs(int a) {
	if (f[a] == 0) { f[a] = r; }
	else { return; }
	for (int i = 0;i < v[a].size();i++) {
		if (f[v[a][i]] == 0) { dfs(v[a][i]); }
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0;i < m; i++) {
		int a, b; cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}	

	for (int i = 0; i < n;i++) {
		if (f[i] == 0) { r++; }
		dfs(i);
	}

	int q; cin >> q;
	vector<string> Ans(q);
	for (int i = 0; i < q; i++) {
		int s, t; cin >> s >> t;
		if (f[s] == f[t]) { Ans[i] = "yes"; }
		else { Ans[i] = "no";}
	
	}
	for (int i = 0; i < q;i++) { cout << Ans[i] << endl; }
	return 0;
}

C++: #include <iostream>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
    d[j]=h;
    d[a]=h;
    d[b]=h;

}
else{
  d[h]=j;
    d[a]=j;
    d[b]=j;

}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int f[100005];
int find(int x){
	int xx=x;
	while(f[x]!=x)x=f[x];
	while(f[xx]!=x){
		int tem=f[xx];
		f[xx]=x;
		xx=tem;
	}
	return x;
}
void join(int x,int y){
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)f[fx]=fy;
}
int n,m;
int main(){
	cin>>n>>m;
	for(int i=0;i<=n;i++)f[i]=i;
	int a,b;
	while(m--){
		cin>>a>>b;
		join(a,b);
	}
	int q;cin>>q;
	while(q--){
		cin>>a>>b;
		if(find(a)==find(b)){
			cout<<"yes"<<endl;
		}else cout<<"no"<<endl;
	}
	return 0;
}