「JSOI2015」salesman

传送门

显然我们为了使收益最大化就直接从子树中选大的就好了。

到达次数的限制就是限制了可以选的子树的数量,因为每次回溯上来都会减一次到达次数。

多种方案的判断就是看自己选中的子树中和没选的子树中是否存在两个值相等的,这样它们就可以通过互换来达到另一种方案,值得注意的是如果选了一个值为 \(0\) 的子树就肯定可以多一种方案出来,因为这颗子树选或不选都是满足最优的。

这里有个小问题:交到BZOJ上面去它会提示你 sort 没有声明,此时需要 #include <cstdlib> ,具体我也不知道为什么。。。

#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} typedef long long LL;
const int _ = 1e5 + 5; int tot, head[_]; struct Edge { int v, nxt; } edge[_ << 1];
inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }, head[u] = tot; } int n, t[_], g[_]; LL a[_], dp[_];
inline bool cmp(int x, int y) { return dp[x] > dp[y]; } inline void dfs(int u, int f) {
vector < int > tmp; tmp.clear();
dp[u] = a[u];
for (rg int i = head[u]; i; i = edge[i].nxt)
if (edge[i].v != f) dfs(edge[i].v, u), tmp.push_back(edge[i].v);
int p = 0, lim = min((int) tmp.size(), t[u] - 1);
sort(tmp.begin(), tmp.end(), cmp);
while (p < lim && dp[tmp[p]] >= 0) dp[u] += dp[tmp[p]], g[u] |= g[tmp[p]], ++p;
if ((p > 0 && p < lim && dp[tmp[p]] == dp[tmp[p - 1]]) || (p > 0 && dp[tmp[p - 1]] == 0)) g[u] = 1;
} int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n);
for (rg int i = 2; i <= n; ++i) read(a[i]); a[1] = 0;
for (rg int i = 2; i <= n; ++i) read(t[i]); t[1] = 2147483647;
for (rg int u, v, i = 1; i < n; ++i) read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dfs(1, 0);
printf("%lld\n", dp[1]);
puts(g[1] ? "solution is not unique" : "solution is unique");
return 0;
}

最新文章

  1. CF733D Kostya the Sculptor[贪心 排序]
  2. Win7下mysql root账户登录提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)解决方案
  3. 基于资源的权限系统-API设计
  4. HTML5 本地存储 localStorage、sessionStorage 的遍历、存储大小限制处理
  5. Web页面速度测试工具
  6. [原创] 更新Ubuntu自带的python2.X版本 ImportError: No module named pip;ImportError: No module named _sqlite3
  7. Android笔记——Windows环境下Android Studio v1.0安装教程
  8. native app
  9. java虚拟机(一)——内存管理机制与OOM异常
  10. SQL SERVER-Delete和Truncate的区别
  11. Linux常用命令英文全称与中文解释Linux系统
  12. python 下划线的使用(转载:安生犹梦 新浪博客)
  13. E - Trees on the level
  14. C#socket通讯两个最经典错误解决方案
  15. tabbar加小红点
  16. Android程序报错 Connection refused 处理
  17. 一键保存网页为PDF
  18. mybatis入门系列一之创建mybatis程序
  19. cocos creator 碰撞检测
  20. mysql 安装问题三:FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper

热门文章

  1. java8 四大核心函数式接口Function、Consumer、Supplier、Predicate(转载)
  2. 第四十二篇 入门机器学习——Numpy的基本操作——索引相关
  3. MySQL关于GTID的一些功能限制
  4. group by分组后对组内数据进行排序
  5. wget 显示网页内容到控制台
  6. AC3 exponent coding
  7. Codeforces 131C . The World is a Theatre(打表组合数)
  8. noobSTL-1-配置器-1
  9. C++11 新特性学习
  10. HDU - 5187 zhx&#39;s contest(快速幂+快速乘法)