Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
vi graph[100];
int lineDistances[100][100];
vvb visited(10, vb(10));
vi vertexA, vertexB;
int number, diameter;
void dfs(int root, int start, int distanceInLines) {
  // commandsBeforeVisitRecursion.
  visited[root][start] = true;
  lineDistances[root][start] = distanceInLines;
  for (int next : graph[start]) {
    if (!visited[root][next]) {
      dfs(root, next, distanceInLines + 1);
    }
  }
  // commandsAfterVisitRecursion.
}
void usingDFS() {
  cin >> number >> diameter;
  int vertex0, vertex1;
  for (int i = 1; i < number; ++i) {
    cin >> vertex0 >> vertex1;
    graph[vertex0].push_back(vertex1);
    graph[vertex1].push_back(vertex0);
    vertexA.push_back(vertex0);
    vertexB.push_back(vertex1);
  }
  for (int vertex = 1; vertex <= number; ++vertex) {
    dfs(vertex, vertex, 0);
  }
  if (diameter % 2 == 0) {
    int minimum = 40000;
    for (int i = 1; i <= number; ++i) {
      int counter = 0;
      for (int j = 1; j <= number; ++j) {
        if (lineDistances[i][j] > diameter / 2) {
          ++counter;
        }
      }
      minimum = min(minimum, counter);
    }
    cout << minimum << endl;
  } else {
    int dotA, dotB, tempMaxDist, minimum = 40000;
    for (int i = 0; i < vertexA.size(); ++i) {
      int counter = 0;
      dotA = vertexA[i];
      dotB = vertexB[i];
      for (int j = 1; j <= number; ++j) {
        tempMaxDist = min(lineDistances[j][dotA], lineDistances[j][dotB]);
        if (tempMaxDist > diameter / 2) {
          ++counter;
        }
      }
      minimum = min(minimum, counter);
    }
    cout << minimum << endl;
  }
}
/*
template<typename Type>
void showContent(Type inputVector)
{
        for(int i=1; i<=number; ++i)
        {
                for(int j=1; j<=number; ++j)
                {
                        cout<<"["<<i<<"]["<<j<<"] <- "<<inputVector[i][j]<<endl;
                }
        }
        return;
}
*/
/*
template<typename NewType>
void showContent0(NewType inputVector)
{
        for(int i=0; i<inputVector.size(); ++i)
        {
                for(int j=0; j<inputVector.size(); ++j)
                {
                        cout<<"["<<i<<"]["<<j<<"] <- "<<inputVector[i][j]<<endl;
                }
        }
        return;
}
*/
/*
template<typename TypeName>
void showContent1(TypeName inputVector)
{
        for(int i=0; i<inputVector.size(); ++i)
        {
                cout<<inputVector[i]<<", ";
        }
        return;
}
*/
int main() {
  usingDFS();
  // cout<<endl;
  // showContent(visited);
  // cout<<endl;
  // showContent(lineDistances);
  // cout<<endl;
  // showContent0(visited);
  // cout<<endl;
  // showContent0(lineDistances);
  // cout<<endl;
  // showContent1(vertexA);
  // cout<<endl;
  // showContent1(vertexB);
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int size = 4000;
vi graph[size];
int lineDistances[size][size];
vvb visited(size, vb(size));
vi vertexA, vertexB;
int number, diameter;
void dfs(int root, int start, int distanceInLines) {
  // commandsBeforeVisitRecursion.
  visited[root][start] = true;
  lineDistances[root][start] = distanceInLines;
  for (int next : graph[start]) {
    if (!visited[root][next]) {
      dfs(root, next, distanceInLines + 1);
    }
  }
  // commandsAfterVisitRecursion.
}
void usingDFS() {
  cin >> number >> diameter;
  int vertex0, vertex1;
  for (int i = 1; i < number; ++i) {
    cin >> vertex0 >> vertex1;
    graph[vertex0].push_back(vertex1);
    graph[vertex1].push_back(vertex0);
    vertexA.push_back(vertex0);
    vertexB.push_back(vertex1);
  }
  for (int vertex = 1; vertex <= number; ++vertex) {
    dfs(vertex, vertex, 0);
  }
  if (diameter % 2 == 0) {
    int minimum = 40000;
    for (int i = 1; i <= number; ++i) {
      int counter = 0;
      for (int j = 1; j <= number; ++j) {
        if (lineDistances[i][j] > diameter / 2) {
          ++counter;
        }
      }
      minimum = min(minimum, counter);
    }
    cout << minimum << endl;
  } else {
    int dotA, dotB, tempMaxDist, minimum = 40000;
    for (int i = 0; i < vertexA.size(); ++i) {
      int counter = 0;
      dotA = vertexA[i];
      dotB = vertexB[i];
      for (int j = 1; j <= number; ++j) {
        tempMaxDist = min(lineDistances[j][dotA], lineDistances[j][dotB]);
        if (tempMaxDist > diameter / 2) {
          ++counter;
        }
      }
      minimum = min(minimum, counter);
    }
    cout << minimum << endl;
  }
}
/*
template<typename Type>
void showContent(Type inputVector)
{
        for(int i=1; i<=number; ++i)
        {
                for(int j=1; j<=number; ++j)
                {
                        cout<<"["<<i<<"]["<<j<<"] <- "<<inputVector[i][j]<<endl;
                }
        }
        return;
}
*/
/*
template<typename NewType>
void showContent0(NewType inputVector)
{
        for(int i=0; i<inputVector.size(); ++i)
        {
                for(int j=0; j<inputVector.size(); ++j)
                {
                        cout<<"["<<i<<"]["<<j<<"] <- "<<inputVector[i][j]<<endl;
                }
        }
        return;
}
*/
/*
template<typename TypeName>
void showContent1(TypeName inputVector)
{
        for(int i=0; i<inputVector.size(); ++i)
        {
                cout<<inputVector[i]<<", ";
        }
        return;
}
*/
int main() {
  usingDFS();
  // cout<<endl;
  // showContent(visited);
  // cout<<endl;
  // showContent(lineDistances);
  // cout<<endl;
  // showContent0(visited);
  // cout<<endl;
  // showContent0(lineDistances);
  // cout<<endl;
  // showContent1(vertexA);
  // cout<<endl;
  // showContent1(vertexB);
  return 0;
}