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++: /* 
 * ALDS1_11B.cpp
 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

using Friends = vector<int>;
using SNS = vector<Friends>;

int n, m, q;
bool visited[100000];
SNS sns;

bool is_friend(int id, int target) {
    if (visited[id])
        return false;

    visited[id] = true;
    for (int i = 0; i < sns[id].size(); ++i) {
        int now = sns[id][i];
        if (is_friend(now, target))
            return true;
    }
    return false;
}

int main(void) {

    scanf("%d %d", &n, &m);
    sns.resize(n);
    for (int i = 0, id, f; i < m; ++i) {
        scanf("%d %d", &id, &f);
        sns[id].push_back(f);
        sns[f].push_back(id);
    }

    scanf("%d", &q);
    for (int i = 0, id, f; i < q; ++i) {
        scanf("%d %d", &id, &f);
        memset(visited, 0, sizeof(bool) * n);
        if (is_friend(id, f))
            printf("yes\n");
        else
            printf("no\n");
    }

    return 0;
}

C++: //   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
using ll=long long;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=1e9;
const ll INF=1e18;
using P=pair<int,int>;
int par[100005]={};
int find(int x) {
  if(par[x]==x) {
    return x;
  }
  return par[x]=find(par[x]);
}
bool same(int x,int y) {
  return par[x]==par[y];
}
void unit(int x,int y) {
  x=find(x),y=find(y);
  if(x!=y) {
    par[x]=y;
  }
}
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  int n,m,s,t,q;
  cin >> n >> m;
  for(int i=0; i<n; i++) {
    par[i]=i;
  }
  for(int i=0; i<m; i++) {
    cin >> s >> t;
    unit(s,t);
  }
  cin >> q;
  while(q--) {
    cin >> s >> t;
    if(find(s)==find(t)) {
      cout << "yes" << "\n";
    }
    else {
      cout << "no" << "\n";
    }
  }
}

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