Problem:
Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define BUF_SIZE 30

// size: specify sizeof(str)
int get_str(char *str, int size) {
    if(!fgets(str, size, stdin)) return -1;
    return 0;
}

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}

int get_int2(int *a1, int *a2) {
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return -1;
  sscanf(line, "%d %d", a1, a2);
#else
#error
#endif
  return 0;
}

struct list {
    int dst;
    struct list *next;
};

#define VERTEXES_MAX 100000

struct list *create_graph(int vs) {
    static struct list graph[VERTEXES_MAX];
    int i;
    for(i = 0; i < vs; i++) {
        graph[i].next = &graph[i];
    }
    return graph;
}
void add_list(struct list *head, int v) {
    struct list *elem = malloc(sizeof(struct list));
    elem->dst = v;
    elem->next = head->next;
    head->next = elem;
}

#define max(a,b) ((a) > (b) ? (a) : (b))

struct pair {
    int v;
    int length;
};

static struct pair queue[VERTEXES_MAX*10];
static int start;
static int end;

int is_empty(void) {
    return start == end;
}

void enqueue(struct pair p) {
    queue[start++] = p;
}

struct pair dequeue(void) {
    return queue[end++];
}

int main(void) {
    int vs, es;
    get_int2(&vs, &es);
    struct list *graph = create_graph(vs);
    int i;
    static char cands[VERTEXES_MAX];
    memset(cands, 0x01, VERTEXES_MAX);
    for(i = 0; i < es; i++) {
        int v1, v2;
        get_int2(&v1, &v2);
        // 1-indexed -> 0-indexed
        v1--; v2--;
        add_list(&graph[v1], v2);
        cands[v2] = 0;
    }

    for(i = 0; i < vs; i++) {
        // if starting point
        struct pair p = {i, 0};
        if(cands[i]) enqueue(p);
    }

    // bfs
    int dp[VERTEXES_MAX] = {0};
    while(!is_empty()) {
        struct pair p = dequeue();
        struct list *head = &graph[p.v];
        struct list *elem;
        for(elem = head->next; elem != head; elem = elem->next) {
            int dst = elem->dst;
            struct pair np = {dst, p.length+1};
            if(dp[dst] < np.length) {
                enqueue(np);
                dp[dst] = np.length;
            }
        }
    }
    int ans = 0;
    for(i = 0; i < vs; i++) {
        ans = max(ans, dp[i]);
    }
    printf("%d\n", ans);

    return 0;
}