답안 #154114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
154114 2019-09-18T11:44:02 Z junhopark Fireworks (APIO16_fireworks) C++14
100 / 100
403 ms 66044 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define all(V) (V).begin(),(V).end()
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int SZ = 3e5+5;
int n, m, piv, ch[SZ], si[SZ];
int pl[SZ], l[SZ], sub[SZ];
LL dep[SZ], sum;
priority_queue<LL> pq[SZ];
vector<pii> v[SZ];
vector<LL> res, test;
void get_dep(int now)
{
	ch[now]=sub[now]=1;
	for (pii c:v[now]) {
		dep[c.fi]=dep[now]+1;
		get_dep(c.fi);
		sub[now]+=sub[c.fi];
	}
}
void solve(int now)
{
	if (now>n) {
		pl[now]=++piv;
		pq[piv].emplace(l[now]);
		pq[piv].emplace(l[now]);
		si[piv]=-1;
		return;
	}
	LL len;
	int p, maxi=-1;
	for (pii c:v[now]) {
		solve(c.fi);
		if (maxi<(int)pq[pl[c.fi]].size()) {
			maxi=(int)pq[pl[c.fi]].size();
			p=c.fi;
			len=l[c.fi];
		}
	}
	pl[now]=pl[p];
	for (pii c:v[now]) {
		if (c.fi==p) continue;
		si[pl[now]]+=si[pl[c.fi]];
		while (pq[pl[c.fi]].size()) {
			pq[pl[now]].emplace(pq[pl[c.fi]].top());
			pq[pl[c.fi]].pop();
		}
	}
	if (now>1) {
		LL cnt=si[pl[now]]+(int)pq[pl[now]].size()-1;
		while (cnt--) pq[pl[now]].pop();
		LL a, b, c;
		a=pq[pl[now]].top();
		pq[pl[now]].pop();
		b=pq[pl[now]].top();
		pq[pl[now]].pop();
		c=pq[pl[now]].top();
		a+=l[now],b+=l[now];
		pq[pl[now]].emplace(a);
		pq[pl[now]].emplace(b);
	}
}
int main()
{
	int i, a, b;
	scanf("%d %d", &n, &m);
	for (i=2; i<=n+m; i++) {
		scanf("%d %d", &a, &b);
		v[a].eb(i,b);
		l[i]=b;
		sum+=b;
	}
	get_dep(1);
	solve(1);
	while (pq[pl[1]].size()) {
		res.eb(pq[pl[1]].top());
		pq[pl[1]].pop();
	}
	LL prev=0, pp=res.size()-1;
	while (si[pl[1]]<0) {
		sum+=(res[pp]-prev)*si[pl[1]];
		si[pl[1]]++;
		prev=res[pp--];
	}
	printf("%lld\n", sum);
	return 0;
}

Compilation message

fireworks.cpp: In function 'void solve(int)':
fireworks.cpp:56:12: warning: variable 'c' set but not used [-Wunused-but-set-variable]
   LL a, b, c;
            ^
fireworks.cpp:34:5: warning: variable 'len' set but not used [-Wunused-but-set-variable]
  LL len;
     ^~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'void solve(int)':
fireworks.cpp:44:14: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
  pl[now]=pl[p];
          ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 17 ms 16760 KB Output is correct
3 Correct 17 ms 16760 KB Output is correct
4 Correct 17 ms 16888 KB Output is correct
5 Correct 17 ms 16760 KB Output is correct
6 Correct 17 ms 16760 KB Output is correct
7 Correct 17 ms 16760 KB Output is correct
8 Correct 17 ms 16888 KB Output is correct
9 Correct 17 ms 16760 KB Output is correct
10 Correct 19 ms 16760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 17 ms 16764 KB Output is correct
3 Correct 21 ms 16840 KB Output is correct
4 Correct 17 ms 16888 KB Output is correct
5 Correct 21 ms 16760 KB Output is correct
6 Correct 20 ms 16760 KB Output is correct
7 Correct 21 ms 16760 KB Output is correct
8 Correct 17 ms 16760 KB Output is correct
9 Correct 18 ms 16760 KB Output is correct
10 Correct 20 ms 16888 KB Output is correct
11 Correct 17 ms 16888 KB Output is correct
12 Correct 17 ms 16888 KB Output is correct
13 Correct 18 ms 16888 KB Output is correct
14 Correct 18 ms 16888 KB Output is correct
15 Correct 17 ms 16888 KB Output is correct
16 Correct 17 ms 16892 KB Output is correct
17 Correct 20 ms 17016 KB Output is correct
18 Correct 18 ms 16888 KB Output is correct
19 Correct 17 ms 16760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 17 ms 16760 KB Output is correct
3 Correct 17 ms 16760 KB Output is correct
4 Correct 17 ms 16888 KB Output is correct
5 Correct 17 ms 16760 KB Output is correct
6 Correct 17 ms 16760 KB Output is correct
7 Correct 17 ms 16760 KB Output is correct
8 Correct 17 ms 16888 KB Output is correct
9 Correct 17 ms 16760 KB Output is correct
10 Correct 19 ms 16760 KB Output is correct
11 Correct 17 ms 16760 KB Output is correct
12 Correct 17 ms 16764 KB Output is correct
13 Correct 21 ms 16840 KB Output is correct
14 Correct 17 ms 16888 KB Output is correct
15 Correct 21 ms 16760 KB Output is correct
16 Correct 20 ms 16760 KB Output is correct
17 Correct 21 ms 16760 KB Output is correct
18 Correct 17 ms 16760 KB Output is correct
19 Correct 18 ms 16760 KB Output is correct
20 Correct 20 ms 16888 KB Output is correct
21 Correct 17 ms 16888 KB Output is correct
22 Correct 17 ms 16888 KB Output is correct
23 Correct 18 ms 16888 KB Output is correct
24 Correct 18 ms 16888 KB Output is correct
25 Correct 17 ms 16888 KB Output is correct
26 Correct 17 ms 16892 KB Output is correct
27 Correct 20 ms 17016 KB Output is correct
28 Correct 18 ms 16888 KB Output is correct
29 Correct 17 ms 16760 KB Output is correct
30 Correct 18 ms 16760 KB Output is correct
31 Correct 18 ms 16888 KB Output is correct
32 Correct 18 ms 16888 KB Output is correct
33 Correct 19 ms 17016 KB Output is correct
34 Correct 23 ms 17016 KB Output is correct
35 Correct 20 ms 17116 KB Output is correct
36 Correct 20 ms 17144 KB Output is correct
37 Correct 21 ms 17272 KB Output is correct
38 Correct 22 ms 17272 KB Output is correct
39 Correct 21 ms 17272 KB Output is correct
40 Correct 20 ms 17528 KB Output is correct
41 Correct 20 ms 17656 KB Output is correct
42 Correct 19 ms 17144 KB Output is correct
43 Correct 21 ms 17528 KB Output is correct
44 Correct 21 ms 17356 KB Output is correct
45 Correct 28 ms 17404 KB Output is correct
46 Correct 21 ms 17400 KB Output is correct
47 Correct 21 ms 17404 KB Output is correct
48 Correct 21 ms 17272 KB Output is correct
49 Correct 25 ms 17400 KB Output is correct
50 Correct 22 ms 17380 KB Output is correct
51 Correct 24 ms 17272 KB Output is correct
52 Correct 21 ms 17400 KB Output is correct
53 Correct 21 ms 17400 KB Output is correct
54 Correct 21 ms 17528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 17 ms 16760 KB Output is correct
3 Correct 17 ms 16760 KB Output is correct
4 Correct 17 ms 16888 KB Output is correct
5 Correct 17 ms 16760 KB Output is correct
6 Correct 17 ms 16760 KB Output is correct
7 Correct 17 ms 16760 KB Output is correct
8 Correct 17 ms 16888 KB Output is correct
9 Correct 17 ms 16760 KB Output is correct
10 Correct 19 ms 16760 KB Output is correct
11 Correct 17 ms 16760 KB Output is correct
12 Correct 17 ms 16764 KB Output is correct
13 Correct 21 ms 16840 KB Output is correct
14 Correct 17 ms 16888 KB Output is correct
15 Correct 21 ms 16760 KB Output is correct
16 Correct 20 ms 16760 KB Output is correct
17 Correct 21 ms 16760 KB Output is correct
18 Correct 17 ms 16760 KB Output is correct
19 Correct 18 ms 16760 KB Output is correct
20 Correct 20 ms 16888 KB Output is correct
21 Correct 17 ms 16888 KB Output is correct
22 Correct 17 ms 16888 KB Output is correct
23 Correct 18 ms 16888 KB Output is correct
24 Correct 18 ms 16888 KB Output is correct
25 Correct 17 ms 16888 KB Output is correct
26 Correct 17 ms 16892 KB Output is correct
27 Correct 20 ms 17016 KB Output is correct
28 Correct 18 ms 16888 KB Output is correct
29 Correct 17 ms 16760 KB Output is correct
30 Correct 18 ms 16760 KB Output is correct
31 Correct 18 ms 16888 KB Output is correct
32 Correct 18 ms 16888 KB Output is correct
33 Correct 19 ms 17016 KB Output is correct
34 Correct 23 ms 17016 KB Output is correct
35 Correct 20 ms 17116 KB Output is correct
36 Correct 20 ms 17144 KB Output is correct
37 Correct 21 ms 17272 KB Output is correct
38 Correct 22 ms 17272 KB Output is correct
39 Correct 21 ms 17272 KB Output is correct
40 Correct 20 ms 17528 KB Output is correct
41 Correct 20 ms 17656 KB Output is correct
42 Correct 19 ms 17144 KB Output is correct
43 Correct 21 ms 17528 KB Output is correct
44 Correct 21 ms 17356 KB Output is correct
45 Correct 28 ms 17404 KB Output is correct
46 Correct 21 ms 17400 KB Output is correct
47 Correct 21 ms 17404 KB Output is correct
48 Correct 21 ms 17272 KB Output is correct
49 Correct 25 ms 17400 KB Output is correct
50 Correct 22 ms 17380 KB Output is correct
51 Correct 24 ms 17272 KB Output is correct
52 Correct 21 ms 17400 KB Output is correct
53 Correct 21 ms 17400 KB Output is correct
54 Correct 21 ms 17528 KB Output is correct
55 Correct 27 ms 18040 KB Output is correct
56 Correct 61 ms 21512 KB Output is correct
57 Correct 97 ms 24728 KB Output is correct
58 Correct 129 ms 27080 KB Output is correct
59 Correct 169 ms 30192 KB Output is correct
60 Correct 234 ms 33488 KB Output is correct
61 Correct 253 ms 36412 KB Output is correct
62 Correct 306 ms 38240 KB Output is correct
63 Correct 403 ms 42476 KB Output is correct
64 Correct 371 ms 43372 KB Output is correct
65 Correct 187 ms 66044 KB Output is correct
66 Correct 170 ms 65912 KB Output is correct
67 Correct 243 ms 38136 KB Output is correct
68 Correct 254 ms 56904 KB Output is correct
69 Correct 307 ms 54628 KB Output is correct
70 Correct 308 ms 54888 KB Output is correct
71 Correct 305 ms 59480 KB Output is correct
72 Correct 311 ms 59452 KB Output is correct
73 Correct 275 ms 55788 KB Output is correct
74 Correct 279 ms 55944 KB Output is correct
75 Correct 279 ms 54684 KB Output is correct
76 Correct 279 ms 54924 KB Output is correct
77 Correct 277 ms 53472 KB Output is correct
78 Correct 280 ms 52552 KB Output is correct
79 Correct 300 ms 57096 KB Output is correct