Submission #429273

# Submission time Handle Problem Language Result Execution time Memory
429273 2021-06-15T19:49:25 Z arayi Islands (IOI08_islands) C++17
67 / 100
684 ms 131076 KB
//Arayi
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>   
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>

#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;

lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
    return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
    return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = { 'a', 'e', 'i', 'o', 'u' };
int dx[] = { 0, -1, 0, 1, -1, -1, 1, 1, 0 };
int dy[] = { -1, 0, 1, 0, -1, 1, -1, 1, 0 };


const int N = 1e6 + 30;
lli mod = 1e9 + 7;
const ld pi = acos(-1);
const int T = 200;

lli bp(lli a, lli b = mod - 2LL)
{
    lli ret = 1;
    while (b)
    {
        if (b & 1) ret *= a, ret %= mod;
        a *= a;
        a %= mod;
        b >>= 1;
    }
    return ret;
}
ostream& operator<<(ostream& c, pir a)
{
    c << a.fr << " " << a.sc;
    return c;
}
template<class T>
void maxi(T& a, T b)
{
    a = max(a, b);
}
template <class T>
void mini(T& a, T b)
{
    a = min(a, b);
}



int n;
vector <pair<int, lli> > g[N];
bool col[N], cl[N];
vector<lli> fp;
vector<int> sm;
lli dp[N], dp1[N], pat;
void dfs0(int v, int par)
{
    col[v] = 1;
    if(sm.empty()) fp.ad(v);
    bool bl = 0;
    for(auto& p : g[v])
    {
        if(p.fr == par && !bl) {bl = 1; continue;}
        if(col[p.fr] && sm.empty()) 
        {
            while( fp.back() != p.fr) sm.ad(fp.back()), cl[fp.back()] = 1, fp.pop_back();
            cl[p.fr] = 1;
            sm.ad(p.fr);
        }
        if(!col[p.fr]) dfs0(p.fr, v);
    }
    if(sm.empty()) fp.pop_back();
}
void dfs1(int v, int par)
{
    for(auto& p : g[v])
    {
        if(cl[p.fr] || p.fr == par) continue;
        dfs1(p.fr, v);
        dp[v] = max(dp[v], dp[p.fr] + p.sc);
    }
}
lli dfs(int v, int par)
{
    vector <lli> fp;
    lli ret = dp[v] + dp1[v];
    for(auto& p : g[v])
    {
        if(p.fr == par || cl[p.fr]) continue;
        dp1[p.fr] = dp1[v] + p.sc;
        fp.ad(dp[p.fr] + p.sc);
    }
    sort(all(fp));
    reverse(all(fp));
    if(fp.size() > 1)
    {
        for(auto& p : g[v])
        {
            if(p.fr == par || cl[p.fr]) continue;
            if(dp[p.fr] + p.sc == fp[0]) dp1[p.fr] = max(dp1[p.fr], fp[1] + dp[p.fr] + p.sc);
            else dp1[p.fr] = max(dp1[p.fr], fp[0] + dp[p.fr] + p.sc);
        }
    }
    for(auto& p : g[v])
    {
        if(p.fr == par || cl[p.fr]) continue;
        ret = max(ret, dfs(p.fr, v));
    }
    return ret;
}
int main()
{
    fastio;
    //freopen("input.in", "r", stdin);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        g[i].ad(MP(a, b));
        g[a].ad(MP(i, b));
    }
    lli mx, sum, nx, ans;
    for (int i = 1; i <= n; i++)
    {
        if(!col[i])
        {
            dfs0(i, i);
            ans = 0;
            for(auto& p : sm)
            {
                dfs1(p, p);
                ans = max(ans, dfs(p, p));
            }
            fp.clear();
            reverse(all(g[sm.back()]));
            sm.ad(sm[0]);
            for (int i = 0; i < (int)sm.size() - 1; i++)
                for(auto& p : g[sm[i]]) if(p.fr == sm[i + 1]) {fp.ad(p.sc); break;}
            sm.pop_back();
            mx = 0, nx = 0;
            for (int i = 0; i < (int)sm.size(); i++)
            {
                ans = max(ans, dp[sm[i]] + nx + mx);
                mx = max(mx, dp[sm[i]] - nx);
                nx += fp[i];
            }
            sum = nx; nx = mx = 0;
            for (int i = 0; i < (int)sm.size(); i++)
            {
                if(i) ans = max(ans, dp[sm[i]] + sum - nx + mx);
                mx = max(mx, dp[sm[i]] + nx);
                nx += fp[i];
            }
            pat += ans;
            fp.clear(), sm.clear();
        }
    }
    cout << pat << endl;
    //cout << "ended" << endl;
    return 0;
}

/*
    __
  *(><)*
    \/ /
    ||/
  --||
    ||
    /\
   /  \
  /    \

*/
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23712 KB Output is correct
2 Incorrect 14 ms 23784 KB Output isn't correct
3 Correct 14 ms 23820 KB Output is correct
4 Correct 16 ms 23816 KB Output is correct
5 Correct 15 ms 23712 KB Output is correct
6 Correct 14 ms 23804 KB Output is correct
7 Correct 15 ms 23788 KB Output is correct
8 Correct 15 ms 23812 KB Output is correct
9 Correct 16 ms 23788 KB Output is correct
10 Correct 15 ms 23772 KB Output is correct
11 Correct 15 ms 23816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23956 KB Output is correct
2 Correct 15 ms 23884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23896 KB Output is correct
2 Correct 20 ms 24276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 25308 KB Output is correct
2 Correct 35 ms 28392 KB Output is correct
3 Incorrect 28 ms 25540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 29776 KB Output is correct
2 Correct 60 ms 32612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 39916 KB Output is correct
2 Correct 119 ms 45424 KB Output is correct
3 Correct 153 ms 58908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 51824 KB Output is correct
2 Correct 253 ms 81436 KB Output is correct
3 Correct 264 ms 83488 KB Output is correct
4 Correct 326 ms 112392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 500 ms 95936 KB Output is correct
2 Runtime error 684 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 438 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -