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;

#define N 100000
#define WHITE 0  // 未訪問
#define BLACK 1  // 訪問ずみ

// 連結成分の番号を振る関数
void dfs(int n, int comp[], vector<int> G[]) {
  int i, j, vertex, neighbor, color[N];
  stack<int> S;
  // 初期状態
  for (i = 0; i < n; i++) color[i] = WHITE;

  for (i = 0; i < n; i++) {
    if (color[i] == WHITE) {
      // dfs
      S.push(i);
      while (!S.empty()) {
        vertex = S.top();
        S.pop();        
        color[vertex] = BLACK;  // 訪問済みにする
        comp[vertex] = i;  // 連結成分の番号を振る          
        for (j = 0; j < (int) G[vertex].size(); j++) {
          neighbor = G[vertex][j]; // 隣接している頂点：友人
          if (color[neighbor] == WHITE) S.push(neighbor);
        }
      }
    }
  }
}

int main() {
  int i, n, m, q, s, t;
  int comp[N];  // 連結成分の番号
  vector<int> G[N];  // グラフを表現する隣接リスト

  cin >> n >> m;
  // 隣接リストを作成
  for (i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  // グラフを連結成分に分ける
  dfs(n, comp, G);

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

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 100100;
const int NO = -1;
const int WHITE = 0, GRAY = 1, BLACK = 2;

int n, m;
vector<int> adl[MAX_N]; // 隣接リスト
int color[MAX_N], group[MAX_N];
queue<int> Q;

void init(){
    rep(i,MAX_N){
        color[i] = WHITE;
        group[i] = NO;
    }
}

void bfs(int u, int c){
    color[u] = GRAY;
    group[u] = c;
    Q.push(u);

    while(!Q.empty()){
        u = Q.front(); Q.pop();
        for(auto v: adl[u]){
            if(color[v] == WHITE){
                color[v] = GRAY;
                group[v] = c;
                Q.push(v);
            }
        }
        color[u] = BLACK;
    }
}

void bfs_visit(){
    rep(i,n){
        if(group[i] == NO) bfs(i, i);
    }
}

int main()
{
    cin >> n >> m;
    init();
    rep(i,m){
        int s, t; cin >> s >> t;
        adl[s].push_back(t);
        adl[t].push_back(s);
    }
    bfs_visit();
    int q; cin >> q;
    rep(_,q){
        int s, t; cin >> s >> t;
        if(group[s] == group[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    //rep(i,n) cout << group[i] << endl;
}

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

static const int INF=(1<<21);

int n,M[101][101];
int d[101];//最短距離を記録（到達不可能な時は、INf)

void bfs(int s){
  queue<int>q;
  q.push(s);//始点sを入れる（訪問）

  for(int i=0;i<n;i++) d[i]=INF;

  d[s]=0;
  while(!q.empty()){//空でない
    int u=q.front();//頂点を取り出す
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0) continue;//辺がない
      if(d[v]!=INF) continue; //訪問したことがある
      d[v]=d[u]+1;//vは未訪問の頂点　隣接しているためd[u]に1を足す
      q.push(v);//vを始点に
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF) ? (-1):d[i] )<<endl;
  }
}

int main(){
  int u,k,v;

  cin>>n;

  for(int i=0;i<n;i++){
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      cin>>v;
      v--;
      M[u][v]=1;
    }
  }

  bfs(0);

  return 0;
}

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

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //setup
    int nb_v; cin>>nb_v;
    int nb_e; cin>>nb_e;
    vector<int> union_tree( nb_v, -1 );
    for(int i=0; i<nb_e; ++i){
        int v1, v2; cin>>v1>>v2;
        if(v1>v2) swap(v1,v2);
        union_tree[v2] = v1;
    }
    
    //solve
    int times; cin>>times;
    for(int i=0; i<times; ++i){
        int from_v; cin>>from_v;
        int to_v; cin>>to_v;
        if (from_v<to_v) swap(from_v, to_v);
        
        int now_v = from_v;
        bool isPossible = false;
        while (now_v!=-1){
            if (union_tree[now_v] == to_v){
                isPossible = true;
                break;
            }
            now_v = union_tree[now_v];
        }
        cout<<(isPossible? "yes\n":"no\n");
    }    
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define  DFS_WHITE -1
#define  DFS_BLACK 1
using namespace std;
typedef vector<int> vi;

vector<vi> graph;
vi dfs_num;

void readGraph()
{
    string edge;
    int node1, node2;
    getline(cin, edge);

    while(getline(cin,edge))
    {
        if(edge.size() == 0)
           break;
        node1 = edge[0] - 65;
        node2 = edge[1] - 65;
        graph[node1].push_back(node2);
        graph[node2].push_back(node1);
    }
}

void print()
{
    for(int x=0; x<graph.size(); x++)
    {cout<<"Nodo : "<<x<<endl;
        for(int y=0; y<graph[x].size(); y++)
            cout<<graph[x][y]<<" ";
        cout<<endl;
    }
}

void dfs(int node)
{
    dfs_num[node] = DFS_BLACK;
    for(int c=0; c<graph[node].size(); c++)
        if(dfs_num[graph[node][c]] == DFS_WHITE)
          dfs(graph[node][c]);
}

int main()
{
string nodes;
int casos, CC, flag=0;
scanf("%d", &casos);
for(int x=0; x<casos; x++)
{
    cin>>nodes;
    graph.assign(nodes[0] - 64, vi());
    dfs_num.assign(nodes[0] - 64, DFS_WHITE);
    readGraph();
    //print();
    CC = 0;
    for(int z=0; z<dfs_num.size(); z++)
    {
        if(dfs_num[z] == DFS_WHITE)
        {
            dfs(z);
            CC++;
        }
    }
    if(flag == 0)
       flag++;
    else
        printf("\n");
    printf("%d\n", CC);
}
return 0;
}