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++: #include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int Max = 100000;
static const int Nil = -1;

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

void dfs(int r, int c) {
	stack<int> S;
	S.push(r);
	color[r] = c;
	while (!S.empty()) {
		int u = S.top(); S.pop();
		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			if (color[v] == Nil) {
				color[v] = c;
				S.push(v);
			}
		}
	}
	return;
}

void assignColor() {
	int id = 1;
	for (int i = 0; i < n; i++) color[i] = Nil;
	for (int u = 0; u < n; u++) {
		if (color[u] == Nil) dfs(u, id++);
	}
	return;
}

int main() {
	int m;
	cin >> n >> m;
	int s, t;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	assignColor();
	int q;
	cin >> q;

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

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

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

void dfs(int r, int c) {
	stack<int> S;
	S.push(r);
	color[r] = c;
	int i, u, v;
	while ( !S.empty() ) {
		u = S.top();
		S.pop();
		for ( i = 0; i < G[u].size(); i++ ) {
			v = G[u][i];
			if ( color[v] == NIL ) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor() {
	int id = 1;
	int i, u;
	for ( i = 0; i < n; i++ ) color[i] = NIL;
	for ( u = 0; u < n; u++ ) {
		if ( color[u] == NIL ) dfs(u, id++);
	}
}

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

	cin >> n >> m;

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

	assignColor();

	cin >> q;

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

	return 0;
}

C++: #include <iostream>
#include <vector>

std::vector<int> v[100010];
int n;
int m;

bool visited[10010];

/**
bool dfs(int from, int to) {

  if(visited[to]) {
    return true;
  }

  visited[from] = true;
  for(int u=0; u<v[from].size(); ++u) {
    if(visited[v[from][u]]) continue;
    else {
      visited[v[from][u]] = true;
      dfs(v[from][u], to);
    }
  }

  return false;
}
**/

void dfs(int from) {
  visited[from] = true;
  for(int u=0; u<v[from].size(); ++u) {
    if(visited[v[from][u]]) continue;
    else {
      dfs(v[from][u]);
    }
  }
}

int main() {
  std::cin >> n >> m;

  for(int i=0; i<m; ++i) {
    int from, to;
    std::cin >> from >> to;
    v[from].push_back(to);
    v[to].push_back(from);
  }

  int q_n;
  std::cin >> q_n;
  bool answers[10010];
  for(int i=0; i<q_n; ++i) {
    int from, to;
    std::cin >> from >> to;
    for(int j=0; j<n; ++j) visited[j] = false;

    dfs(from);
    answers[i] = visited[to];
  }

  for(int i=0; i<q_n; ++i) {
    if(answers[i]) std::cout << "yes" << std::endl;
    else std::cout << "no" << std::endl;
  }

}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);

using namespace std;

vector<int> G[200001];
int color[200001];
int s[10000],head,tail;
void dfs(int r,int c){
  head=tail=0;
  s[tail++]=r;
  color[r]=c;
  while(head!=tail){
    int u=s[head++];
    for(int i=0;i<(int)G[u].size();i++){
      int v=G[u][i];
      // if(v!=0){
	if(color[v]==-1){
	  s[tail++]=v;
	  color[v]=c;
	}
	//}
    }
  }
}
int main(){
  int n,m,ia,ib,p,id=1;
  rep(i,200000)color[i]=-1;
  cin>>n>>p;
  rep(i,p){
    cin>>ia>>ib;
    G[ia].push_back(ib);
  }
  rep(i,p)if(color[i]==-1)dfs(i,id++);
  cin>>m;
  rep(i,m){
    cin>>ia>>ib;
    if(color[ia]==color[ib])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }

  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;

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