Submission #571365

# Submission time Handle Problem Language Result Execution time Memory
571365 2022-06-02T04:35:32 Z amukkalir Islands (IOI08_islands) C++17
12 / 100
443 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 cnt = 1; 

int vis[nax+5]; 
int c[nax+5]; //cost 
long long val[nax+5]; 
vector<pii> adj[nax+5]; 
vector<pii> edg; 
vector<int> cyc; 
 
void gp(int u, int ls=0) {
    vis[u]=cnt; 
    for (auto t : adj[u]) {
        int v = t.fi; 
        int e = t.se; 
        if(e==ls) continue; 
        if(vis[v] != cnt) gp(v, 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] = cnt; 
            vis[v] = cnt; 
            //cout << u << " " << v << endl; 
            if(edg[e].fi == u) swap(edg[e].fi, edg[e].se); 
            cyc.push_back(e); 
            return true; 
        }
    }
    return false; 
}
 
long long nono = 0; 
long long trav(int u) {
    if(val[u] != -1) return val[u];
    vis[u] = cnt; 

    priority_queue<long long, vector<long long>, greater<long long>> pq; 
    for(pii t : adj[u]) {
        int v = t.fi; 
        int e = t.se; 
        if(vis[v] != cnt) {
            pq.push(1ll*c[e] + trav(v)); 
        }
        while(pq.size() > 2) pq.pop(); 
    }

    if(pq.size() == 2) {
        long long tmp = pq.top(); 
        pq.pop(); 
        nono = max(nono, tmp + pq.top()); 
    }

    return val[u] = pq.empty() ? 0 : pq.top(); 
}
 
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(!vis[i]) {
            cnt++; 
            nono = 0; 
            cyc.clear(); 
            gp(i,i); 

            cnt++; 
            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 += max(opt,nono); 
        }

    }  
 
    printf("%lld", ans); 
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         scanf("%lld %lld", &j, &l);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Runtime error 35 ms 48116 KB Execution killed with signal 11
3 Correct 14 ms 23764 KB Output is correct
4 Runtime error 31 ms 47972 KB Execution killed with signal 11
5 Correct 12 ms 23712 KB Output is correct
6 Incorrect 13 ms 23764 KB Output isn't correct
7 Incorrect 13 ms 23764 KB Output isn't correct
8 Runtime error 31 ms 48052 KB Execution killed with signal 11
9 Incorrect 12 ms 23764 KB Output isn't correct
10 Runtime error 29 ms 47976 KB Execution killed with signal 11
11 Correct 12 ms 23792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 40 ms 48120 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 34 ms 48232 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 38 ms 49932 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 50 ms 54404 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 108 ms 73364 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 158 ms 54296 KB Output is correct
2 Runtime error 203 ms 111832 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 443 ms 131072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 443 ms 131072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -