Submission #652851

# Submission time Handle Problem Language Result Execution time Memory
652851 2022-10-24T22:08:55 Z lovrot Fireworks (APIO16_fireworks) C++17
19 / 100
24 ms 33772 KB
#include <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back 

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

struct func{
	ll a;
	ll b;
	int ind;
};

const int N = 300010;

int n, m, siz[N];
ll edge[N];

vector<int> g[N];
priority_queue<ll> slope[N];

func merge(func a, func b){
	func ret;
	ret.a = a.a + b.a;
	ret.b = a.b + b.b;	
	ret.ind = a.ind;
	
	while(!slope[b.ind].empty()){ 
		slope[a.ind].push(slope[b.ind].top()); 
		slope[b.ind].pop();
	}
	return ret;
}

func f(int u){
	func ret;
	if(g[u].empty()){
		ret.a = 1;
		ret.b = -edge[u];
		ret.ind = u;
		slope[u].push(edge[u]);
		slope[u].push(edge[u]);
		return ret;
	} 
	ret = f(g[u][0]);
	for(int i = 1; i < g[u].size(); i++){
		ret = merge(ret, f(g[u][i]));
	}
	int ind = ret.ind;
	while(ret.a > 1){
		ll val = slope[ind].top();
		slope[ind].pop();
		ret.a--;
		ret.b += val;
	}
	ll val = slope[ind].top() + edge[u];
	slope[ind].pop(); 
	ll val2 = slope[ind].top() + edge[u];
	slope[ind].pop(); 
	slope[ind].push(val);
	slope[ind].push(val2);
	ret.b -= edge[u];
	return ret;
}

int dfs(int u){
	int ret = 1;
	for(int v : g[u]){
		ret += dfs(v);
	}
	siz[u] = ret;
	return ret;
}

int main(){
	scanf("%d%d\n", &n, &m);
	
	for(int i = 2; i <= n + m; i++){
		int par, val;
		scanf("%d%d", &par, &val);
		g[par].pb(i);
		edge[i] = val;
	}
	
	dfs(1);
	
	for(int i = 1; i <= n; i++){
		sort(g[i].begin(), g[i].end(), [](int a, int b) -> bool {return siz[a] >= siz[b];});	
	}
	
	func res = f(1);
	ll ans = slope[res.ind].top() + res.b;
	printf("%lld\n", ans);
	return 0;
}

Compilation message

fireworks.cpp: In function 'func f(int)':
fireworks.cpp:50:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |  for(int i = 1; i < g[u].size(); i++){
      |                 ~~^~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d%d\n", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~
fireworks.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |   scanf("%d%d", &par, &val);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Runtime error 24 ms 33772 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 9 ms 16708 KB Output is correct
4 Correct 9 ms 16724 KB Output is correct
5 Correct 9 ms 16724 KB Output is correct
6 Correct 9 ms 16756 KB Output is correct
7 Correct 9 ms 16724 KB Output is correct
8 Correct 9 ms 16644 KB Output is correct
9 Correct 9 ms 16724 KB Output is correct
10 Correct 10 ms 16724 KB Output is correct
11 Correct 10 ms 16724 KB Output is correct
12 Correct 10 ms 16724 KB Output is correct
13 Correct 9 ms 16724 KB Output is correct
14 Correct 10 ms 16728 KB Output is correct
15 Correct 9 ms 16680 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 9 ms 16724 KB Output is correct
18 Correct 9 ms 16724 KB Output is correct
19 Correct 10 ms 16724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Runtime error 24 ms 33772 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Runtime error 24 ms 33772 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -