G - Longest Path

Problem Statement

There is a directed graph GG with NN vertices and MM edges. The vertices are numbered 1,2,…,N1,2,…,N, and for each ii (1≤i≤M1≤i≤M), the ii-th directed edge goes from Vertex xixi to yiyiGG does not contain directed cycles.

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

Constraints

  • All values in input are integers.
  • 2≤N≤1052≤N≤105
  • 1≤M≤1051≤M≤105
  • 1≤xi,yi≤N1≤xi,yi≤N
  • All pairs (xi,yi)(xi,yi) are distinct.
  • GG does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

NN MM
x1x1 y1y1
x2x2 y2y2
::
xMxM yMyM

Output

Print the length of the longest directed path in GG.


Sample Input 1 Copy

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

Sample Output 1 Copy

Copy
3

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

G - Longest Path


Sample Input 2 Copy

Copy
6 3
2 3
4 5
5 6

Sample Output 2 Copy

Copy
2

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

G - Longest Path


Sample Input 3 Copy

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

Sample Output 3 Copy

Copy
3

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

G - Longest Path

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod =1e9+7;
const int N = 1e5+5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
ll qpow(ll m, ll k, ll mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}      

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n+1);
    vector<int> in(n + 1),dp(n+1);
    rep(i, 1, m)
    {
        int u, v;
        cin >> u >> v;
        a[u].push_back(v);
        in[v]++;
    }
    queue<int> q;
    rep(i, 1, n)
    {
        if (!in[i])
            q.push(i);
    }
    while (!q.empty())
    {
        int f = q.front();
        q.pop();
        for (int i = 0; i < a[f].size(); i++)
        {
            int to = a[f][i];
            in[to]--;
            if (!in[to])
            {
                dp[to] = dp[f] + 1;
                q.push(to);
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}

 

上一篇:ORA-1034 ORACLE not available (转)


下一篇:Python pyQt4/pyQt5 学习笔记2(状态栏、菜单栏和工具栏)