#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<pair<int,int>> adj[300000];
int W[300000], C[300000];
long long A[300000], B[300000];
priority_queue<long long> Q[300000];
int prep(int c)
{
W[C[c]=c]=1;
for(auto &n: adj[c]) {
W[c]+=prep(n.first);
if(W[adj[c][0].first]<W[n.first]) swap(adj[c][0],n);
}
if(adj[c].size()) C[c]=C[adj[c][0].first];
return W[c];
}
void solve(int c)
{
long long t1, t2;
if(C[c]==c) {
Q[c].push(0); Q[c].push(0); A[c]=1;
return;
}
for(auto[n,w]: adj[c]) {
solve(n);
t1=Q[C[n]].top(); Q[C[n]].pop();
t2=Q[C[n]].top(); Q[C[n]].pop();
Q[C[n]].push(t1+w); Q[C[n]].push(t2+w); B[C[n]]+=w;
if(C[c]^C[n]) {
A[C[c]]+=A[C[n]];
B[C[c]]+=B[C[n]];
while(!Q[C[n]].empty()) {
Q[C[c]].push(Q[C[n]].top());
Q[C[n]].pop();
}
}
}
while(Q[C[c]].size()>A[C[c]]+1) Q[C[c]].pop();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, M;
stack<long long> res;
long long ans, x=0;
cin>>N>>M; N+=M; M=1;
for(int i=1;i<N;i++) {
int p, c;
cin>>p>>c;
adj[--p].emplace_back(i,c);
}
prep(0); solve(0); ans=B[C[0]];
while(Q[C[0]].size()) {
res.push(Q[C[0]].top());
Q[C[0]].pop(); M--;
}
while(res.size()) {
ans+=(res.top()-x)*M++;
x=res.top();
res.pop();
}
cout<<ans<<'\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void solve(int)':
fireworks.cpp:50:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(Q[C[c]].size()>A[C[c]]+1) Q[C[c]].pop();
~~~~~~~~~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
13 ms |
16768 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
13 ms |
16896 KB |
Output is correct |
6 |
Correct |
13 ms |
16896 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
13 ms |
16896 KB |
Output is correct |
9 |
Correct |
15 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16768 KB |
Output is correct |
2 |
Correct |
13 ms |
16756 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
13 ms |
16768 KB |
Output is correct |
5 |
Correct |
13 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16896 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
13 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16896 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16896 KB |
Output is correct |
13 |
Correct |
14 ms |
17024 KB |
Output is correct |
14 |
Correct |
13 ms |
16896 KB |
Output is correct |
15 |
Correct |
13 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16896 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
14 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
13 ms |
16768 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
13 ms |
16896 KB |
Output is correct |
6 |
Correct |
13 ms |
16896 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
13 ms |
16896 KB |
Output is correct |
9 |
Correct |
15 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16756 KB |
Output is correct |
13 |
Correct |
13 ms |
16768 KB |
Output is correct |
14 |
Correct |
13 ms |
16768 KB |
Output is correct |
15 |
Correct |
13 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16896 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
13 ms |
16768 KB |
Output is correct |
20 |
Correct |
13 ms |
16896 KB |
Output is correct |
21 |
Correct |
14 ms |
16768 KB |
Output is correct |
22 |
Correct |
14 ms |
16896 KB |
Output is correct |
23 |
Correct |
14 ms |
17024 KB |
Output is correct |
24 |
Correct |
13 ms |
16896 KB |
Output is correct |
25 |
Correct |
13 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
13 ms |
16896 KB |
Output is correct |
28 |
Correct |
13 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16896 KB |
Output is correct |
31 |
Correct |
14 ms |
16896 KB |
Output is correct |
32 |
Correct |
17 ms |
16896 KB |
Output is correct |
33 |
Correct |
18 ms |
17024 KB |
Output is correct |
34 |
Correct |
16 ms |
17024 KB |
Output is correct |
35 |
Correct |
16 ms |
17152 KB |
Output is correct |
36 |
Correct |
16 ms |
17152 KB |
Output is correct |
37 |
Correct |
17 ms |
17152 KB |
Output is correct |
38 |
Correct |
17 ms |
17280 KB |
Output is correct |
39 |
Correct |
16 ms |
17152 KB |
Output is correct |
40 |
Correct |
15 ms |
17664 KB |
Output is correct |
41 |
Correct |
16 ms |
17664 KB |
Output is correct |
42 |
Correct |
16 ms |
17152 KB |
Output is correct |
43 |
Correct |
16 ms |
17408 KB |
Output is correct |
44 |
Correct |
17 ms |
17408 KB |
Output is correct |
45 |
Correct |
18 ms |
17408 KB |
Output is correct |
46 |
Correct |
17 ms |
17408 KB |
Output is correct |
47 |
Correct |
18 ms |
17408 KB |
Output is correct |
48 |
Correct |
16 ms |
17280 KB |
Output is correct |
49 |
Correct |
17 ms |
17280 KB |
Output is correct |
50 |
Correct |
16 ms |
17152 KB |
Output is correct |
51 |
Correct |
17 ms |
17280 KB |
Output is correct |
52 |
Correct |
16 ms |
17280 KB |
Output is correct |
53 |
Correct |
17 ms |
17280 KB |
Output is correct |
54 |
Correct |
16 ms |
17296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
13 ms |
16768 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
13 ms |
16896 KB |
Output is correct |
6 |
Correct |
13 ms |
16896 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
13 ms |
16896 KB |
Output is correct |
9 |
Correct |
15 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16756 KB |
Output is correct |
13 |
Correct |
13 ms |
16768 KB |
Output is correct |
14 |
Correct |
13 ms |
16768 KB |
Output is correct |
15 |
Correct |
13 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16896 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
13 ms |
16768 KB |
Output is correct |
20 |
Correct |
13 ms |
16896 KB |
Output is correct |
21 |
Correct |
14 ms |
16768 KB |
Output is correct |
22 |
Correct |
14 ms |
16896 KB |
Output is correct |
23 |
Correct |
14 ms |
17024 KB |
Output is correct |
24 |
Correct |
13 ms |
16896 KB |
Output is correct |
25 |
Correct |
13 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
13 ms |
16896 KB |
Output is correct |
28 |
Correct |
13 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16896 KB |
Output is correct |
31 |
Correct |
14 ms |
16896 KB |
Output is correct |
32 |
Correct |
17 ms |
16896 KB |
Output is correct |
33 |
Correct |
18 ms |
17024 KB |
Output is correct |
34 |
Correct |
16 ms |
17024 KB |
Output is correct |
35 |
Correct |
16 ms |
17152 KB |
Output is correct |
36 |
Correct |
16 ms |
17152 KB |
Output is correct |
37 |
Correct |
17 ms |
17152 KB |
Output is correct |
38 |
Correct |
17 ms |
17280 KB |
Output is correct |
39 |
Correct |
16 ms |
17152 KB |
Output is correct |
40 |
Correct |
15 ms |
17664 KB |
Output is correct |
41 |
Correct |
16 ms |
17664 KB |
Output is correct |
42 |
Correct |
16 ms |
17152 KB |
Output is correct |
43 |
Correct |
16 ms |
17408 KB |
Output is correct |
44 |
Correct |
17 ms |
17408 KB |
Output is correct |
45 |
Correct |
18 ms |
17408 KB |
Output is correct |
46 |
Correct |
17 ms |
17408 KB |
Output is correct |
47 |
Correct |
18 ms |
17408 KB |
Output is correct |
48 |
Correct |
16 ms |
17280 KB |
Output is correct |
49 |
Correct |
17 ms |
17280 KB |
Output is correct |
50 |
Correct |
16 ms |
17152 KB |
Output is correct |
51 |
Correct |
17 ms |
17280 KB |
Output is correct |
52 |
Correct |
16 ms |
17280 KB |
Output is correct |
53 |
Correct |
17 ms |
17280 KB |
Output is correct |
54 |
Correct |
16 ms |
17296 KB |
Output is correct |
55 |
Correct |
21 ms |
17792 KB |
Output is correct |
56 |
Correct |
48 ms |
20728 KB |
Output is correct |
57 |
Correct |
88 ms |
23616 KB |
Output is correct |
58 |
Correct |
102 ms |
25456 KB |
Output is correct |
59 |
Correct |
142 ms |
28380 KB |
Output is correct |
60 |
Correct |
185 ms |
31216 KB |
Output is correct |
61 |
Correct |
217 ms |
33128 KB |
Output is correct |
62 |
Correct |
236 ms |
34924 KB |
Output is correct |
63 |
Correct |
319 ms |
38672 KB |
Output is correct |
64 |
Correct |
308 ms |
39528 KB |
Output is correct |
65 |
Correct |
146 ms |
71032 KB |
Output is correct |
66 |
Correct |
147 ms |
71032 KB |
Output is correct |
67 |
Correct |
153 ms |
33504 KB |
Output is correct |
68 |
Correct |
218 ms |
56804 KB |
Output is correct |
69 |
Correct |
262 ms |
54244 KB |
Output is correct |
70 |
Correct |
253 ms |
54116 KB |
Output is correct |
71 |
Correct |
278 ms |
54620 KB |
Output is correct |
72 |
Correct |
289 ms |
54872 KB |
Output is correct |
73 |
Correct |
246 ms |
51164 KB |
Output is correct |
74 |
Correct |
250 ms |
51296 KB |
Output is correct |
75 |
Correct |
251 ms |
49760 KB |
Output is correct |
76 |
Correct |
251 ms |
49760 KB |
Output is correct |
77 |
Correct |
256 ms |
48480 KB |
Output is correct |
78 |
Correct |
247 ms |
47336 KB |
Output is correct |
79 |
Correct |
242 ms |
46172 KB |
Output is correct |