Live2D

Solution -「十二省联考2019」春节十二响

题目

题意简述

  link.

  给一棵 \(n\) 个结点的有根树,点带权。把点分为若干组,并要求同组内不存在任何祖先-后代关系。最小化每组内的最大点权之和。

数据规模

  \(n\le2\times10^5\)

Solution

  考虑一个部分分——链。

  当根节点 \(1\) 不是链头,相当于 \(1\) 左右两条链分组,\(1\) 单独一组。而显然,答案是若干点权的和。所以我们只需要贪心地让较大点权与最大点权成为一组来减小答案。

  所以可以用堆——把 \(1\) 左右的点权存入两个堆,每次取出两个堆的最大值构成一组。当一个堆空,另一个堆每个元素构成一组,\(1\) 单独构成一组。

  把这个做法搬到树上——每个结点维护一个堆,堆内存放子树内每个分组的最大值。在当前子树根结点启发式合并每个儿子的堆,并按上述做法贪心地分组计算答案。最后记得将当前子树根的权加入堆。

代码

#include <queue>
#include <cstdio>
#include <vector>

typedef long long LL;

inline int rint () {
	int x = 0, f = 1; char s = getchar ();
	for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
	for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
	return x * f;
}

template<typename Tp>
inline void wint ( Tp x ) {
	if ( x < 0 ) putchar ( '-' ), x = ~ x + 1;
	if ( 9 < x ) wint ( x / 10 );
	putchar ( x % 10 ^ '0' );
}

const int MAXN = 2e5;
int n, ecnt, mem[MAXN + 5], head[MAXN + 5], hid[MAXN + 5];
std :: vector<LL> tmp;
std :: priority_queue<LL> heap[MAXN + 5];

struct Edge { int to, nxt; } graph[MAXN + 5];

inline void link ( const int s, const int t ) { graph[++ ecnt] = { t, head[s] }, head[s] = ecnt; }

inline void DFS ( const int u ) {
	for ( int i = head[hid[u] = u], v; i; i = graph[i].nxt ) {
		DFS ( v = graph[i].to );
		if ( heap[hid[u]].size () < heap[hid[v]].size () ) hid[u] ^= hid[v] ^= hid[u] ^= hid[v];
		for ( ; ! heap[hid[v]].empty (); heap[hid[u]].pop (), heap[hid[v]].pop () ) {
			tmp.push_back ( std :: max ( heap[hid[u]].top (), heap[hid[v]].top () ) );
		}
		for ( ; ! tmp.empty (); tmp.pop_back () ) heap[hid[u]].push ( tmp[tmp.size () - 1] );
	}
	heap[hid[u]].push ( mem[u] );
}

int main () {
	n = rint ();
	for ( int i = 1; i <= n; ++ i ) mem[i] = rint  ();
	for ( int i = 2; i <= n; ++ i ) link ( rint (), i );
	DFS ( 1 ); LL ans = 0;
	for ( ; ! heap[hid[1]].empty (); heap[hid[1]].pop () ) ans += heap[hid[1]].top ();
	wint ( ans ), putchar ( '\n' );
	return 0;
}
posted @ 2020-06-16 09:51  Rainybunny  阅读(110)  评论(0编辑  收藏  举报