Submission #429356

# Submission time Handle Problem Language Result Execution time Memory
429356 2021-06-15T21:36:54 Z arayi Islands (IOI08_islands) C++17
0 / 100
17 ms 23884 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;


const int N = 1e6 + 30;

int n;
vector <pair<int, int> > g[N];
int pr[N];
bool col[N], cl[N];
vector<int> sm, fp;
lli dp[N], pat;
void dfs0(int v, int par)
{
    //cout << v << par << endl;
    pr[v] = par;
    col[v] = 1;
    bool bl = 0;
    for(auto& p : g[v])
    {
        if(p.fr == par && !bl) {bl = 1; continue;}
        if(col[p.fr] && sm.empty()) 
        {
            int u = v;
            while(u != p.fr) sm.ad(u), cl[u] = 1, u = pr[u];
            cl[p.fr] = 1;
            sm.ad(p.fr);
        }
        if(!col[p.fr]) dfs0(p.fr, v);
    }
}
lli dfs1(int v, int par)
{
    lli ret = 0;
    lli mx = 0, mx1 = 0;
    for(auto& p : g[v])
    {
        if(cl[p.fr] || p.fr == par) continue;
        ret = max(ret, dfs1(p.fr, v));
        dp[v] = max(dp[v], dp[p.fr] + p.sc);
        mx1 = max(mx1, dp[p.fr] + p.sc);
        if(mx1 > mx) swap(mx, mx1);
    }
    ret = max(ret, mx + mx1);
    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])
        {
            //cout << i << endl;
            dfs0(i, i);
            ans = 0;
            for(auto& p : sm) ans = max(ans, dfs1(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;
}

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

*/

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:94:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     freopen("input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23756 KB Output isn't correct
2 Incorrect 16 ms 23860 KB Output isn't correct
3 Incorrect 17 ms 23884 KB Output isn't correct
4 Incorrect 15 ms 23756 KB Output isn't correct
5 Incorrect 16 ms 23824 KB Output isn't correct
6 Incorrect 16 ms 23784 KB Output isn't correct
7 Incorrect 16 ms 23884 KB Output isn't correct
8 Incorrect 17 ms 23772 KB Output isn't correct
9 Incorrect 17 ms 23880 KB Output isn't correct
10 Incorrect 16 ms 23828 KB Output isn't correct
11 Incorrect 16 ms 23756 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23824 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 23876 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23788 KB Output isn't correct
2 Halted 0 ms 0 KB -