G - Longest Path

G - Longest Path

キーワード

解説

問題 (opens in a new tab)

edting...

提出コード

ll f(vector<ll> &dp, vector<vector<ll>> &G, int x) {
    if (dp[x] != -1) return dp[x];
    ll res = 0;
    for (auto y : G[x]) {
        res = max(res, f(dp, G, y) + 1);
    }
    return dp[x] = res;
}
 
int main() {
    ll n, m; cin >> n >> m;
    vector<vector<ll>> G(n, vector<ll>(0));
    rep(i, 0, m) {
        ll a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
    }
 
    vector<ll> dp(n, -1);
    ll ans = 0;
    rep(i, 0, n) {
        ans = max(ans, f(dp, G, i));
    }
    cout << ans << endl;
}