Submission #12213578


Source Code Expand

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define CLOCK_START clock_t chrono_clk_beg = clock()
#define CLOCK_END clock_t chrono_clk_end = clock(); cerr << (double(chrono_clk_end - chrono_clk_beg) / CLOCKS_PER_SEC) << " sec"
#define bug(args ...) cerr << __LINE__ << ">> ", err(new istringstream(string(#args)), args), cerr << '\n'
#define all(x) x.begin(), x.end()
#define INF (1LL<<30)
#define INF32 (1LL<<62)
#define F first
#define S second
#define M_PI 3.14159265358979323846
#define MOD 1000000007
#define int ll
typedef unsigned long long BITMASK; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<long long, long long> pll; typedef cc_hash_table<int, int, hash<int>> intHashTable;
struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } };
inline int powMod(int a, int b) { int x = 1; while (b > 0) { if (b&1) x = (x*a) % MOD; a = (a*a) % MOD; b >>= 1; } return x; }
inline int multiply(int x, int y) { return ((x % MOD) * (y % MOD)) % MOD; }
inline int divide(int x, int y) { return ((x % MOD) * powMod(y % MOD, MOD-2)) % MOD; }
inline int ceil(int a, int b) { return (a+b-1)/b; }
int gcd (int a, int b) { while (b) { a %= b; swap(a, b); } return a; }
int lcm (int a, int b) { return a / gcd(a, b) * b; }
int inverseMod(int a, int m) { a = a % m; for (ll x = 1; x < m; x++) if ((a * x) % m == 1) return x; return -1; }
void err(istringstream *iss) {} template<typename T, typename ... Args> void err(istringstream *iss, const T &_val, const Args & ... args) { string _name; *iss >> _name; if (_name.back()==',') _name.pop_back(); cerr << _name << " = " << _val << "; ", err(iss, args ...); }
int str_replace(string& str, const string& from, const string& to, int limit = -1) { if(from.empty()) return 0; size_t start_pos = 0; int cnt = 0; while((start_pos = str.find(from, start_pos)) != std::string::npos) { str.replace(start_pos, from.length(), to); start_pos += to.length(); cnt++; if (cnt == limit) break; } return cnt; }
template<int D, typename T> struct vec : public vector<vec<D - 1, T>> { static_assert(D >= 1, "Vector dimension must be greater than zero!");  template<typename... Args> vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)) { } }; template<typename T> struct vec<1, T> : public vector<T> { vec(int n = 0, T val = T()) : vector<T>(n, val) { }};

int n, e;
const int N = 1e5+5;
vec<1, int> adj[N];
vector<int> topologicalSort(int n, vector<int> adj[])
{
    vector<int> inDeg(n, 0);
    for (int i = 0; i < n; ++i)
        for (auto &x : adj[i]) ++inDeg[x];
    queue<int> q;
    for (int i = 0; i < n; ++i)
        if (inDeg[i] == 0) q.push(i);
    vector<int> topOrder;
    while (!q.empty())
    {
        auto cur = q.front();
        q.pop();
        topOrder.push_back(cur);
        for (auto &x : adj[cur])
        {
            --inDeg[x];
            if (inDeg[x] == 0) q.push(x);
        }
    }
    return topOrder;
}
signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> n >> e;
    while (e--)
    {
        int u, v; cin >> u >> v;
        adj[u-1].push_back(v-1);
    }
    vec<1, int> DP(n, 0);
    for (auto &u : topologicalSort(n, adj))
        for (auto &v : adj[u]) DP[v] = max(DP[v], DP[u]+1);
    cout << *max_element(all(DP)) << '\n';
    return 0;
}

Submission Info

Submission Time
Task G - Longest Path
User ankit_priyarup
Language C++14 (GCC 5.4.1)
Score 100
Code Size 3910 Byte
Status AC
Exec Time 40 ms
Memory 8440 KiB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 22
Set Name Test Cases
All 0_00, 0_01, 0_02, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09, 1_10, 1_11, 1_12, 1_13, 1_14, 1_15, 1_16, 1_17, 1_18
Case Name Status Exec Time Memory
0_00 AC 2 ms 2560 KiB
0_01 AC 2 ms 2560 KiB
0_02 AC 2 ms 2560 KiB
1_00 AC 2 ms 2560 KiB
1_01 AC 40 ms 8440 KiB
1_02 AC 17 ms 3840 KiB
1_03 AC 36 ms 7672 KiB
1_04 AC 30 ms 5712 KiB
1_05 AC 27 ms 4976 KiB
1_06 AC 24 ms 4480 KiB
1_07 AC 22 ms 4096 KiB
1_08 AC 20 ms 3968 KiB
1_09 AC 19 ms 3840 KiB
1_10 AC 17 ms 3712 KiB
1_11 AC 26 ms 4736 KiB
1_12 AC 36 ms 7244 KiB
1_13 AC 34 ms 6396 KiB
1_14 AC 27 ms 4864 KiB
1_15 AC 25 ms 4480 KiB
1_16 AC 32 ms 6004 KiB
1_17 AC 38 ms 7656 KiB
1_18 AC 29 ms 5500 KiB