제출 #570744

#제출 시각아이디문제언어결과실행 시간메모리
570744amukkalirIslands (IOI08_islands)C++17
34 / 100
2094 ms131072 KiB
#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]; 
long long psum[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; 
            cyc[p[go]].push_back(e); 
            return true; 
        } 
    }

    return false; 
}

long long trav(int u) {
    if(val[u]) 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; 
}

long long sum (int l, int r, int i) {
    if(r < l) return psum[r] + psum[cyc[i].size()-1] - psum[l-1]; 
    if(l) return psum[r] - psum[l-1]; 
    return psum[r]; 
}

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}); 
    }

    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]].fi, edg[cyc[i][0]].se, cyc[i][0]); 

            long long opt = 0; 
            long long total = 0; 

            for(int q = 0; q < cyc[i].size(); q++) {
                int k = cyc[i][q]; 
                total += c[k]; 
                psum[q] = c[k]; 
                if(q) psum[q] += psum[q-1];
            }

            for(int q=0; q<cyc[i].size(); q++) {
                for(int r=q; r < cyc[i].size(); r++) {
                    int u = edg[cyc[i][q]].fi; 
                    int v = edg[cyc[i][r]].se; 
                    if(u == v) continue; 
                    opt = max(opt, trav(u) + trav(v) + max(sum(q,r, i), total - sum(q,r, i))); 
                }
            }
            ans += opt; 
        }
    }

    printf("%lld", ans); 
}

컴파일 시 표준 에러 (stderr) 메시지

islands.cpp: In function 'int main()':
islands.cpp:101:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |             for(int q = 0; q < cyc[i].size(); q++) {
      |                            ~~^~~~~~~~~~~~~~~
islands.cpp:108:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |             for(int q=0; q<cyc[i].size(); q++) {
      |                          ~^~~~~~~~~~~~~~
islands.cpp:109:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |                 for(int r=q; r < cyc[i].size(); r++) {
      |                              ~~^~~~~~~~~~~~~~~
islands.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         scanf("%lld %lld", &j, &l);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...