#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
https://codeforces.com/blog/entry/88748?#comment-774017 (thread)
got the key idea, but didnt know how to implement in a clean way
implementation idea:
represent the subtree cost @u as a staircase function using a map
can be merged efficiently using small to large
*/
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<ll> adj[N];
vector<pll> a(N);
vector<ll> subsiz(N);
vector<bool> on_cyc(N);
void dfs1(ll u){
subsiz[u] = 1;
trav(v,adj[u]){
if(on_cyc[v]) conts;
dfs1(v);
subsiz[u] += subsiz[v];
}
}
map<ll,ll> mp[N];
void dfs2(ll u){
pll best = {-1,-1};
trav(v,adj[u]){
if(on_cyc[v]) conts;
pll px = {subsiz[v],v};
amax(best,px);
}
ll heavy = best.ss;
if(heavy == -1){
auto [h,c] = a[u];
mp[u][1] = 0;
mp[u][h+1] = c;
return;
}
dfs2(heavy);
if(on_cyc[u]){
mp[u] = mp[heavy];
}
else{
swap(mp[u],mp[heavy]);
}
trav(v,adj[u]){
if(on_cyc[v]) conts;
if(v == heavy) conts;
dfs2(v);
for(auto [i,x] : mp[v]){
mp[u][i] += x;
}
// mp[v].clear();
}
auto [h,c] = a[u];
mp[u][1] += c;
mp[u][h] -= c;
mp[u][h+1] += c;
auto it = mp[u].find(h);
vector<ll> rem;
ll val = mp[u][h], recent_h = h;
while(val < 0){
it--;
rem.pb(recent_h);
recent_h = it->first;
val += it->second;
}
mp[u][recent_h] = val;
trav(x,rem){
mp[u].erase(x);
}
}
void solve(int test_case)
{
ll n; cin >> n;
vector<ll> par(n+5);
rep1(i,n){
ll p,h,c; cin >> p >> h >> c;
adj[p].pb(i);
par[i] = p;
a[i] = {h,c};
}
vector<ll> outdeg(n+5);
rep1(i,n) outdeg[i] = sz(adj[i]);
queue<ll> q;
rep1(i,n){
if(outdeg[i] == 0){
q.push(i);
}
}
while(!q.empty()){
ll u = q.front();
q.pop();
ll p = par[u];
outdeg[p]--;
if(outdeg[p] == 0){
q.push(p);
}
}
rep1(i,n){
if(outdeg[i]){
on_cyc[i] = 1;
}
}
vector<bool> vis(n+5);
ll ans = 0;
rep1(i,n){
if(vis[i] or !on_cyc[i]) conts;
ll j = i;
map<ll,ll> sum_mp;
vector<ll> nodes;
ll tot_sum = 0;
while(!vis[j]){
vis[j] = 1;
nodes.pb(j);
dfs1(j);
dfs2(j);
sum_mp[a[j].ff] += a[j].ss;
tot_sum += a[j].ss;
j = par[j];
}
map<ll,ll> cost_mp;
cost_mp[1] += 0;
ll cost_sum = 0;
trav(u,nodes){
cost_mp[a[u].ff] += 0;
trav(v,adj[u]){
if(on_cyc[v]) conts;
for(auto [i,x] : mp[v]){
cost_mp[i] += x;
}
}
}
ll cost = inf2;
for(auto [h,delta] : cost_mp){
cost_sum += delta;
ll curr_cost = cost_sum+tot_sum-sum_mp[h];
amin(cost,curr_cost);
}
ans += cost;
}
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
7 ms |
19280 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19032 KB |
Output is correct |
5 |
Correct |
11 ms |
21084 KB |
Output is correct |
6 |
Correct |
9 ms |
20060 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
11 ms |
21084 KB |
Output is correct |
9 |
Correct |
9 ms |
20060 KB |
Output is correct |
10 |
Correct |
9 ms |
19800 KB |
Output is correct |
11 |
Correct |
7 ms |
19544 KB |
Output is correct |
12 |
Correct |
10 ms |
20572 KB |
Output is correct |
13 |
Correct |
8 ms |
20316 KB |
Output is correct |
14 |
Correct |
9 ms |
20060 KB |
Output is correct |
15 |
Correct |
9 ms |
19804 KB |
Output is correct |
16 |
Correct |
12 ms |
21340 KB |
Output is correct |
17 |
Correct |
9 ms |
20212 KB |
Output is correct |
18 |
Correct |
7 ms |
19676 KB |
Output is correct |
19 |
Correct |
10 ms |
20828 KB |
Output is correct |
20 |
Correct |
8 ms |
20056 KB |
Output is correct |
21 |
Correct |
7 ms |
20056 KB |
Output is correct |
22 |
Correct |
10 ms |
20828 KB |
Output is correct |
23 |
Correct |
8 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20828 KB |
Output is correct |
25 |
Correct |
8 ms |
20316 KB |
Output is correct |
26 |
Correct |
11 ms |
22104 KB |
Output is correct |
27 |
Correct |
10 ms |
21084 KB |
Output is correct |
28 |
Correct |
13 ms |
21340 KB |
Output is correct |
29 |
Correct |
10 ms |
21340 KB |
Output is correct |
30 |
Correct |
10 ms |
20948 KB |
Output is correct |
31 |
Correct |
11 ms |
20828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
7 ms |
19280 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19032 KB |
Output is correct |
5 |
Correct |
11 ms |
21084 KB |
Output is correct |
6 |
Correct |
9 ms |
20060 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
11 ms |
21084 KB |
Output is correct |
9 |
Correct |
9 ms |
20060 KB |
Output is correct |
10 |
Correct |
9 ms |
19800 KB |
Output is correct |
11 |
Correct |
7 ms |
19544 KB |
Output is correct |
12 |
Correct |
10 ms |
20572 KB |
Output is correct |
13 |
Correct |
8 ms |
20316 KB |
Output is correct |
14 |
Correct |
9 ms |
20060 KB |
Output is correct |
15 |
Correct |
9 ms |
19804 KB |
Output is correct |
16 |
Correct |
12 ms |
21340 KB |
Output is correct |
17 |
Correct |
9 ms |
20212 KB |
Output is correct |
18 |
Correct |
7 ms |
19676 KB |
Output is correct |
19 |
Correct |
10 ms |
20828 KB |
Output is correct |
20 |
Correct |
8 ms |
20056 KB |
Output is correct |
21 |
Correct |
7 ms |
20056 KB |
Output is correct |
22 |
Correct |
10 ms |
20828 KB |
Output is correct |
23 |
Correct |
8 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20828 KB |
Output is correct |
25 |
Correct |
8 ms |
20316 KB |
Output is correct |
26 |
Correct |
11 ms |
22104 KB |
Output is correct |
27 |
Correct |
10 ms |
21084 KB |
Output is correct |
28 |
Correct |
13 ms |
21340 KB |
Output is correct |
29 |
Correct |
10 ms |
21340 KB |
Output is correct |
30 |
Correct |
10 ms |
20948 KB |
Output is correct |
31 |
Correct |
11 ms |
20828 KB |
Output is correct |
32 |
Correct |
12 ms |
21340 KB |
Output is correct |
33 |
Correct |
377 ms |
114124 KB |
Output is correct |
34 |
Correct |
254 ms |
65876 KB |
Output is correct |
35 |
Correct |
360 ms |
111956 KB |
Output is correct |
36 |
Correct |
248 ms |
65644 KB |
Output is correct |
37 |
Correct |
164 ms |
47108 KB |
Output is correct |
38 |
Correct |
148 ms |
42324 KB |
Output is correct |
39 |
Correct |
201 ms |
74028 KB |
Output is correct |
40 |
Correct |
182 ms |
73656 KB |
Output is correct |
41 |
Correct |
103 ms |
73696 KB |
Output is correct |
42 |
Correct |
185 ms |
53588 KB |
Output is correct |
43 |
Correct |
169 ms |
53584 KB |
Output is correct |
44 |
Correct |
362 ms |
141992 KB |
Output is correct |
45 |
Correct |
225 ms |
82004 KB |
Output is correct |
46 |
Correct |
90 ms |
42168 KB |
Output is correct |
47 |
Correct |
289 ms |
90452 KB |
Output is correct |
48 |
Correct |
136 ms |
62964 KB |
Output is correct |
49 |
Correct |
106 ms |
62296 KB |
Output is correct |
50 |
Correct |
318 ms |
90060 KB |
Output is correct |
51 |
Correct |
103 ms |
52848 KB |
Output is correct |
52 |
Correct |
318 ms |
92128 KB |
Output is correct |
53 |
Correct |
128 ms |
63692 KB |
Output is correct |
54 |
Correct |
316 ms |
123660 KB |
Output is correct |
55 |
Correct |
269 ms |
99444 KB |
Output is correct |
56 |
Correct |
258 ms |
106940 KB |
Output is correct |
57 |
Correct |
259 ms |
112664 KB |
Output is correct |
58 |
Correct |
297 ms |
90312 KB |
Output is correct |
59 |
Correct |
305 ms |
90396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
7 ms |
19280 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19032 KB |
Output is correct |
5 |
Correct |
11 ms |
21084 KB |
Output is correct |
6 |
Correct |
9 ms |
20060 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
11 ms |
21084 KB |
Output is correct |
9 |
Correct |
9 ms |
20060 KB |
Output is correct |
10 |
Correct |
9 ms |
19800 KB |
Output is correct |
11 |
Correct |
7 ms |
19544 KB |
Output is correct |
12 |
Correct |
10 ms |
20572 KB |
Output is correct |
13 |
Correct |
8 ms |
20316 KB |
Output is correct |
14 |
Correct |
9 ms |
20060 KB |
Output is correct |
15 |
Correct |
9 ms |
19804 KB |
Output is correct |
16 |
Correct |
12 ms |
21340 KB |
Output is correct |
17 |
Correct |
9 ms |
20212 KB |
Output is correct |
18 |
Correct |
7 ms |
19676 KB |
Output is correct |
19 |
Correct |
10 ms |
20828 KB |
Output is correct |
20 |
Correct |
8 ms |
20056 KB |
Output is correct |
21 |
Correct |
7 ms |
20056 KB |
Output is correct |
22 |
Correct |
10 ms |
20828 KB |
Output is correct |
23 |
Correct |
8 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20828 KB |
Output is correct |
25 |
Correct |
8 ms |
20316 KB |
Output is correct |
26 |
Correct |
11 ms |
22104 KB |
Output is correct |
27 |
Correct |
10 ms |
21084 KB |
Output is correct |
28 |
Correct |
13 ms |
21340 KB |
Output is correct |
29 |
Correct |
10 ms |
21340 KB |
Output is correct |
30 |
Correct |
10 ms |
20948 KB |
Output is correct |
31 |
Correct |
11 ms |
20828 KB |
Output is correct |
32 |
Correct |
12 ms |
21340 KB |
Output is correct |
33 |
Correct |
377 ms |
114124 KB |
Output is correct |
34 |
Correct |
254 ms |
65876 KB |
Output is correct |
35 |
Correct |
360 ms |
111956 KB |
Output is correct |
36 |
Correct |
248 ms |
65644 KB |
Output is correct |
37 |
Correct |
164 ms |
47108 KB |
Output is correct |
38 |
Correct |
148 ms |
42324 KB |
Output is correct |
39 |
Correct |
201 ms |
74028 KB |
Output is correct |
40 |
Correct |
182 ms |
73656 KB |
Output is correct |
41 |
Correct |
103 ms |
73696 KB |
Output is correct |
42 |
Correct |
185 ms |
53588 KB |
Output is correct |
43 |
Correct |
169 ms |
53584 KB |
Output is correct |
44 |
Correct |
362 ms |
141992 KB |
Output is correct |
45 |
Correct |
225 ms |
82004 KB |
Output is correct |
46 |
Correct |
90 ms |
42168 KB |
Output is correct |
47 |
Correct |
289 ms |
90452 KB |
Output is correct |
48 |
Correct |
136 ms |
62964 KB |
Output is correct |
49 |
Correct |
106 ms |
62296 KB |
Output is correct |
50 |
Correct |
318 ms |
90060 KB |
Output is correct |
51 |
Correct |
103 ms |
52848 KB |
Output is correct |
52 |
Correct |
318 ms |
92128 KB |
Output is correct |
53 |
Correct |
128 ms |
63692 KB |
Output is correct |
54 |
Correct |
316 ms |
123660 KB |
Output is correct |
55 |
Correct |
269 ms |
99444 KB |
Output is correct |
56 |
Correct |
258 ms |
106940 KB |
Output is correct |
57 |
Correct |
259 ms |
112664 KB |
Output is correct |
58 |
Correct |
297 ms |
90312 KB |
Output is correct |
59 |
Correct |
305 ms |
90396 KB |
Output is correct |
60 |
Correct |
6 ms |
19036 KB |
Output is correct |
61 |
Correct |
6 ms |
19032 KB |
Output is correct |
62 |
Correct |
5 ms |
19136 KB |
Output is correct |
63 |
Correct |
5 ms |
19276 KB |
Output is correct |
64 |
Correct |
352 ms |
97776 KB |
Output is correct |
65 |
Correct |
267 ms |
58448 KB |
Output is correct |
66 |
Correct |
413 ms |
85996 KB |
Output is correct |
67 |
Correct |
267 ms |
57848 KB |
Output is correct |
68 |
Correct |
188 ms |
44436 KB |
Output is correct |
69 |
Correct |
156 ms |
41132 KB |
Output is correct |
70 |
Correct |
456 ms |
74124 KB |
Output is correct |
71 |
Correct |
221 ms |
58948 KB |
Output is correct |
72 |
Correct |
481 ms |
85348 KB |
Output is correct |
73 |
Correct |
243 ms |
60532 KB |
Output is correct |
74 |
Correct |
479 ms |
86216 KB |
Output is correct |
75 |
Correct |
270 ms |
61388 KB |
Output is correct |
76 |
Correct |
216 ms |
60264 KB |
Output is correct |
77 |
Correct |
384 ms |
88692 KB |
Output is correct |
78 |
Correct |
165 ms |
57544 KB |
Output is correct |
79 |
Correct |
412 ms |
99232 KB |
Output is correct |
80 |
Correct |
232 ms |
63688 KB |
Output is correct |
81 |
Correct |
189 ms |
54216 KB |
Output is correct |
82 |
Correct |
327 ms |
85580 KB |
Output is correct |
83 |
Correct |
108 ms |
58704 KB |
Output is correct |
84 |
Correct |
375 ms |
92724 KB |
Output is correct |
85 |
Correct |
376 ms |
93012 KB |
Output is correct |
86 |
Correct |
354 ms |
87300 KB |
Output is correct |
87 |
Correct |
360 ms |
92608 KB |
Output is correct |
88 |
Correct |
367 ms |
92740 KB |
Output is correct |