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 <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;
  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) {
        S.push(v);
        color[v] = c;
      }
    }
  }
}

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

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

  assignColor();

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

  return 0;
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){
  bool used[m];
  for(int i=0;i<m;i++) used[i]=false; //初期化
  queue<int> Q;
  Q.push(s); used[s]=true;

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    if(k==t){return true;}
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(!used[temp]){ //未訪問なら
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  int q; cin>>q;
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s,t)){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}

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

class person
{
public:
	int id;
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		for(list<person*>::iterator temp=lis.begin();temp!=lis.end();++temp)
		{
			if ((*temp) == Per)return true;
			else if ((*temp)->Isdone == true)continue;
			else
			{
				if ((*temp)->serch(Per))return true;
			}
		}
		return false;
	}
};

person P[100001];

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)P[i].id = i;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		scanf("%d%d",&p1, &p2);
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}			 
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		scanf("%d%d", &p1, &p2);
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
vector<int> G[100005];
vector<int> color(100005, -1);
vector<bool> seen(100005, false);

void dfs(int now, int id){
    seen[now] = true;
    color[now] = id;

    for(int i=0; i<G[now].size(); ++i){
        int to = G[now][i];
        if(!seen[to]) dfs(to, id);
    }
}

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

    int id = 0;
    for(int i=0; i<n; ++i){
        if(!seen[i]){
            ++id;
            dfs(i, id);
        }
    }

    int q; cin >> q;
    for(int i=0; i<q; ++i){
        int s, t; cin >> s >> t;
        if(color[s] == color[t]) printf("yes\n");
        else printf("no\n");
    }

    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 1e5+10 ;

vector<int> s[MAXN];
int color[MAXN],n,m;

void dfs(int x,int id)
{
    stack<int> st;
    color[x] = id;
    st.push(x);
    while(!st.empty())
    {
        int u = st.top();   st.pop();
        for(int i = 0; i < s[u].size(); ++i)
        {
            int v = s[u][i];
            if(color[v] == -1)
            {
                color[v] = id;
                st.push(v);
            }
        }
    }
}
void init()
{
    int id = 0;
    memset(color,-1,sizeof color);
    for(int i = 0; i < n; ++i)
    {
        if(color[i] == -1)
            dfs(i,++id);
    }
}

int main(void)
{
    cin>>n>>m;
    int u,v;
    for(int i = 1; i <= m; ++i)
    {
        cin>>u>>v;
        s[u].push_back(v);
        s[v].push_back(u);
    }
    init();
    int q;
    cin>>q;
    while(q--)
    {
        cin>>u>>v;
        if(color[u] == color[v])
            cout<<"yes\n";
        else
            cout<<"no\n";
    }
}

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

vector<int> list[100000];
int num[100000];
int n,m;

void bfs(int s,int sign){

  queue<int> Q;
  Q.push(s);

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(num[temp]==-1){ //未訪問なら
        Q.push(temp); num[temp]=sign;
      }
    }
  }
}
int main(){

  cin>>n>>m;

  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  fill(num,num+100000,-1);
  int q; cin>>q;

  int sign=0;
  for(int i=0;i<n;i++){
    if(num[i]==-1){
      bfs(i,sign++);
    }
  }
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(num[s]==num[t]){
      cout<<"yes\n";
    }else{
      cout<<"no\n";
    }
  }

}