답안 #570852

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
570852 2022-05-31T11:16:19 Z amukkalir Islands (IOI08_islands) C++17
64 / 100
375 ms 131072 KB
#include "bits/stdc++.h"
using namespace std; 

#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 c[nax+5]; 
long long val[nax+5]; 
vector<pii> adj[nax+5]; 
vector<int> cyc[nax+5]; 
vector<pii> edg; 

void gp(int u, int par, int ls=0) {
    p[u] = par; 
    for(pii t : adj[u]) {
        int v = t.fi; 
        int e = t.se; 
        if(e == ls) continue; 

        if(p[v] == 0) {
            gp(v, par, e); 
        } else {
            cyc[par].push_back(e); 
        }
    }
}

bool fc(int u, int go, int ls) {
    if(u == go) return true; 

    for(pii 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[p[go]].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, c[e] + trav(v)); 
        }
    }

    return val[u] = res; 
}

int 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; 
        adj[j].pb({i,i}); 
        adj[i].pb({j,i}); 
        edg.pb({i,j}); 
        val[i] = -1; 
    }

    long long ans = 0; 
    for(int i=1; i<=n; i++) {
        if(p[i] == 0) {
            gp(i,i); 
            cyc[i].pop_back(); 
            fc(edg[cyc[i][0]].se, edg[cyc[i][0]].fi, cyc[i][0]); 

            long long opt = 0; 

            //priority_queue<long long> pq; 
            deque<pair<long long, int>> dq; 

            long long dpq = 0; 
            long long sum = 0; 
            int sz = cyc[i].size(); 
            int cnt = 0; 

            for(int j=0; j<sz; j++) {
                int e = cyc[i][j]; 
                swap(edg[e].fi, edg[e].se); 
                sum += c[e]; 
                
                if(j+1 < sz) {
                    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<sz; j++) {
                int e = cyc[i][j]; 
                int u = edg[e].fi; 
                opt = max(opt, trav(u) + dq.front().fi + dpq); 
                dpq -= c[e]; 
                long long temp = trav(u) + sum - c[e] - dpq; 

                while(!dq.empty() && dq.front().se <= cnt-sz) 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:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         scanf("%lld %lld", &j, &l);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47300 KB Output is correct
2 Incorrect 31 ms 47180 KB Output isn't correct
3 Correct 25 ms 47316 KB Output is correct
4 Correct 23 ms 47292 KB Output is correct
5 Correct 26 ms 47200 KB Output is correct
6 Correct 25 ms 47188 KB Output is correct
7 Correct 26 ms 47188 KB Output is correct
8 Correct 27 ms 47216 KB Output is correct
9 Correct 37 ms 47212 KB Output is correct
10 Incorrect 24 ms 47276 KB Output isn't correct
11 Correct 24 ms 47304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47316 KB Output is correct
2 Correct 28 ms 47316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47328 KB Output is correct
2 Correct 26 ms 47692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 48424 KB Output is correct
2 Correct 45 ms 51124 KB Output is correct
3 Correct 35 ms 48616 KB Output is correct
4 Incorrect 30 ms 47988 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 52580 KB Output is correct
2 Correct 64 ms 55620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 62324 KB Output is correct
2 Correct 127 ms 69252 KB Output is correct
3 Correct 137 ms 79416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 75272 KB Output is correct
2 Correct 225 ms 100932 KB Output is correct
3 Correct 305 ms 110784 KB Output is correct
4 Correct 323 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 350 ms 114756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 375 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -