Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if root(s, &group) == root(t, &group) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouye
//int n,m;
//int e;
//int begin[301010];
//int next[301010];
//int to[301010];

int par[101010];

int find(int x){
    if(x!=par[x])
        par[x] = find(par[x]);
    return par[x]; 
}

int unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)
		par[y]=x;
}

void init(int n){
	for(int i=0;i<=n;i++){
		par[i]=i;
	}
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	int n,m,q;
	while(cin>>n>>m){
		init(n);
		for(int i=0;i<m;i++){
			int a,b;
			cin>>a>>b;
			unite(a,b);
		}
		cin>>q;
		for(int i=0;i<q;i++){
			int a,b;
			cin>>a>>b;
			if(find(a)==find(b))
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	return 0;
}

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

const int MAX = 100000;
vector<int> friends[MAX + 1];
int groups[MAX + 1];

void dfs(int id, int gid) {
  if (groups[id] != -1) return;
  groups[id] = gid;

  int l = friends[id].size();
  for (int i = 0; i < l; i++) {
    dfs(friends[id][i], gid);
  }
}

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

  int n, m;
  cin >> n >> m;

  int a, b, q;
  int gid = 0;

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

  for (int i = 0; i < n; i++) groups[i] = -1;

  for (int i = 0; i < n; i++) {
    if (groups[i] == -1) dfs(i, gid++);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (groups[a] == groups[b]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = INT_MAX;
const int N = 100000;
const int M = 100000;
int n, m, q;
vector<edge> es[N];
int d[N];

void dijkstra(int s) {
  int i;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  q.push(PII(d[s], s));

  while (q.size()) {
    PII p = q.top();
    q.pop();
    int v = p.second;

    for (i=0; i<(int)es[v].size(); i++) {
      edge e = es[v][i];
      if (d[e.to]==-1) {
        d[e.to] = d[v];
        q.push(PII(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int i;
  for (i=0; i<N; i++) es[i].clear();
  memset(d, 0xff, sizeof(d));

  scanf("%d%d ", &n, &m);
  for (i=0; i<m; i++) {
    int u, v;
    scanf("%d%d ", &u, &v);
    es[u].psb(edge(u, v, 0));
    es[v].psb(edge(v, u, 0));
  }

  int c = 0;
  for (i=0; i<n; i++)
    if (d[i]==-1)  {
      d[i] = c++; 
      dijkstra(i);
    }

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

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
#define MAX_N 1000
int g[MAX_N][MAX_N];
int n;

bool can_connect(int s,int t) {
  bool visited[n];
  queue<int> que;
  visited[s] = true;
  que.push(s);
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(int i=0;i<n;i++) {
      if(g[v][i] != 0 && !visited[i]) {
        if(i == t) return true;
        visited[i] = true;
        que.push(i);
      }
    }
  }
  return false;
}

int main() {
  int m,s,t,q;
  scanf("%d %d\n",&n,&m);
  for(int i=0;i<m;i++) {
    scanf("%d %d",&s,&t);
    g[s][t] = 1;
    g[t][s] = 1;
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++) {
    scanf("%d %d",&s,&t);
    if(can_connect(s,t)){
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

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++) { // id別に連結リスト内を辿る
			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; // 質問用の友人s,t
	int m, q; // 友人の関係がある数ｍ、質問の個数ｑ

	cin >> n >> m; // 頂点の個数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;

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

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define MAX 100
using namespace std;

int main() {
	int i, n, m, q, a, b, gnum = 0, ptog[MAX] = {};

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		if (ptog[a] != 0)ptog[b] = ptog[a];
		else if (ptog[b] != 0)ptog[a] = ptog[b];
		else ptog[a] = ptog[b] = ++gnum;
	}

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> a >> b;
		if (ptog[a] == ptog[b])cout << "yes\n";
		else cout << "no\n";
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 1e6;
constexpr int NIL = -1;
int n;
vi G[MAX];
vi color(MAX, NIL);
inline void dfs(int r, int c) {
	deque<int> S;
	S.eb(r);
	color[r] = c;
	int u, v;
	while(!S.empty()) {
		u = S.back(); S.pop_back();
		rep(i, 0, len(G[u])) {
			v = G[u][i];
			if(color[v] == NIL) {
				color[v] = c;
				S.eb(v);
			}
		}
	}
}
inline void assignColor() {
	int id = 0;
	rep(u, 0, n) {
		if(color[u] == NIL) dfs(u, ++id);
	}
}
int main() {
	int m;
	cin >> n >> m;
	int s, t;
	rep(i, 0, m) {
		cin >> s >> t;
		G[s].eb(t);
		G[t].eb(s);
	}
	assignColor();
	int q; cin >> q;
	rep(i, 0, q) {
		cin >> s >> t;
		P(color[s] == color[t] ? "yes" : "no");
	}
	return 0;
}