답안 #639099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
639099 2022-09-08T14:58:09 Z puppy Islands (IOI08_islands) C++17
70 / 100
479 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
int N;
vector<pair<int, int>> adj[1000005];
bitset<1000005> visited;
bitset<1000005> cycle;
pair<int, int> parent[1000005];
int cyc, cyc2;
int cycval;
long long far[1000005];
long long dp[1000005];
vector<int> chd[1000005];

struct maxheap
{
    priority_queue<long long> pq;
    priority_queue<long long> del;
    void push(long long v)
    {
        pq.push(v);
        while(!del.empty() && del.top() == pq.top()) {
            pq.pop();
            del.pop();
        }
    }
    void dsp(long long v)
    {
        del.push(v);
        while(!del.empty() && del.top() == pq.top()) {
            pq.pop();
            del.pop();
        }
    }
    long long top()
    {
        return pq.top();
    }
    void pop()
    {
        pq.pop();
        while(!del.empty() && del.top() == pq.top()) {
            pq.pop();
            del.pop();
        }
    }
    int Size()
    {
        return pq.size() - del.size();
    }
    bool isEmpty()
    {
        return Size() == 0;
    }
    void reset()
    {
        while(!pq.empty()) pq.pop();
        while(!del.empty()) del.pop();
    }
};

void dfs(int v, int p, int asg)
{
    visited[v] = true;
    parent[v] = make_pair(p, asg);
    int cnt = 0;
    int tmp;
    for(auto &i:adj[v]){
        if(i.first==p){
            ++cnt;
            if(cnt==1) tmp = i.second;
            else{
                if(cyc == -1){
                    cyc = v;
                    cyc2 = i.first;
                    cycle[v] = true;
                    cycval = (tmp == asg) ? i.second : tmp;
                }
            }
            continue;
        }
        if(visited[i.first]){
            if(cyc == -1){
                cyc = v;
                cyc2 = i.first;
                cycval = i.second;
                cycle[v] = true;
            }
            continue;
        }
        dfs(i.first, v, i.second);
    }
}
void treedfs(int v, int prv, int p)
{
    chd[p].push_back(v);
    for(auto &i:adj[v]){
        if(i.first == prv || cycle[i.first]) continue;
        dp[i.first] = dp[v] + i.second;
        treedfs(i.first, v, p);
    }
}
void treedfs2(int v, int prv, int p)
{
    for(auto &i:adj[v]){
        if(cycle[i.first] && i.first != p) continue;
        if(i.first == prv) continue;
        dp[i.first] = dp[v] + i.second;
        treedfs2(i.first, v, p);
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    long long ans = 0;
    cin>>N;
    int tmp, k;
    for(int i=1;i<=N;i++){
        cin>>tmp>>k;
        adj[tmp].push_back(make_pair(i, k));
        adj[i].push_back(make_pair(tmp, k));
    }
    for(int i=0;i<1000005;i++) adj[i].shrink_to_fit();
    int cycstart, cycend, tt;
    long long cur, dsp, sub;
    long long m; int loc;
    vector<int> fun, val;
    for(int i=1;i<=N;i++){
        if(visited[i]) continue;
        long long tmpans = 0;
        vector<int>().swap(fun);
        vector<int>().swap(val);
        cyc = -1;
        cyc2 = -1;
        dfs(i, 0, 0);
        cycstart = cyc;
        cycend = cyc2;
        fun.push_back(cycstart);
        val.push_back(cycval);
        while(cycstart != cycend){
            tt = parent[cycstart].second;
            cycstart = parent[cycstart].first;
            cycle[cycstart] = true;
            fun.push_back(cycstart);
            val.push_back(tt);
        }
        fun.shrink_to_fit();
        val.shrink_to_fit();
        for(int i:fun){
            treedfs(i, i, i);
            m = -1;
            for(int k:chd[i]){
                if(dp[k] > m){
                    m = dp[k];
                    loc = k;
                }
            }
            far[i] = m;
            dp[loc] = 0;
            treedfs2(loc, loc, i);
            m = -1;
            for(int k:chd[i]) m = max(m, dp[k]);
            tmpans = max(tmpans, m);
        }
        maxheap heap;
        cur = dsp = sub = 0;
        for(int i=1;i<(int)fun.size();i++){
            cur += val[i];
            heap.push(cur+far[fun[i]]);
        }
        tmpans = max(tmpans, heap.top()+far[fun[0]]);
        for(int i=1;i<(int)fun.size();i++){
            dsp += val[i];
            heap.dsp(dsp + far[fun[i]]);
            sub += val[i];
            cur += val[i-1];
            heap.push(cur+far[fun[i-1]]);
            tmpans = max(tmpans, heap.top()+far[fun[i]]-sub);
        }
        heap.reset();
        cur = dsp = sub = 0;
        for(int i=(int)fun.size()-2;i>=0;i--){
            cur += val[i+1];
            heap.push(cur+far[fun[i]]);
        }
        tmpans = max(tmpans, heap.top()+far[fun.back()]);
        for(int i=(int)fun.size()-2;i>=0;i--){
            sub += val[i+1];
            dsp += val[i+1];
            cur += val[(i+2)%val.size()];
            heap.dsp(dsp+far[fun[i]]);
            heap.push(cur+far[fun[(i+1)%fun.size()]]);
            tmpans = max(tmpans, heap.top() + far[fun[i]]-sub);
        }
        ans += tmpans;
    }
    cout<<ans<<'\n';
}

Compilation message

islands.cpp: In function 'void dfs(int, int, int)':
islands.cpp:76:28: warning: 'tmp' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     cycval = (tmp == asg) ? i.second : tmp;
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:159:21: warning: 'loc' may be used uninitialized in this function [-Wmaybe-uninitialized]
  159 |             treedfs2(loc, loc, i);
      |             ~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 47188 KB Output is correct
2 Correct 25 ms 47252 KB Output is correct
3 Correct 24 ms 47316 KB Output is correct
4 Correct 25 ms 47236 KB Output is correct
5 Correct 30 ms 47272 KB Output is correct
6 Correct 25 ms 47284 KB Output is correct
7 Correct 26 ms 47188 KB Output is correct
8 Correct 25 ms 47316 KB Output is correct
9 Correct 28 ms 47276 KB Output is correct
10 Correct 26 ms 47216 KB Output is correct
11 Correct 25 ms 47316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 47348 KB Output is correct
2 Correct 25 ms 47444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 47428 KB Output is correct
2 Correct 29 ms 47836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 48620 KB Output is correct
2 Correct 48 ms 51564 KB Output is correct
3 Correct 43 ms 48828 KB Output is correct
4 Correct 30 ms 47980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 53396 KB Output is correct
2 Correct 90 ms 56776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 62908 KB Output is correct
2 Correct 160 ms 74192 KB Output is correct
3 Correct 200 ms 86008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 233 ms 78732 KB Output is correct
2 Correct 308 ms 108104 KB Output is correct
3 Correct 438 ms 126804 KB Output is correct
4 Runtime error 287 ms 131072 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 304 ms 110128 KB Output is correct
2 Runtime error 479 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 326 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -