Go: // AOJ ALDS1_11_D: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var nocolor = -1

type graph struct {
	graph  [][]int
	colors []int
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func newGraph(nodeNum int) *graph {
	colors := make([]int, nodeNum)
	g := make([][]int, nodeNum)
	for i := 0; i < nodeNum; i++ {
		g[i] = []int{}
		colors[i] = nocolor
	}

	return &graph{g, colors}
}

func constructGraph(sc *bufio.Scanner) *graph {
	usersNum := nextInt(sc)
	linkNum := nextInt(sc)

	g := newGraph(usersNum)
	for i := 0; i < linkNum; i++ {
		u := nextInt(sc) // node
		l := nextInt(sc) // link node
		// edge each other
		g.graph[u] = append(g.graph[u], l)
		g.graph[l] = append(g.graph[l], u)
	}
	return g
}

func (g *graph) dfs(r, color int) {
	g.colors[r] = color

	stack := list.New()
	stack.PushFront(r)

	for stack.Len() > 0 {
		v := stack.Remove(stack.Front()).(int)
		for _, u := range g.graph[v] {
			if g.colors[u] == nocolor {
				g.colors[u] = color
				stack.PushFront(u)
			}
		}
	}
}

func (g *graph) assignColor() {
	color := 1
	for u := 0; u < len(g.graph); u++ {
		if g.colors[u] == nocolor {
			g.dfs(u, color)
			color++
		}
	}
}

func (g *graph) isLink(s, t int) bool {
	return g.colors[s] == g.colors[t]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// construct graph and friendly colors
	g := constructGraph(sc)
	g.assignColor()

	// judge link
	q := nextInt(sc)
	for i := 0; i < q; i++ {
		s := nextInt(sc)
		t := nextInt(sc)

		if g.isLink(s, t) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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

void G_find(vector<vector<int> > A, vector<int> &v, int u){
	v[u] = 1;
	for(int i=0;i<v.size();i++)
		if((A[u][i] == 1) && (v[i] == 0))
			G_find(A, v, i);
}

int main(){
	int n, m, q, s, t;
	vector<vector<int> > A;
	vector<int> v;

	cin>>n>>m;
	A.resize(n);
	v.resize(n);
	for(int i=0;i<n;i++)
		A[i].resize(n);

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			A[i][j] = 0;

	for(int i=0;i<m;i++){
		cin>>s>>t;
		A[s][t] = 1;
		A[t][s] = 1;
	}

	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s>>t;
		for(int j=0;j<n;j++)
			v[j] = 0;

		G_find(A, v, s);

		if(v[s] == v[t])
			cout<<"yes"<<endl;
		else
			cout<<"no"<<endl;
	}

	return 0;
}

C++: 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<vector>

#define NIL -1
int color[100000];
std::vector<int>V[100005];

void dfs(int i,int id){
    std::stack<int>S;
    S.push(i);
    color[i]=id;
    int j;
    while(!S.empty()){
        int top=S.top();
        S.pop();
        for(j=0;j<V[top].size();j++){
            if(color[V[top][j]]==NIL){
                color[V[top][j]]=id;
                S.push(V[top][j]);
            }
        }
    }
}
int main(){
    int n,m,i;
    scanf("%d %d",&n,&m);
    
    for(i=1;i<=m;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        V[s].push_back(t);
        V[t].push_back(s);
    }
    for(i=0;i<=n-1;i++)color[i]=NIL;
    int id=1;
    for(i=0;i<=n-1;i++){
        if(color[i]==NIL){
            id++;
            dfs(i,id);
        }
    }
    
    int q;
    scanf("%d",&q);
    for(i=1;i<=q;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        if(color[s]==color[t])printf("yes");
        else printf("no");
        printf("\n");
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int cnt=1;
struct Point{
	int x;
	int y;
};

bool depth(vector< vector<int> > &vv,vector<int> &f,vector<int> &c,int num1,int num2){
	
	f[num1]=cnt;
	cnt++;
	
	for(int i=0; i<vv[num1].size(); i++){
		if(vv[num1][i]!=0 && f[i]==0){
			depth(vv,f,c,i,num2);
		}
	}

	if(f[num2]!=0){
		return 1;
	}else{
		return 0;
	}	

}
int main(){
	int n,m,l;
	cin>>n>>m;
	vector< vector<int> > v(n,vector<int> (n,0));
	vector<int> ft(n,0);
	vector<int> ct(n,0);
	for(int i=0; i<m; i++){
		Point a;
		cin>>a.x>>a.y;
		v[a.x][a.y]=1;
	}
	cin>>l;
	vector<string> ans(l);
	for(int i=0; i<l; i++){
		Point b;
		cin>>b.x>>b.y;
		if(depth(v,ft,ct,b.x,b.y)){
			ans[i]="yes";
		}else{
			ans[i]="no";
		}
	}
	for(int i=0; i<l; i++){
		cout<<ans[i]<<endl;
	}
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <unordered_set>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< unordered_set<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< unordered_set<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).insert(t);
		friendGraphList.at(t).insert(s);
	}
}

bool checkFriendAccesibility(const vector<unordered_set<int>>& graphList, const int s, const int t) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	//vector<int> distanceList(n, -1);//
	vector<bool> isVisitList(n, false);//距離に興味はないのでbool
	nodeQueue.push(s);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		isVisitList.at(id).flip();//trueにする
		const auto& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			if (isVisitList.at(adjID))continue;
			if (graphList.at(adjID).find(id) == graphList.at(adjID).end())continue;
			if (adjID == t)return true;
			nodeQueue.push(adjID);
		}
		nodeQueue.pop();
	}
	return false;
}

template<typename T>
void execute(T& cin) {
	vector< unordered_set<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << (checkFriendAccesibility(graphList, s, t) ? "yes" : "no") << endl;
	}
}

int main()
{
	execute(cin);
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>

typedef struct{
    int height;
    int parent;
}node;

node* nd;

int top(int key){
    if(nd[key].parent == -1)return key;
    else return top(nd[key].parent);
}

void join(int s, int t){
    int a = top(s);
    int b = top(t);
    if(a==b)return;
    if(nd[a].height<nd[b].height){
        nd[a].parent = b;
    }else{
        nd[b].parent = a;
        if(nd[a].height==nd[b].height)nd[a].height++;
    }
    return;
}

bool isFriend(int s, int t){
    if(top(s)==top(t))return true;
    else return false;
}

int main(){
    int i, n, m, s, t, q;
    scanf("%d %d",&n,&m);

    nd = (node *)malloc(sizeof(node)*n);

    for(i=0;i<n;i++){
        nd[i].height = 1;
        nd[i].parent = -1;
    }

    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        join(s,t);
    }

    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(s,t))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}