Submission #19348371


Source Code Expand

Copy
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef vector<int> vi;
typedef vector<ll> vll;
 
typedef vector<vi> vvi;
typedef vector<vll> vvll;
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
typedef vector<pii> vpii;
typedef vector<pll> vpll;
 
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
 
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
 
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
 
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifdef ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
 
const char nl = '\n';

const int mxN = 2e5 + 12;
const ll INF = 1e18;

vi adj[mxN];
vi adj2[mxN];
ll a[mxN];
bool visited[mxN];

ll maksi[mxN];
ll mini[mxN];

vi topo;

void dfs(int s) {
    visited[s] = 1;
    for(auto e : adj[s]) {
        if(!visited[e])
            dfs(e);
    }
    topo.pb(s);
}

//ovaj je za maksi
//koristi adj2

void dfs2(int s) {
    visited[s] = 0;
    ckmax(maksi[s], a[s]);
    for(auto e : adj2[s]) {
        ckmax(maksi[e], maksi[s]);
        if(visited[e]) {
            dfs2(e);
        }
    }
}

void dfs3(int s) {
    visited[s] = 1;
    ckmin(mini[s], a[s]);
    for(auto e : adj[s]) {
        ckmin(mini[e], mini[s]);
        if(!visited[e]) {
            dfs3(e);
        }
    }
}

void solve() {
    //nema CIKLUSA
    //topo sort
    int n, m;
    cin >> n >> m;

    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    for(int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].pb(v);
        adj2[v].pb(u);
    }

    for(int i = 0; i < n; ++i) {
        maksi[i] = -INF;
        mini[i] = INF;
    }

    for(int i = 0; i < n; ++i) {
        if(!visited[i])
            dfs(i);
    }
    //ides od pozadi, tj. ides na maksi
    for(int i = 0; i < n; ++i) {
        if(visited[topo[i]])
            dfs2(topo[i]);
    }
    reverse(all(topo));

    //ides na mini
    for(int i = 0; i < n; ++i) {
        if(!visited[topo[i]])
            dfs3(topo[i]);
    }

    ll ans = -INF;

    for(int i = 0; i < n; ++i) {
        for(auto e : adj[i]) {
            ckmax(ans, maksi[e] - mini[i]);
        }
    }

    cout << ans << nl;
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int testCases = 1;
    //cin >> testCases;
    while(testCases--)
        solve();
 
}

Submission Info

Submission Time
Task E - Peddler
User ljuba
Language C++ (GCC 9.2.1)
Score 0
Code Size 3570 Byte
Status WA
Exec Time 182 ms
Memory 40336 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 47
WA × 2
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All extreme_00.txt, extreme_01.txt, extreme_02.txt, extreme_03.txt, handmade_00.txt, handmade_01.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_dense_00.txt, random_dense_01.txt, random_dense_02.txt, random_dense_03.txt, random_dense_04.txt, random_dense_05.txt, random_dense_06.txt, random_dense_07.txt, random_dense_08.txt, random_dense_09.txt, random_small_00.txt, random_small_01.txt, random_small_02.txt, random_small_03.txt, random_small_04.txt, random_small_05.txt, random_small_06.txt, random_small_07.txt, random_small_08.txt, random_small_09.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
extreme_00.txt AC 182 ms 40336 KB
extreme_01.txt AC 91 ms 25952 KB
extreme_02.txt AC 75 ms 25584 KB
extreme_03.txt AC 39 ms 18544 KB
handmade_00.txt AC 13 ms 12944 KB
handmade_01.txt AC 16 ms 12852 KB
random_00.txt AC 92 ms 22004 KB
random_01.txt AC 114 ms 21520 KB
random_02.txt AC 47 ms 15580 KB
random_03.txt AC 48 ms 17324 KB
random_04.txt WA 40 ms 15108 KB
random_05.txt AC 40 ms 17776 KB
random_06.txt AC 26 ms 13784 KB
random_07.txt AC 73 ms 19552 KB
random_08.txt WA 77 ms 17732 KB
random_09.txt AC 68 ms 20572 KB
random_10.txt AC 44 ms 15900 KB
random_11.txt AC 112 ms 22680 KB
random_12.txt AC 58 ms 18276 KB
random_13.txt AC 98 ms 22816 KB
random_14.txt AC 63 ms 17916 KB
random_15.txt AC 125 ms 23320 KB
random_16.txt AC 72 ms 18072 KB
random_17.txt AC 129 ms 24252 KB
random_18.txt AC 19 ms 14108 KB
random_19.txt AC 74 ms 20904 KB
random_dense_00.txt AC 29 ms 14064 KB
random_dense_01.txt AC 38 ms 14988 KB
random_dense_02.txt AC 41 ms 14900 KB
random_dense_03.txt AC 32 ms 14620 KB
random_dense_04.txt AC 13 ms 12892 KB
random_dense_05.txt AC 26 ms 13476 KB
random_dense_06.txt AC 40 ms 15116 KB
random_dense_07.txt AC 18 ms 13028 KB
random_dense_08.txt AC 42 ms 15324 KB
random_dense_09.txt AC 24 ms 13544 KB
random_small_00.txt AC 12 ms 12812 KB
random_small_01.txt AC 15 ms 12872 KB
random_small_02.txt AC 10 ms 12872 KB
random_small_03.txt AC 16 ms 12808 KB
random_small_04.txt AC 15 ms 12852 KB
random_small_05.txt AC 11 ms 12952 KB
random_small_06.txt AC 11 ms 13000 KB
random_small_07.txt AC 13 ms 12876 KB
random_small_08.txt AC 14 ms 12948 KB
random_small_09.txt AC 11 ms 12896 KB
sample_01.txt AC 10 ms 12864 KB
sample_02.txt AC 11 ms 12852 KB
sample_03.txt AC 19 ms 12924 KB