Submission #388418

# Submission time Handle Problem Language Result Execution time Memory
388418 2021-04-11T12:19:48 Z osaaateiasavtnl Islands (IOI08_islands) C++14
90 / 100
1097 ms 131072 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
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
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; 
}
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];
void dfs1(int u, int p) {
    for (auto e : g[u]) {
        int v = e.f;
        if (v == p || inc[v]) continue;
        dfs1(v, u);
        ckmax(diam[u], diam[v]);
        ckmax(diam[u], h[u] + h[v] + e.s);
        ckmax(h[u], h[v] + e.s);
    }   
    ckmax(diam[u], h[u]);
}   
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]) {
    		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 23792 KB Output is correct
3 Correct 14 ms 23884 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 16 ms 23868 KB Output is correct
6 Correct 14 ms 23804 KB Output is correct
7 Correct 16 ms 23884 KB Output is correct
8 Correct 14 ms 23876 KB Output is correct
9 Correct 13 ms 23824 KB Output is correct
10 Correct 14 ms 23884 KB Output is correct
11 Correct 14 ms 23828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23928 KB Output is correct
2 Correct 14 ms 23852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24016 KB Output is correct
2 Correct 14 ms 24140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 24900 KB Output is correct
2 Correct 29 ms 26540 KB Output is correct
3 Correct 28 ms 25300 KB Output is correct
4 Correct 19 ms 24504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 27548 KB Output is correct
2 Correct 49 ms 30276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 37716 KB Output is correct
2 Correct 99 ms 37060 KB Output is correct
3 Correct 126 ms 43576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 49024 KB Output is correct
2 Correct 202 ms 61372 KB Output is correct
3 Correct 208 ms 57016 KB Output is correct
4 Correct 257 ms 73824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 341 ms 79768 KB Output is correct
2 Correct 701 ms 91828 KB Output is correct
3 Correct 307 ms 76940 KB Output is correct
4 Correct 393 ms 96160 KB Output is correct
5 Correct 381 ms 97868 KB Output is correct
6 Correct 1097 ms 88208 KB Output is correct
7 Correct 393 ms 102556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 381 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -