Submission #388425

# Submission time Handle Problem Language Result Execution time Memory
388425 2021-04-11T12:34:33 Z osaaateiasavtnl Islands (IOI08_islands) C++14
90 / 100
857 ms 131076 KB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
#include <stack>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down 
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT> void re(V<T>& x) { 
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; 
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; 
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}

const int N = 1e6 + 7;
const ll INF = 1e18 + 7;
vector <ii> tree[N];
bool inc[N];
int in[N];
int n;

int to[N], len[N];

struct Queue {
int qu[N];
int l, r; // r - number of added, l - number of used
void clear() {
    l = r = 0;
}   
int size() {
    return r - l;
}   
int pop() {
    assert(size());
    l++;
    return qu[l - 1];
}   
void push(int x) {
    qu[r] = x;
    r++;
}   
} q;  

void bfs() {
    for (int i = 1; i <= n; ++i) {
        if (in[i] == 0) q.push(i);
        inc[i] = 1;
    }   
    while (q.size()) {
        int u = q.pop();
        inc[u] = 0;
        int v = to[u];
        assert(in[v] > 0);
        in[v]--;
        tree[v].app(mp(u, len[u]));
        if (in[v] == 0) {
        	q.push(v);
        } 
    }   
}

ll h[N], diam[N];
void dfs1(int u, int p) {
    for (auto e : tree[u]) {
        int v = e.x;
        dfs1(v, u);
        ckmax(diam[u], diam[v]);
        ckmax(diam[u], h[u] + h[v] + e.y);
        ckmax(h[u], h[v] + e.y);
    }   
    ckmax(diam[u], h[u]);
}   
ll pref[N];
int r[N];
int cl[N];
int s[N];
ll solve(int n) {
    ll ans = 0;
    for (int i = 0; i < n; ++i)
    	ans = max(ans, diam[cl[i]]);
    for (int i = 1; i < n; ++i) pref[i] = pref[i - 1] + len[cl[i - 1]];
    int sz = 0;
    for (int i = 0; i < n; ++i) r[i] = n;
    for (int i = 0; i < n; ++i) {
        while (sz && h[cl[s[sz - 1]]] - pref[s[sz - 1]] <= h[cl[i]] - pref[i]) {
            r[s[sz - 1]] = i;
            sz--;
        }   
        s[sz++] = i;
    }
    int ptr = 0; ll cur = 0;
    auto get = [&] (int i) {
    	if (i < n)
    		return cl[i];
    	else
    		return cl[i - n];
    };	
    for (int i = 1; i < 2 * n - 1; ++i) {
        cur += len[get(i - 1)];
        ptr = max(ptr, i - n + 1);
        while (r[ptr] != n && r[ptr] < i) ptr = r[ptr];
        ans = max(ans, h[get(i)] + cur + h[get(ptr)] - pref[ptr]);
    }   
    return ans;        
}
bool used[N]; 
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> to[i] >> len[i];
        in[to[i]]++;
    }   
    bfs();
    for (int i = 1; i <= n; ++i) {
    	if (inc[i]) {
    		dfs1(i, i);
    	}
    }
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (!used[i] && inc[i]) {
            int u = i;
            int len = 0;
            while (!used[u]) {
                used[u] = 1;
                cl[len++] = u;
                u = to[u];
            }   
            ans += solve(len);
        }
    }   
    cout << ans << '\n';
}   
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23796 KB Output is correct
3 Correct 14 ms 23772 KB Output is correct
4 Correct 14 ms 23800 KB Output is correct
5 Correct 14 ms 23884 KB Output is correct
6 Correct 14 ms 23884 KB Output is correct
7 Correct 15 ms 23884 KB Output is correct
8 Correct 14 ms 23920 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 15 ms 23884 KB Output is correct
11 Correct 13 ms 23884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23828 KB Output is correct
2 Correct 13 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23884 KB Output is correct
2 Correct 16 ms 24104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 24524 KB Output is correct
2 Correct 25 ms 25672 KB Output is correct
3 Correct 22 ms 24996 KB Output is correct
4 Correct 17 ms 24396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 26492 KB Output is correct
2 Correct 38 ms 27972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 34512 KB Output is correct
2 Correct 67 ms 32324 KB Output is correct
3 Correct 85 ms 37628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 40952 KB Output is correct
2 Correct 150 ms 50708 KB Output is correct
3 Correct 136 ms 40616 KB Output is correct
4 Correct 188 ms 58748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 257 ms 44872 KB Output is correct
2 Correct 443 ms 64708 KB Output is correct
3 Correct 237 ms 60496 KB Output is correct
4 Correct 347 ms 75712 KB Output is correct
5 Correct 290 ms 76188 KB Output is correct
6 Correct 857 ms 76292 KB Output is correct
7 Correct 299 ms 79032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 312 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -