Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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

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

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define floot10 cout<<fixed<<setprecision(10);
#define int long long

struct UnionFind{
  int n;
  vector<int> r,p;
  UnionFind(){}
  UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
  int find(int x){
	return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
	return find(x)==find(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);
	r[x]+=r[y];
	p[y]=x;
  }
};


signed main(){
	int n,m;
	cin>>n>>m;
	UnionFind hoge(n);
	lp(i,m){
		int a,b;
		cin>>a>>b;
		hoge.unite(a,b);
	}
	int q;
	cin>>q;
	lp(i,q){
		int a,b;
		cin>>a>>b;
		if(hoge.same(a,b)==true) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#include <cstdlib>

const int N = 100000;
int color[N];
std::vector<int> adj_list[N];

void DFS(int node_id, int c)
{
    std::stack<int> S;
    S.push(node_id);
    color[node_id] = c;

    while(!S.empty()) {
        int target = S.top();
        S.pop();
        for (int i = 0; i < adj_list[target].size(); i++) {
            int friend_id = adj_list[target][i];
            if (color[friend_id] == -1) {
                color[friend_id] = c;
                S.push(friend_id);
            }
        }
    }
}

void AssignColor(int n)
{
    int id = 1;
    for (int i = 0; i < n; i++) color[i] = -1;
    for (int i = 0; i < n; i++) {
        if (color[i] == -1) DFS(i, id++);
    }
}

int main(int argc, char *argv[])
{
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);

    // make adj matrix
    for (int i = 0; i < m; i++) {
        int node_id, friend_id;
        scanf("%d", &node_id);
        scanf("%d", &friend_id);
        adj_list[node_id].push_back(friend_id);
        adj_list[friend_id].push_back(node_id);
    }

    AssignColor(n);

    int q_num;
    scanf("%d", &q_num);
    for (int i = 0; i < q_num; i++) {
        int src, dst;
        scanf("%d", &src);
        scanf("%d", &dst);
        if (color[src] == color[dst]) fprintf(stdout, "yes\n");
        else fprintf(stdout, "no\n");
    }

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxN=100000;
int link[maxN+10];
struct bian{
    int next;
    int zhongdian;
    bian():next(-1),zhongdian(-1){}
}a[maxN+10];
int k=0;
int flag;
void dfs(int x,int y)
{

        int g=link[x];
        while(g!=-1)
        {
            if(a[g].zhongdian==y)
            {
               flag=1;
               return;
            }
            dfs(a[g].zhongdian,y);
            g=a[g].next;
        }
        return;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<maxN+10;i++)
    {
        link[i]=-1;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        a[k].zhongdian=y;
        a[k].next=link[x];
        link[x]=k;
        k++;
    }
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        flag=0;
        dfs(x,y);
       /* int g=link[x];
        while(g!=-1)
        {
            if(a[g].zhongdian==y)
            {
                flag=1;break;
            }
            g=a[g].next;
        }*/
        if(flag==1)
        {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

bool canReach(int s, int d, vector<int> &h){
	h.push_back(s);
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(find(h.begin(), h.end(), nodes[s].rel[i]) == h.end()){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		vector<int> h;
		h.clear();
		printf("%s\n", canReach(l, r, h) ? "yes" : "no");
	}
	return 0;
}

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

vector<int> G[100003];
int color[100003];
int n;

int nextPoint(int curPoint){
    int next;
    for(int i=0;i<G[curPoint].size();++i){
        next = G[curPoint][i];
        if(color[next] == NIL){
            return next;
        }
    }
    return -1;
}

void dfs(int point, int value){
    color[point] = value;
    while(1){
        int next = nextPoint(point);
        if(next != -1){
            dfs(next,value);
        }
        else{
            color[point] = value;
            return;
        }
    }
}

int main(){
    int m, s, t;
    scanf("%d %d",&n, &m);
    for(int i=0;i<m;++i){
        scanf("%d %d",&s, &t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
    for(int i=0;i<n;++i){
        color[i] = NIL;
    }
    int id = 0;
    for(int i=0;i<n;++i){
        if(color[i]==NIL){
            id += 1;
            dfs(i,id);
        }
    }
    int q;
    scanf("%d",&q);
    for(int i=0;i<q;++i){
        scanf("%d %d",&s, &t);
        if(color[s]==color[t]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}