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++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define ull unsigned long long
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define eps 1e-9
#define P pair<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

void YES() { cout << "YES\n"; }
void NO() { cout << "NO\n"; }
void Yes() { cout << "Yes\n"; }
void No() { cout << "No\n"; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }

int n, m;
vector<vector<int>> g;
int color[100000];

void dfs(int s, int col) {
	if (color[s] > 0)
		return;
	color[s] = col;

	for (int i = 0; i < g[s].size(); ++i) {
		dfs(g[s][i], col);
	}
}

int main() {
	int q, s, t;
	cin >> n >> m;
	g.resize(n);
	while (m--) {
		cin >> s >> t;
		g[s].push_back(t);
		g[t].push_back(s);
	}

	int col = 1;
	for (int i = 0; i < n; ++i) {
		dfs(i, col++);
	}

	cin >> q;
	while (q--) {
		cin >> s >> t;
		if (color[s] == color[t])
			yes();
		else
			no();
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 2e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

class UnionFind {
public:
    vector<int> par;
    int num;

    UnionFind(int n) : par(n, -1), num(n) {};

    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }

    int size(int x) {
        return -par[root(x)];
    }

    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (size(x) < size(y)) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        num--;
        return true;
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    rep(i, 0, m) {
        int x, y;
        cin >> x >> y;
        if (!uf.same(x, y)) uf.unite(x, y);
    }
    int q;
    cin >> q;
    vector<string> ans(q);
    rep(i, 0, q) {
        int x, y;
        cin >> x >> y;
        if (uf.same(x, y)) ans[i] = "yes";
        else ans[i] = "no";
    }

    rep(i, 0, q) cout << ans[i] << endl;
    return 0;
}

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

int x[100000][1000];
bool y[100000];
int z[100000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][z[a]]=b;
		x[b][z[b]]=a;
		z[a]++;
		z[b]++;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<50;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<z[j];k++){
						y[x[j][k]]=true;
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}

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;

SNS allfriend;

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 (now == target)
            return true;
        if (is_friend(now, target))
            return true;
    }
    return false;
}

void makefriendlist(int id, int now) {
    if (visited[now])
        return;

    visited[now] = true;
    allfriend[id].push_back(now);
    for (int i = 0; i < sns[now].size(); ++i) {
        makefriendlist(id, sns[now][i]);
    }
}

bool isfriend(int id, int target) {
    for (int i = 0; i < allfriend[id].size(); ++i)
        if (allfriend[id][i] == target)
            return true;
    for (int i = 0; i < allfriend[target].size(); ++i)
        if (allfriend[target][i] == id)
            return true;
    return false;
}

int main(void) {

    scanf("%d %d", &n, &m);
    sns.resize(n);
    allfriend.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);
    }

    for (int i = 0; i < n; ++i) {
        memset(visited, 0, sizeof(bool) * n);
        for (int j = 0; j < sns[i].size(); ++j)
            makefriendlist(i, sns[i][j]);
    }

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

    return 0;
}

C++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#include <bits/stdc++.h>
using namespace std;

vector<int> vec[100010],color(100010,-1);

void dfs(int u){
  if(color[u]==-1) color[u]=u;
  
  stack<int> s;
  s.push(u);
  while(!s.empty()){
    int r=s.top(); s.pop();
    for(auto x:vec[r]){
      if(color[x]==-1){
        color[x]=u;
        s.push(x);
      }
    }
  }
}
  

int main() {
  int n,m,q;
  cin>>n>>m;
  
  int s,t;
  rep(i,m){
    cin>>s>>t;
    vec[s].push_back(t);
    vec[t].push_back(s);
  }
  
  rep(i,n){
    if(color[i]==-1) dfs(i);
  }
  
  cin>>q;
  rep(i,q){
    cin>>s>>t;
    if(color[s]==color[t]) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  
  return 0;
}