답안 #485769

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485769 2021-11-09T10:22:03 Z milisav Fireworks (APIO16_fireworks) C++14
100 / 100
420 ms 151032 KB
#include<bits/stdc++.h>
#define maxn 1000000
using namespace std;
int n,m;
vector<int> a[maxn];
int p[maxn];
int c[maxn];
long long ans;
multiset<long long> x[maxn];
long long t[maxn];
int pt[maxn];
vector<long long> cur;
void dfs(int u) {
	if(a[u].size()==0) {
		x[u].insert(c[u]);
		x[u].insert(c[u]);
		pt[u]=u;
		t[u]=c[u];
		return;
	}
	for(auto v:a[u]) {
		dfs(v);
	}
	pt[u]=0;
	for(auto v:a[u]) {
		if(x[pt[v]].size()>x[pt[u]].size()) {
			pt[u]=pt[v];
		}
	}
	for(auto v:a[u]) {
		if(pt[u]!=pt[v]) {
			for(auto c:x[pt[v]]) {
				x[pt[u]].insert(c);
			}
		}
	}
	int sz=a[u].size();
	long long sum=0;
	for(int i=0;i<sz-1;i++) {
		sum+=(*x[pt[u]].rbegin());
		x[pt[u]].erase(--x[pt[u]].end());
	}
	long long p1=(*x[pt[u]].rbegin());
	x[pt[u]].erase(--x[pt[u]].end());
	long long p2=(*x[pt[u]].rbegin());
	x[pt[u]].erase(--x[pt[u]].end());
	x[pt[u]].insert(p1+c[u]);
	x[pt[u]].insert(p2+c[u]);
	t[u]=p1+c[u];
	ans+=(sum-(sz-1)*p1);
	for(auto v:a[u]) {
		ans+=(p1-t[v]);
	}
}
int main() {
	scanf("%d %d",&n,&m);
	for(int i=2;i<=n+m;i++) {
		scanf("%d %d",&p[i],&c[i]);
		a[p[i]].push_back(i);
	}
	dfs(1);
	printf("%lld\n",ans);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |  scanf("%d %d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   scanf("%d %d",&p[i],&c[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70732 KB Output is correct
2 Correct 31 ms 70720 KB Output is correct
3 Correct 31 ms 70748 KB Output is correct
4 Correct 33 ms 70784 KB Output is correct
5 Correct 31 ms 70732 KB Output is correct
6 Correct 32 ms 70780 KB Output is correct
7 Correct 32 ms 70708 KB Output is correct
8 Correct 31 ms 70672 KB Output is correct
9 Correct 31 ms 70732 KB Output is correct
10 Correct 31 ms 70700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 70768 KB Output is correct
2 Correct 33 ms 70764 KB Output is correct
3 Correct 39 ms 70764 KB Output is correct
4 Correct 34 ms 70660 KB Output is correct
5 Correct 32 ms 70732 KB Output is correct
6 Correct 33 ms 70732 KB Output is correct
7 Correct 32 ms 70728 KB Output is correct
8 Correct 35 ms 70724 KB Output is correct
9 Correct 37 ms 70668 KB Output is correct
10 Correct 34 ms 70704 KB Output is correct
11 Correct 32 ms 70792 KB Output is correct
12 Correct 32 ms 70748 KB Output is correct
13 Correct 32 ms 70852 KB Output is correct
14 Correct 31 ms 70792 KB Output is correct
15 Correct 31 ms 70732 KB Output is correct
16 Correct 31 ms 70812 KB Output is correct
17 Correct 32 ms 70732 KB Output is correct
18 Correct 40 ms 70692 KB Output is correct
19 Correct 33 ms 70732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70732 KB Output is correct
2 Correct 31 ms 70720 KB Output is correct
3 Correct 31 ms 70748 KB Output is correct
4 Correct 33 ms 70784 KB Output is correct
5 Correct 31 ms 70732 KB Output is correct
6 Correct 32 ms 70780 KB Output is correct
7 Correct 32 ms 70708 KB Output is correct
8 Correct 31 ms 70672 KB Output is correct
9 Correct 31 ms 70732 KB Output is correct
10 Correct 31 ms 70700 KB Output is correct
11 Correct 33 ms 70768 KB Output is correct
12 Correct 33 ms 70764 KB Output is correct
13 Correct 39 ms 70764 KB Output is correct
14 Correct 34 ms 70660 KB Output is correct
15 Correct 32 ms 70732 KB Output is correct
16 Correct 33 ms 70732 KB Output is correct
17 Correct 32 ms 70728 KB Output is correct
18 Correct 35 ms 70724 KB Output is correct
19 Correct 37 ms 70668 KB Output is correct
20 Correct 34 ms 70704 KB Output is correct
21 Correct 32 ms 70792 KB Output is correct
22 Correct 32 ms 70748 KB Output is correct
23 Correct 32 ms 70852 KB Output is correct
24 Correct 31 ms 70792 KB Output is correct
25 Correct 31 ms 70732 KB Output is correct
26 Correct 31 ms 70812 KB Output is correct
27 Correct 32 ms 70732 KB Output is correct
28 Correct 40 ms 70692 KB Output is correct
29 Correct 33 ms 70732 KB Output is correct
30 Correct 32 ms 70804 KB Output is correct
31 Correct 33 ms 70852 KB Output is correct
32 Correct 35 ms 70872 KB Output is correct
33 Correct 33 ms 71068 KB Output is correct
34 Correct 37 ms 71260 KB Output is correct
35 Correct 35 ms 71196 KB Output is correct
36 Correct 34 ms 71268 KB Output is correct
37 Correct 35 ms 71336 KB Output is correct
38 Correct 44 ms 71452 KB Output is correct
39 Correct 36 ms 71356 KB Output is correct
40 Correct 35 ms 71772 KB Output is correct
41 Correct 34 ms 71772 KB Output is correct
42 Correct 33 ms 71124 KB Output is correct
43 Correct 34 ms 71656 KB Output is correct
44 Correct 35 ms 71608 KB Output is correct
45 Correct 35 ms 71600 KB Output is correct
46 Correct 35 ms 71856 KB Output is correct
47 Correct 36 ms 71876 KB Output is correct
48 Correct 37 ms 71712 KB Output is correct
49 Correct 42 ms 71776 KB Output is correct
50 Correct 34 ms 71604 KB Output is correct
51 Correct 35 ms 71508 KB Output is correct
52 Correct 34 ms 71580 KB Output is correct
53 Correct 36 ms 71672 KB Output is correct
54 Correct 37 ms 71752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70732 KB Output is correct
2 Correct 31 ms 70720 KB Output is correct
3 Correct 31 ms 70748 KB Output is correct
4 Correct 33 ms 70784 KB Output is correct
5 Correct 31 ms 70732 KB Output is correct
6 Correct 32 ms 70780 KB Output is correct
7 Correct 32 ms 70708 KB Output is correct
8 Correct 31 ms 70672 KB Output is correct
9 Correct 31 ms 70732 KB Output is correct
10 Correct 31 ms 70700 KB Output is correct
11 Correct 33 ms 70768 KB Output is correct
12 Correct 33 ms 70764 KB Output is correct
13 Correct 39 ms 70764 KB Output is correct
14 Correct 34 ms 70660 KB Output is correct
15 Correct 32 ms 70732 KB Output is correct
16 Correct 33 ms 70732 KB Output is correct
17 Correct 32 ms 70728 KB Output is correct
18 Correct 35 ms 70724 KB Output is correct
19 Correct 37 ms 70668 KB Output is correct
20 Correct 34 ms 70704 KB Output is correct
21 Correct 32 ms 70792 KB Output is correct
22 Correct 32 ms 70748 KB Output is correct
23 Correct 32 ms 70852 KB Output is correct
24 Correct 31 ms 70792 KB Output is correct
25 Correct 31 ms 70732 KB Output is correct
26 Correct 31 ms 70812 KB Output is correct
27 Correct 32 ms 70732 KB Output is correct
28 Correct 40 ms 70692 KB Output is correct
29 Correct 33 ms 70732 KB Output is correct
30 Correct 32 ms 70804 KB Output is correct
31 Correct 33 ms 70852 KB Output is correct
32 Correct 35 ms 70872 KB Output is correct
33 Correct 33 ms 71068 KB Output is correct
34 Correct 37 ms 71260 KB Output is correct
35 Correct 35 ms 71196 KB Output is correct
36 Correct 34 ms 71268 KB Output is correct
37 Correct 35 ms 71336 KB Output is correct
38 Correct 44 ms 71452 KB Output is correct
39 Correct 36 ms 71356 KB Output is correct
40 Correct 35 ms 71772 KB Output is correct
41 Correct 34 ms 71772 KB Output is correct
42 Correct 33 ms 71124 KB Output is correct
43 Correct 34 ms 71656 KB Output is correct
44 Correct 35 ms 71608 KB Output is correct
45 Correct 35 ms 71600 KB Output is correct
46 Correct 35 ms 71856 KB Output is correct
47 Correct 36 ms 71876 KB Output is correct
48 Correct 37 ms 71712 KB Output is correct
49 Correct 42 ms 71776 KB Output is correct
50 Correct 34 ms 71604 KB Output is correct
51 Correct 35 ms 71508 KB Output is correct
52 Correct 34 ms 71580 KB Output is correct
53 Correct 36 ms 71672 KB Output is correct
54 Correct 37 ms 71752 KB Output is correct
55 Correct 42 ms 72636 KB Output is correct
56 Correct 70 ms 78052 KB Output is correct
57 Correct 104 ms 83656 KB Output is correct
58 Correct 119 ms 87528 KB Output is correct
59 Correct 160 ms 93156 KB Output is correct
60 Correct 197 ms 98916 KB Output is correct
61 Correct 214 ms 102724 KB Output is correct
62 Correct 275 ms 106708 KB Output is correct
63 Correct 292 ms 114320 KB Output is correct
64 Correct 329 ms 115148 KB Output is correct
65 Correct 159 ms 133084 KB Output is correct
66 Correct 151 ms 133060 KB Output is correct
67 Correct 171 ms 91112 KB Output is correct
68 Correct 258 ms 128324 KB Output is correct
69 Correct 290 ms 125168 KB Output is correct
70 Correct 299 ms 125124 KB Output is correct
71 Correct 319 ms 151032 KB Output is correct
72 Correct 327 ms 150852 KB Output is correct
73 Correct 331 ms 138388 KB Output is correct
74 Correct 337 ms 138292 KB Output is correct
75 Correct 336 ms 137728 KB Output is correct
76 Correct 331 ms 138020 KB Output is correct
77 Correct 332 ms 135620 KB Output is correct
78 Correct 420 ms 133204 KB Output is correct
79 Correct 385 ms 137584 KB Output is correct