답안 #570862

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
570862 2022-05-31T12:01:50 Z amukkalir Islands (IOI08_islands) C++17
64 / 100
435 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]; 
long long 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; 
//cout << u << " " << v << endl; 
        if(!p[v]) gp(v, par, e); 
        else {
//cout << u << " " << v << endl; 
            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, 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); 
            }

            for(int j=0; j<m; j++) {
                assert((edg[cyc[j]].fi == edg[cyc[(j-1+m) % m]].se) && (edg[cyc[j]].se == edg[cyc[(j+1) % m]].fi)); 
            }

            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 += 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 -= c[e]; 
                long long temp = trav(u) + sum - 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:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         scanf("%lld %lld", &j, &l);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23712 KB Output is correct
2 Incorrect 15 ms 23824 KB Output isn't correct
3 Correct 14 ms 23844 KB Output is correct
4 Correct 13 ms 23820 KB Output is correct
5 Correct 15 ms 23732 KB Output is correct
6 Correct 13 ms 23800 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 15 ms 23844 KB Output is correct
10 Incorrect 14 ms 23736 KB Output isn't correct
11 Correct 14 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23820 KB Output is correct
2 Correct 14 ms 23912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23840 KB Output is correct
2 Correct 15 ms 24228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 25036 KB Output is correct
2 Correct 35 ms 27588 KB Output is correct
3 Correct 24 ms 25168 KB Output is correct
4 Incorrect 18 ms 24404 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 29172 KB Output is correct
2 Correct 68 ms 32124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 38892 KB Output is correct
2 Correct 110 ms 45456 KB Output is correct
3 Correct 150 ms 55900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 50908 KB Output is correct
2 Correct 214 ms 77400 KB Output is correct
3 Correct 288 ms 86244 KB Output is correct
4 Correct 332 ms 111568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 325 ms 91192 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 435 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -