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++: #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;
}