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 <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define maxn 10005
#define NTL -1
int color[maxn];
vector<int> g[maxn];
stack<int> s;
int n,m,q;
void tarjan(int r,int c)
{
    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]==NTL)
            {
                color[v]=c;
                s.push(v);
            }
        }
    }
}
void assigncolor()
{
    int id=0;
    for(int i=0;i<n;i++)
        color[i]=NTL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NTL)
            tarjan(u,id++);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    assigncolor();
    scanf("%d",&q);
    while(q--)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        if(color[u]==color[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
// Welcome to my source code!

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

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

int main() {
    int s, t, m, q;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    assignColor();
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> s >> t;
        if (color[s] == color[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
#define pop onany
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.onany();
    for ( int i = 0; i < G[u].size() ; ++i) {
      int v = G[u][i];
      if ( color[v] == NIL ) {
	color[v] = c;
	S.push(v);
      }
    }
  }
}

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

  int id = 1;
  
  for ( int i = 0; i < n; ++i) {
    color[i] = NIL; //preset
  }
  
  for ( int u = 0; u < n; ++u) {
    if ( color[u] == NIL ) {
      dfs(u,id);
      id++;
    }
  }
  
  scanf("%d", &q);
  for ( int i = 0; i < q; ++i) {
    scanf("%d %d", &s, &t);
    if ( color[s] == color[t] ) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return (0);
}

C++: #include <iostream>

using namespace std;

int user[100000];

void init(int n) {
    fill_n(user, n, -1);
}

int root(int s) {
    if (user[s] < 0)
        return s;
    else
        return user[s] = root(user[s]);
}

void unite(int s, int t) {
    int rs = root(s);
    int rt = root(t);
    if (rs != rt) {
        if (user[rs] < user[rt])
            swap(rs, rt);
        user[rs] += user[rt];
        user[rt] = rs;
    }
}

bool same(int s, int t) {
    return root(s) == root(t);
}

int main() {
    int n, m, q, s, t, c = 1;
    cin >> n >> m;
    init(n);
    for (int i = 0; i < m; ++i) {
        cin >> s >> t;
        unite(s, t);
    }
    cin >> q;
    for (int i = 0; i < q; ++i) {
        cin >> s >> t;
        if (same(s, t))
            cout << "yes" << endl;
        else 
            cout << "no" << endl;
    }

    return 0;
}

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

class queue{
    int head;
    int tail;
    int size;
    int *q;
public:
    queue(int n){
        head=0;
        tail=0;
        size=n;
        q = (int *)malloc(sizeof(int)*size);
    }
    void push(int n){
        if(tail-head>=size){
            return;
        }
        q[tail%size]=n;
        tail++;
    }
    int pop(){
        if(tail<=head){
            return -1;
        }
        head++;
        return q[(head-1)%size];
    }
};

bool isFriend(int s, int t,int n,bool **f,int **kakunin){
    bool hoge;
    int tmp;
    queue ob(n);
    ob.push(s);
    for(int i=0;i<n;i++){
        hoge = true;
        while(true){
            tmp = ob.pop();
            if(tmp==-1)break;
            for(int j=0;j<n;j++){
                if(f[tmp][j]&&(!kakunin[tmp][j])){
                    ob.push(j);
                    kakunin[tmp][j] = true;
                    f[s][j] = true;
                    hoge = false;
                    if(j==t)return true;
                }
            }
        }
        if(hoge)return false;
    }
    return false;
}

int main(){
    int i,j,k,s,t,q,n,m;
    scanf("%d %d",&n,&m);
    bool **f = (bool **)malloc(sizeof(bool *)*n);
    int **kakunin = (int **)malloc(sizeof(int *)*n);
    for(i=0;i<n;i++){
        f[i] = (bool *)malloc(sizeof(bool)*n);
        kakunin[i] = (int *)malloc(sizeof(int)*n);
        memset(f+i,0,sizeof(bool)*n);
        memset(kakunin+i,0,sizeof(int)*n);
    }
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        f[s][t] = true;
        f[t][s] = true;
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
        for(j=0;j<n;j++){
            for(k=0;k<n;k++){
                kakunin[j][k] = false;
            }
        }
        scanf("%d %d",&s,&t);
        if(isFriend(s,t,n,f,kakunin))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}