제출 #671074

#제출 시각아이디문제언어결과실행 시간메모리
671074AdamGSFireworks (APIO16_fireworks)C++17
100 / 100
216 ms70920 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=3e5+7;
vector<pair<int,ll>>V[LIM];
ll cnt[LIM], n, m, ans;
void DFS1(int x) {
        cnt[x]=1;
        for(auto i : V[x]) {
                DFS1(i.st);
                cnt[x]+=cnt[i.st];
        }
        rep(i, V[x].size()) if(cnt[V[x][i].st]>cnt[V[x][0].st]) swap(V[x][0], V[x][i]);
}
void DFS2(int x, ll k, priority_queue<ll>&q) {
        ans+=k;
        if(x>=n) {
                q.push(0);
                q.push(k);
                q.push(k);
                return;
        }
        DFS2(V[x][0].st, V[x][0].nd, q);
        for(int i=1; i<V[x].size(); ++i) {
                priority_queue<ll>p;
                DFS2(V[x][i].st, V[x][i].nd, p);
                while(!p.empty()) {
                        q.push(p.top());
                        p.pop();
                }
        }
        rep(i, V[x].size()-1) q.pop();
        ll a=q.top(); q.pop();
        ll b=q.top(); q.pop();
        q.push(a+k); q.push(b+k);
}
int main() {
        ios_base::sync_with_stdio(0); cin.tie(0);
        cin >> n >> m;
        rep(i, n+m-1) {
                ll a, b;
                cin >> a >> b; --a;
                V[a].pb({i+1, b});
        }
        DFS1(0);
        priority_queue<ll>Q;
        DFS2(0, 0, Q);
        Q.pop();
        vector<ll>P;
        while(!Q.empty()) {
                P.pb(Q.top());
                Q.pop();
        }
        for(ll i=P.size()-1; i; --i) ans-=i*(P[i-1]-P[i]);
        cout << ans << '\n';
}

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

fireworks.cpp: In function 'void DFS1(int)':
fireworks.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
      |                                    ^
fireworks.cpp:19:9: note: in expansion of macro 'rep'
   19 |         rep(i, V[x].size()) if(cnt[V[x][i].st]>cnt[V[x][0].st]) swap(V[x][0], V[x][i]);
      |         ^~~
fireworks.cpp: In function 'void DFS2(int, ll, std::priority_queue<long long int>&)':
fireworks.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         for(int i=1; i<V[x].size(); ++i) {
      |                      ~^~~~~~~~~~~~
fireworks.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
      |                                    ^
fireworks.cpp:38:9: note: in expansion of macro 'rep'
   38 |         rep(i, V[x].size()-1) q.pop();
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...