Submission #571067

# Submission time Handle Problem Language Result Execution time Memory
571067 2022-06-01T07:55:54 Z amukkalir Islands (IOI08_islands) C++17
64 / 100
387 ms 131072 KB
#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll; 
 
#define pii pair<int,int> 
#define fi first 
#define se second 
#define pb push_back 
 
const int nax = 1e6; 
int n; 
 
int p[nax+5]; 
bool vis[nax+5]; 
long long val[nax+5]; 
int c[nax+5]; //cost 
vector<pii> adj[nax+5]; 
vector<pii> edg; 
vector<int> cyc; 
 
void gp(int u, int par, int ls=0) {
    p[u]=par; 
    for (auto t : adj[u]) {
        int v = t.fi; 
        int e = t.se; 
        if(e==ls) continue; 
        if(!p[v]) gp(v, par, e); 
        else {
            cyc.push_back(e); 
        }
    }
}
 
bool fc(int u, int go, int ls) {
    if(u == go) return true; 
 
    for(auto t : adj[u]) {
        int v = t.fi; 
        int e = t.se; 
        if(e == ls) continue; 
 
        bool ok = fc(v, go, e); 
        if(ok) {
            vis[u] = true; 
            vis[v] = true; 
            if(edg[e].fi == u) swap(edg[e].fi, edg[e].se); 
            cyc.push_back(e); 
            return true; 
        }
    }
    return false; 
}
 
long long trav(int u) {
    if(val[u] != -1) return val[u];
    vis[u] = true; 
    long long res = 0; 
    for(pii t : adj[u]) {
        int v = t.fi; 
        int e = t.se; 
        if(!vis[v]) res = max(res, 1ll*c[e] + trav(v)); 
    }
    return res; 
}
 
signed main () {
    scanf("%d", &n); 
    edg.pb({0,0}); 
 
    for(int i=1; i<=n; i++) {
        long long l, j; 
        scanf("%lld %lld", &j, &l); 
        c[i] = l; 
        val[i] = -1; 
        adj[j].pb({i,i}); 
        adj[i].pb({j,i}); 
        edg.pb({i,j}); 
    }
 
    long long ans = 0; 
    for(int i=1; i<=n; i++) {
        if(!p[i]) {
            cyc.clear(); 
            gp(i,i); 
            cyc.pop_back(); 
            fc(edg[cyc[0]].se, edg[cyc[0]].fi, cyc[0]); 
            swap(edg[cyc[0]].fi, edg[cyc[0]].se); 
 
            int m = cyc.size(); 
            for(int j=0; j<m; j++) {
                val[edg[cyc[j]].fi] = trav(edg[cyc[j]].fi); 
                if(edg[cyc[j]].fi != edg[cyc[(j-1+m) % m]].se) swap(edg[cyc[j]].fi, edg[cyc[j]].se); 
            }
 
            long long opt = 0; 
            deque<pair<long long, int>> dq; 
            long long dif = 0; 
            long long sum = 0; 
            int cnt = 0; 
 
            for(int j=0; j<m; j++) {
                int e = cyc[j]; 
                sum += 1ll*c[e]; 
 
                if(j+1 < m) {
                    long long temp = sum + trav(edg[e].se); 
                    while(!dq.empty() && dq.back().fi <= temp) dq.pop_back(); 
                    dq.push_back({temp, cnt++}); 
                }
            }
 
            for(int j=0; j<m; j++) {
                int e = cyc[j]; 
                int u = edg[e].fi; 
 
                opt = max(opt , trav(u) + dq.front().fi + dif); 
                dif -= 1ll*c[e]; 
                long long temp = trav(u) + sum - 1ll*c[e] - dif; 
 
                while(!dq.empty() && dq.front().se <= cnt-m) dq.pop_front(); 
                while(!dq.empty() && dq.back().fi <= temp) dq.pop_back(); 
                dq.push_back({temp, cnt++}); 
            }
 
            ans += opt; 
        }
 
    }
 
    printf("%lld", ans); 
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         scanf("%lld %lld", &j, &l);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Correct 15 ms 23764 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 14 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 14 ms 23756 KB Output is correct
10 Incorrect 13 ms 23780 KB Output isn't correct
11 Correct 13 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24000 KB Output is correct
2 Correct 16 ms 23880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 15 ms 24276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 25036 KB Output is correct
2 Correct 33 ms 27976 KB Output is correct
3 Correct 24 ms 25356 KB Output is correct
4 Incorrect 24 ms 24524 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 29552 KB Output is correct
2 Correct 57 ms 32960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 40504 KB Output is correct
2 Correct 117 ms 46824 KB Output is correct
3 Correct 151 ms 58680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 52172 KB Output is correct
2 Correct 226 ms 81108 KB Output is correct
3 Correct 348 ms 89332 KB Output is correct
4 Correct 313 ms 111224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 387 ms 90848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 383 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -