Submission #388417

# Submission time Handle Problem Language Result Execution time Memory
388417 2021-04-11T12:15:48 Z osaaateiasavtnl Islands (IOI08_islands) C++14
90 / 100
1042 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_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e6 + 7;
const ll INF = 1e18 + 7;
vector <ii> g[N];
bool inc[N];
int pw[N];
int n;
void bfs() {
    queue <int> q;
    for (int i = 1; i <= n; ++i) {
        pw[i] = g[i].size();
        if (pw[i] == 1) q.push(i);
        inc[i] = 1;
    }   
    while (q.size()) {
        int u = q.front(); q.pop();
        inc[u] = 0;
        for (auto e : g[u]) {
            int v = e.f;
            --pw[v];
            if (pw[v] == 1) q.push(v);
        }   
    }   
}
int to[N], len[N];
ll h[N], diam[N];
pair <ll, ll> dfs1(int u, int p) {
    pair <ll, ll> ans = {0, 0};
    for (auto e : g[u]) {
        int v = e.f;
        if (v == p || inc[v]) continue;
        auto t = dfs1(v, u);
        ans.s = max(ans.s, t.s);
        ans.s = max(ans.s, ans.f + t.f + e.s);
        ans.f = max(ans.f, t.f + e.s);
    }   
    ans.s = max(ans.s, ans.f);
    return ans;
}   
ll pref[N];
int r[N];
int cl[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]];
    stack <int> s;
    for (int i = 0; i < n; ++i) r[i] = n;
    for (int i = 0; i < n; ++i) {
        while (s.size() && h[cl[s.top()]] - pref[s.top()] <= h[cl[i]] - pref[i]) {
            r[s.top()] = i;
            s.pop();
        }   
        s.push(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];
        g[i].app(mp(to[i], len[i])); g[to[i]].app(mp(i, len[i]));
    }   
    bfs();
    for (int i = 1; i <= n; ++i) if (inc[i]) tie(h[i], diam[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 13 ms 23756 KB Output is correct
2 Correct 13 ms 23900 KB Output is correct
3 Correct 13 ms 23884 KB Output is correct
4 Correct 13 ms 23844 KB Output is correct
5 Correct 13 ms 23800 KB Output is correct
6 Correct 13 ms 23776 KB Output is correct
7 Correct 15 ms 23876 KB Output is correct
8 Correct 13 ms 23780 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23856 KB Output is correct
11 Correct 13 ms 23780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23944 KB Output is correct
2 Correct 14 ms 23956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24036 KB Output is correct
2 Correct 15 ms 24140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 25236 KB Output is correct
2 Correct 34 ms 26944 KB Output is correct
3 Correct 23 ms 25580 KB Output is correct
4 Correct 17 ms 24652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 28368 KB Output is correct
2 Correct 50 ms 32044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 40176 KB Output is correct
2 Correct 93 ms 40296 KB Output is correct
3 Correct 115 ms 47292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 55640 KB Output is correct
2 Correct 200 ms 66984 KB Output is correct
3 Correct 203 ms 69392 KB Output is correct
4 Correct 253 ms 83312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 95280 KB Output is correct
2 Correct 692 ms 107036 KB Output is correct
3 Correct 305 ms 88892 KB Output is correct
4 Correct 429 ms 108752 KB Output is correct
5 Correct 381 ms 110000 KB Output is correct
6 Correct 1042 ms 91972 KB Output is correct
7 Correct 391 ms 117564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 381 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -