답안 #414995

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414995 2021-05-31T11:48:41 Z 반딧불(#7587) Worst Reporter 4 (JOI21_worst_reporter4) C++17
0 / 100
18 ms 16076 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n;
int par[200002], arr[200002]; ll cost[200002];
vector<int> link[200002];
ll ans;

bool hasChecked[200002];

void input();
void findRoot(int);
void treeDP();

int main(){
    input();
    for(int i=1; i<=n; i++){
        if(hasChecked[i]) continue;
        findRoot(i);
        treeDP();
    }
    printf("%lld", ans);
}

void input(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%d %d %lld", &par[i], &arr[i], &cost[i]);
        link[par[i]].push_back(i);
    }
}

vector<int> roots;
vector<int> rootLink;

void findRoot(int x){
    roots.clear();
    rootLink.clear();

    vector<int> stk;
    while(1){
        if(hasChecked[x]){
            auto it = find(stk.begin(), stk.end(), x);
            while(it != stk.end()){
                roots.push_back(*it);
                ++it;
            }
            break;
        }
        hasChecked[x] = 1;
        stk.push_back(x);
        x = par[x];
    }

    while(!stk.empty()){
        hasChecked[stk.back()] = 0;
        stk.pop_back();
    }
    for(auto x: roots){
        stk.push_back(x);
        hasChecked[x] = 1;
    }

    for(auto x: roots){
        for(auto y: link[x]){
            if(!hasChecked[y]) rootLink.push_back(y);
        }
    }

    while(!stk.empty()){
        int tmp = stk.back(); stk.pop_back();

        for(auto y: link[tmp]){
            if(hasChecked[y]) continue;
            hasChecked[y] = 1;
            stk.push_back(y);
        }
    }
}

map<int, ll> DP[200002];
ll costSum;

void treeDP(int);
void treeDP(){
    costSum = 0;
    for(auto y: rootLink) treeDP(y);

    vector<pair<int, ll> > vec;
    map<int, ll> tmpMap;
    map<int, ll> costMap;
    for(auto y: rootLink){
        for(auto p: DP[y]){
            tmpMap[p.first-1] -= p.second;
        }
    }
    for(auto p: tmpMap) vec.push_back(p);

    vec.push_back(make_pair(1e9 + 1, costSum));
    sort(vec.begin(), vec.end());

    for(int i=(int)vec.size()-2; i>=0; i--){
        vec[i].second = vec[i+1].second + vec[i].second;
    }

    ll changeCost = 0;
    for(auto y: roots){
        changeCost += cost[y];
        costMap[arr[y]] += cost[y];
    }

    ll hereAns = changeCost + vec[0].second;
    for(auto p: costMap){
        auto it = lower_bound(vec.begin(), vec.end(), make_pair(p.first, LLONG_MIN));
        hereAns = min(hereAns, changeCost - p.second + it->second);
    }
    ans += hereAns;
}

void mergeDP(map<int, ll> &dpA, map<int, ll> &dpB){
    for(auto p: dpB){
        dpA[p.first] += p.second;
    }

    dpB.clear();
}

void treeDP(int x){
    costSum += cost[x];
    for(auto y: link[x]){
        treeDP(y);

        if((int)DP[x].size() < (int)DP[y].size()) DP[x].swap(DP[y]);
        mergeDP(DP[x], DP[y]);
    }

    if(DP[x].empty()){
        DP[x].insert(make_pair(arr[x]+1, cost[x]));
        return;
    }
    DP[x][arr[x]+1] += cost[x];

    auto it = DP[x].upper_bound(arr[x]);
    ll downSum = cost[x];

    while(it != DP[x].begin()){
        if(downSum <= prev(it)->second){
            if(downSum == prev(it)->second) break;
            ll w = prev(it)->second - downSum;
            prev(it)->second = w;
            break;
        }
        else{
            downSum -= prev(it)->second;
            DP[x].erase(prev(it));
        }
    }
}

Compilation message

worst_reporter2.cpp: In function 'void input()':
worst_reporter2.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
worst_reporter2.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         scanf("%d %d %lld", &par[i], &arr[i], &cost[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14328 KB Output is correct
4 Correct 9 ms 14284 KB Output is correct
5 Correct 15 ms 15072 KB Output is correct
6 Correct 13 ms 14600 KB Output is correct
7 Correct 13 ms 14492 KB Output is correct
8 Correct 18 ms 15180 KB Output is correct
9 Correct 13 ms 14540 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 11 ms 14572 KB Output is correct
12 Correct 13 ms 15308 KB Output is correct
13 Correct 12 ms 15208 KB Output is correct
14 Correct 12 ms 14924 KB Output is correct
15 Correct 13 ms 14924 KB Output is correct
16 Correct 15 ms 15052 KB Output is correct
17 Correct 14 ms 14604 KB Output is correct
18 Correct 11 ms 14468 KB Output is correct
19 Correct 14 ms 15308 KB Output is correct
20 Correct 12 ms 14780 KB Output is correct
21 Correct 14 ms 14828 KB Output is correct
22 Correct 13 ms 15308 KB Output is correct
23 Correct 12 ms 14796 KB Output is correct
24 Correct 16 ms 15324 KB Output is correct
25 Correct 11 ms 14924 KB Output is correct
26 Incorrect 14 ms 16076 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14328 KB Output is correct
4 Correct 9 ms 14284 KB Output is correct
5 Correct 15 ms 15072 KB Output is correct
6 Correct 13 ms 14600 KB Output is correct
7 Correct 13 ms 14492 KB Output is correct
8 Correct 18 ms 15180 KB Output is correct
9 Correct 13 ms 14540 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 11 ms 14572 KB Output is correct
12 Correct 13 ms 15308 KB Output is correct
13 Correct 12 ms 15208 KB Output is correct
14 Correct 12 ms 14924 KB Output is correct
15 Correct 13 ms 14924 KB Output is correct
16 Correct 15 ms 15052 KB Output is correct
17 Correct 14 ms 14604 KB Output is correct
18 Correct 11 ms 14468 KB Output is correct
19 Correct 14 ms 15308 KB Output is correct
20 Correct 12 ms 14780 KB Output is correct
21 Correct 14 ms 14828 KB Output is correct
22 Correct 13 ms 15308 KB Output is correct
23 Correct 12 ms 14796 KB Output is correct
24 Correct 16 ms 15324 KB Output is correct
25 Correct 11 ms 14924 KB Output is correct
26 Incorrect 14 ms 16076 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14328 KB Output is correct
4 Correct 9 ms 14284 KB Output is correct
5 Correct 15 ms 15072 KB Output is correct
6 Correct 13 ms 14600 KB Output is correct
7 Correct 13 ms 14492 KB Output is correct
8 Correct 18 ms 15180 KB Output is correct
9 Correct 13 ms 14540 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 11 ms 14572 KB Output is correct
12 Correct 13 ms 15308 KB Output is correct
13 Correct 12 ms 15208 KB Output is correct
14 Correct 12 ms 14924 KB Output is correct
15 Correct 13 ms 14924 KB Output is correct
16 Correct 15 ms 15052 KB Output is correct
17 Correct 14 ms 14604 KB Output is correct
18 Correct 11 ms 14468 KB Output is correct
19 Correct 14 ms 15308 KB Output is correct
20 Correct 12 ms 14780 KB Output is correct
21 Correct 14 ms 14828 KB Output is correct
22 Correct 13 ms 15308 KB Output is correct
23 Correct 12 ms 14796 KB Output is correct
24 Correct 16 ms 15324 KB Output is correct
25 Correct 11 ms 14924 KB Output is correct
26 Incorrect 14 ms 16076 KB Output isn't correct
27 Halted 0 ms 0 KB -