Color a Tree

题目链接

好不可做?可以尝试一下DP贪心网络流。DP 似乎没法做,网络流也不太行,所以试一下贪心。

考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它;否则,选完它父亲就一定先选它。于是我们可以把它缩成一个点。

但是我们并不知道缩点后的权值是多少,这样就没法继续缩下去。我们考虑一对被缩点的父子 \(x,y\),以及全局中的另一个点 \(a\),什么时候会先选 \(a\),什么时候先选 \(x,y\)。如果先选 \(x,y\),那么就有 \(x + 2y + 3a \le a + 2x + 3y\),整理得 \(a \le \dfrac{x + y}{2}\),即我们可以把点中的均值当作点权。

于是并查集 + 堆模拟即可。注意重载运算符时当均值相同时要比较其余变量,否则可删堆将出错。

struct node {
ll val, siz;
int cur;
bool operator <(const node a) const {
if (val * a.siz != siz * a.val) return val * a.siz < siz * a.val;
else if (val != a.val) return val < a.val;
else if (siz != a.siz) return siz < a.siz;
return cur < a.cur;
}
node operator +(const node a) const {
memo += siz * a.val;
return (node){val + a.val, siz + a.siz, cur};
}
bool operator ==(const node a) const { return cur == a.cur && val == a.val && siz == a.siz; }
}ans, tmp[N];
struct Heap {
priority_queue<node> q, w;
inline node top() { while (w.size() && q.top() == w.top()) q.pop(), w.pop(); return q.top(); }
inline void del(node x) { w.push(x); }
inline void push(node x) { q.push(x); }
inline int size() { return q.size() - w.size(); }
}hp;
int v[N];
int fa[N];
bool vis[N];
int find(int cur) { return fa[cur] == cur ? cur : fa[cur] = find(fa[cur]); } int main() {
int n, r;
read(n), read(r);
ans = (node){0, 0, 0};
while (n || r) {
memo = 0;
for (register int i = 1; i <= n; ++i) read(v[i]), tmp[i] = (node){v[i], 1, i}, hp.push(tmp[i]), fa[i] = i;
for (register int i = 1; i < n; ++i) {
int u, v; read(u), read(v); fath[v] = u;
}
while (hp.size()) {
node nd = hp.top(); hp.del(nd);
int rt = nd.cur, faa = find(fath[rt]);
if (!faa || vis[faa]) { ans = ans + nd; vis[rt] = true; continue; }
hp.del(tmp[faa]);
tmp[faa] = tmp[faa] + tmp[rt]; fa[rt] = fath[rt];
hp.push(tmp[faa]);
}
printf("%lld\n", memo + ans.val);
ans = (node){0, 0, 0};
while (hp.q.size()) hp.q.pop();
while (hp.w.size()) hp.w.pop();
for (register int i = 1; i <= n; ++i) fath[i] = 0, vis[i] = false; read(n), read(r);
}
return 0;
}

排列

题目链接

题意较为难懂,可以理解成,如果第 \(i\) 个数跑到了第 \(j\) 个位置,那么 \(j\) 及之前的位置就都不能有 \(a\) 值为 \(i\) 的数。换句话说,值为 \(i\) 的数能够出现,当且仅当第 \(i\) 个数已经出现。

题意转化以后就比较好做了。我们可以把这种限制关系用边来表示一下,结果应该是基环树森林。显然如果有环一定无解,那么结果是森林。然后再看最小化的那个值,发现实际上和 Color a Tree 本质相同,于是直接搬过来即可。

Color a Tree & 排列的更多相关文章

  1. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  2. Color a Tree[HDU1055]

    Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. Color a Tree HDU - 6241

    /* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...

  4. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  5. Poj2054 color a tree && [HNOI/AHOI2018]排列

    https://zybuluo.com/ysner/note/1120723 题面 原题 某省选强化题 大致意思是给你一颗树,选父亲后才能选儿子. 每个点对答案的贡献为你在第几次选这个点 × 该点权值 ...

  6. hdu 6241 Color a Tree 2017 CCPC 哈理工站 L

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

  7. HDU - 6241 :Color a Tree(不错的二分)

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

  8. POJ 2054 Color a Tree#贪心(难,好题)

    题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...

  9. UVA - 1205 Color a Tree

    大意就是给你一颗树,每个点有一个权值w[i],求一个排列使得 所有的父亲都在儿子前面 并且排列的权值最小. 排列的权值在这里定义为 Σ i * w[p[i]]   ,其中p[i] 是排列第i个位置的元 ...

随机推荐

  1. 华为云GaussDB(DWS)内存知识点,你知道吗?

    前言 在日常数据库的使用中,难免会遇到一些内存问题.此次博文主要向大家分享一些华为云数仓GaussDB(DWS)内存的基本框架以及基本视图的使用,以便遇到内存问题后可以有一个基本的判断. 注意,本篇博 ...

  2. NOI Online#1 小记

    虽然只是一个普通的模拟赛,但是毕竟是我第一次参加官方组织的比赛,所以还是写一篇小记纪念一下吧(毕竟经验少,太菜了. 上午一直颓着,随便看了两眼文化课,补了补昨天的化学作业,就当是对明天月考的复习吧(月 ...

  3. Idea 提交配置说明

    Idea 提交配置说明# Auto-update after commit :自动升级后提交 keep files locked :把文件锁上,我想这应该就只能你修改其他开发人不能修改不了的功能 在你 ...

  4. Android BottomNavigationView的用法

    BottomNavigationView是相当于一个导航的标签,但是它的形式就是像QQ,微信之类的界面 这三个图标就是BottomNavigationView的体现. 至于写出后怎样绑定这三个界面,就 ...

  5. 新手阅读 Nebula Graph 源码的姿势

    摘要:在本文中,我们将通过数据流快速学习 Nebula Graph,以用户在客户端输入一条 nGQL 语句 SHOW SPACES 为例,使用 GDB 追踪语句输入时 Nebula Graph 是怎么 ...

  6. 019_go语言中的方法

    代码演示 package main import "fmt" type rect struct { width, heigh int } func (r *rect) area() ...

  7. CSS可见格式化模型

    1.盒模型 1.1 盒子大小 盒模型描述了元素如何显示,以及如何相互作用.相互影响. 页面中的所有元素都被看作一个矩形盒子,这个盒子包含元素的内容.内边距.边框和外边距. 给元素应用的背景会作用于元素 ...

  8. JS 筋斗云案例

    .nav { width: 1000px; height: 60px; line-height: 60px; margin: 0 auto; position: relative; } ul { wi ...

  9. 代码生成器插件与Creator预制体文件解析

    前言 之前写过一篇自动生成脚本的工具,但是我给它起名叫半自动代码生成器.之所以称之为半自动,因为我觉得全自动代码生成器应该做到两点:代码生成+自动绑定.之前的工具只做了代码生成,并没有做自动绑定,所以 ...

  10. windows系统下python setup.py install ---出现cl问题,cpp_extension.py:237: UserWarning: Error checking compiler version for cl: 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byte

    将cpp_extension.py文件中的 原始的是   compiler_info.decode() try: if sys.platform.startswith('linux'): minimu ...