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++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX1 = 1e5;
const int MAX2 = 1e4;
struct node {
    int id;
    node* next;
};
node* g[MAX1];
int c;
int color[MAX1];
bool visit[MAX1];
int n,m;
void dfs(int root, int c) {
    stack<int> s;
    s.push(root);
    visit[root] = true;
    color[root] = c;
    while(!s.empty()) {
        int x = s.top();
        node* p = g[x]->next;
        int flag = 0;
        while(p != NULL) {
            if(visit[p->id]==false) {
                flag = 1;
                s.push(p->id);
                visit[p->id] = true;
                color[p->id] = c;
                break;
            }
            p = p->next;
        }
        if(flag == 0) s.pop();

    }

}

bool reach(int s, int t) {
    return color[s] == color[t];
}
int main() {
    c = 0;// 代表颜色
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        visit[i] = false;
    }
    for(int i = 0; i < n; i++) {
        g[i] = (node*)malloc(sizeof(node));
        g[i]->next = NULL;
    }
    for(int i = 0; i < m; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        node* l = (node*)malloc(sizeof(node));
        l->id = t;
        l->next = NULL;
        node* p = g[s];
        while(p->next!=NULL) p = p->next;
        p->next = l;

        node* l2 = (node*)malloc(sizeof(node));
        l2->id = s;
        l2->next = NULL;
        node* p2 = g[t];
        while(p2->next!=NULL) p2 = p2->next;
        p2->next = l2;
    }
    for(int i = 0; i < n; i++) {
        if(visit[i] == false) {
            dfs(i, c);
            c++;
        }
    }
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        if(reach(s, t)) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
    return 0;
}

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

using namespace std;
#define WHITE 0 //未到達
#define GLAY 1 //訪問中
#define BLACK 2 //訪問完了
#define INF 1000000001


void dfs(const vector<vector<int> > &adj_list,
     const int id, vector<int> &color,const int color_id){
    color[id] = color_id;
    for(auto itr = adj_list[id].begin();itr != adj_list[id].end(); itr++){
        int v = *itr;
        if(color[v] == -1){
            color[v] = color_id;
            dfs(adj_list,v,color,color_id);
        }
    }
    return;
}

void assignColor(const vector<vector<int> > &adj_list, vector<int> &color,const int n){
    int color_id = 0;
    for(int u = 0; u < n; u++){
        if(color[u] == -1){
            dfs(adj_list,u,color,color_id);
            color_id++;
        }
    }
}


int main(){
    int n,m;
    int s,t;
    int q;

    cin >> n >> m;
    vector<vector<int> > adj_list(n);
    vector<int> color(n,-1);


    for(int i = 0; i < m; i++){
        cin >> s >> t;
        adj_list[s].push_back(t);
        adj_list[t].push_back(s);
    }

    assignColor(adj_list,color,n);

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

    return 0;
}

C++: # include <iostream>
# include <cstdio>
using namespace std;
int *a;
int find_top(int i)
{
	while(a[i] != i) i = a[i];
	return i;
}
void union_find(int x,int y)
{
	a[x] = find_top(x);
	a[y] = find_top(y);
	a[a[x]] = a[y];
}
int main(void)
{
	int n,m,u,v;
	scanf("%d %d",&n,&m);
	a = new int [n];
	for(int i = 0;i < n;i++) a[i] = i;
	while(m--)
	{
		scanf("%d %d",&u,&v);
		union_find(u,v);
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d %d",&u,&v);
		a[u] = find_top(u); a[v] = find_top(v);
		printf("%s\n",a[u] == a[v]?"yes":"no");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int CRL = -1;
int v[N]={0};
void ketugou(int k,int l,int n){
	int x,mn;
	x = max(k,l);
	mn = min(k,l);
	for(int i=0;i<n;i++){
		if(v[i]==x)v[i]=mn;
	}
}

int main(){
	int n,m,q;
	memset(v, CRL, sizeof(v));
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL && v[l]==CRL){
			v[k] = v[l] = min(k,l);
		}else if(v[k]!=CRL && v[l]!=CRL){
			ketugou(k,l,n);
		}else if(v[k]==CRL){
			v[k]=v[l];
		}else{
			v[l]=v[k];
		}
	}
	cin >> q;
	for(int i=0;i<q;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL || v[l]==CRL) cout << "no" << endl;
		else if(v[k]==v[l])cout << "yes" << endl;
		else cout << "no" << endl;
	}
    return 0;
}