Codeforces 1120D (树形DP 或 最小生成树)
题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543
思路看这篇:https://blog.csdn.net/corsica6/article/details/88115948
有个坑点,不能深搜去找具体方案,不然 test14 会 MLE(或许是本蒟蒻写丑了)
代码:
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int, int>
using namespace std;
const int maxn = 200010;
int head[maxn], Next[maxn * 2], ver[maxn * 2], tot;
int a[maxn];
LL dp[maxn][2], sum[maxn], f[maxn];
bool v[maxn][2];
bool res[maxn];
vector<int> ans;
bool is_leaf[maxn];
void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
void dfs1(int x, int fa) {
int cnt = 0;
f[x] = fa;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(y == fa) continue;
dfs1(y, x);
sum[x] += dp[y][0];
cnt++;
}
if(cnt == 0) {
dp[x][0] = a[x];
dp[x][1] = 0;
is_leaf[x] = 1;
return;
}
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(y == fa) continue;
dp[x][0] = min(dp[x][0], sum[x] - dp[y][0] + dp[y][1] + a[x]);
dp[x][1] = min(dp[x][1], sum[x] - dp[y][0] + dp[y][1]);
}
dp[x][0] = min(dp[x][0], sum[x]);
} void bfs() {
queue<pii> q;
q.push(make_pair(1, 0));
while(!q.empty()) {
pii tmp = q.front();
q.pop();
int x = tmp.first, flag = tmp.second;
if(v[x][flag]) continue;
v[x][flag] = 1;
if(flag == 0) {
int pos = -1, cnt = 0;
if(is_leaf[x]) {
res[x] = 1;
continue;
}
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(y == f[x]) continue;
if(dp[x][flag] == sum[x] - dp[y][0] + dp[y][1] + a[x]) {
if(v[y][1]) continue;
res[x] = 1;
q.push(make_pair(y, 1));
pos = y;
cnt++;
}
}
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(v[y][0]) continue;
if(y == f[x] || y == pos) continue;
q.push(make_pair(y, 0));
}
if(cnt > 1 || (sum[x] == dp[x][0] && pos != -1)) {
if(v[pos][0]) continue;
q.push(make_pair(pos, 0));
}
} else {
int pos = -1, cnt = 0;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(y == f[x]) continue;
if(dp[x][flag] == sum[x] - dp[y][0] + dp[y][1]) {
if(v[y][1]) continue;
q.push(make_pair(y, 1));
pos = y;
cnt++;
}
}
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(v[y][0]) continue;
if(y == f[x] || y == pos) continue;
q.push(make_pair(y, 0));
}
if(cnt > 1) {
if(v[pos][0]) continue;
q.push(make_pair(pos, 0));
}
}
}
}
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
memset(dp, 0x3f, sizeof(dp));
dfs1(1, -1);
bfs();
for (int i = 1; i <= n; i++) {
if(res[i])
ans.push_back(i);
}
printf("%lld %d\n", dp[1][0], ans.size());
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++)
printf("%d ", ans[i]);
printf("\n");
}
最小生成树解法先留个坑在这。。。
补坑:
思路看这篇博客:https://www.cnblogs.com/river-flows-in-you/p/10596821.html
说一下我个人的理解:把每个叶子节点看成新图的顶点,对于原树中的每个顶点,我们可以计算出它影响哪些叶子节点,用差分的思想连边。只要求出了生成树就说明可以任意取,因为形成生成树之后,我们对每个点的赋值操作就可以类比在生成树上遍历的过程。
代码:
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 200010;
int head[maxn], Next[maxn * 2], ver[maxn * 2], tot;
int sz[maxn], cnt, l[maxn], r[maxn], a[maxn], f[maxn];
bool v[maxn];
struct Edge{
int u, v, w, id;
bool operator < (const Edge& rhs) const {
return w < rhs.w;
}
};
Edge b[maxn];
void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
void dfs(int x, int fa) {
sz[x] = 1;
l[x] = INF, r[x] = -1;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(y == fa) continue;
dfs(y, x);
sz[x] += sz[y];
l[x] = min(l[x], l[y]);
r[x] = max(r[x], r[y]);
}
if(sz[x] == 1) {
l[x] = r[x] = ++cnt;
}
b[x] = (Edge){l[x], r[x] + 1, a[x], x};
}
int get(int x) {
if(x == f[x]) return x;
return f[x] = get(f[x]);
}
int main() {
int n, x, y;
scanf("%d", &n);
int sum = 0;
LL ans = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, -1);
sort(b + 1, b + 1 + n);
cnt++;
for (int i = 1; i <= cnt; i++) f[i] = i;
for(int L = 1, R; L <= n; L = R + 1) {
R = L;
while(b[L].w == b[R + 1].w && R < n) R++;
for (int i = L; i <= R; i++) {
x = get(b[i].u), y = get(b[i].v);
if(x != y) {
v[b[i].id] = 1;
sum++;
}
}
for (int i = L; i <= R; i++) {
x = get(b[i].u), y = get(b[i].v);
if(x != y) {
ans += b[i].w;
// printf("%lld %d\n", ans, b[i].w);
f[x] = y;
}
}
}
// printf("%lld %d\n", ans, sum);
cout << ans << " " << sum << endl;
for (int i = 1; i <= n; i++)
if(v[i]) printf("%d ", i);
}
Codeforces 1120D (树形DP 或 最小生成树)的更多相关文章
- Codeforces 1153D 树形DP
题意:有一个游戏,规则如下:每个点有一个标号,为max或min, max是指这个点的值是所有子节点中值最大的那一个,min同理.问如何给这颗树的叶子节点赋值,可以让这棵树的根节点值最大. 思路:很明显 ...
- Codeforces 1088E 树形dp+思维
比赛的时候看到题意没多想就放弃了.结果最后D也没做出来,还掉分了,所以还是题目做的太少,人太菜. 回到正题: 题意:一棵树,点带权值,然后求k个子连通块,使得k个连通块内所有的点权值相加作为分子除以k ...
- Codeforces 1179D 树形DP 斜率优化
题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...
- CodeForces - 337D 树形dp
题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...
- CodeForces 219D 树形DP
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- codeforces 337D 树形DP Book of Evil
原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...
- Up and Down the Tree CodeForces - 1065F (树形dp)
链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...
- codeforces 1053D 树形DP
题意:给一颗树,1为根节点,有两种节点,min或者max,min节点的值是它的子节点的值中最小的,max节点的值是它的子节点的值中最大的,若共有k个叶子,叶子的值依次为1~k. 问给每个叶子的值赋为几 ...
- Codeforces 735E 树形DP
题意:给你一棵树,你需要在这棵树上选择一些点染成黑色,要求染色之后树中任意节点到离它最近的黑色节点的距离不超过m,问满足这种条件的染色方案有多少种? 思路:设dp[x][i]为以x为根的子树中,离x点 ...
随机推荐
- Windows手动安装Apache,MySql服务
绿色版的Apache,Mysql需要以服务方式运行.或者由于某种原因服务管理器里面没有服务项了,可以进行手工注册服务和启并服务. # Apache Apache进到安装目录的bin目录下,运行dos命 ...
- 通过设置debug_options调试squid
debug_options用于设置输出到access.log中的调试信息的模块和级别,默认为ALL,1. ALL指所有模块,每个源文件被赋予一个唯一的模块号 1指级别,完全调试的级别为9.很显然,设置 ...
- HihoCoder - 1615矩阵游戏II(贪心)
矩阵游戏II 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个NxN的整数矩阵,小Hi每次操作可以选择两列,将这两列中的所有数变成它的相反数. 小Hi可以进行任意 ...
- 剑指Offer面试题:13.合并两个排序的链表
一 题目:合并两个排序的链表 题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的.例如输入下图中的链表1和链表2,则合并之后的升序链表如链表3所示. 二 代码实现 te ...
- avalon 总线时序关系理解
对于读,等待时间指的是从端口捕获数据的时间相对于read信号的延时 建立时间指的是read信号相对于chipselect和addr的延时时间 对于写,等待时间指的是相对于非等待情况下各个信号的延时时间 ...
- #510. 「LibreOJ NOI Round #1」动态几何问题
题目: 题解: 几何部分,先证明一下 \(KX = \sqrt{a},YL = \sqrt{b}\) 设左侧的圆心为 \(O\) ,连接 \(OK\) ,我们有 \(OK = r\). 然后有 \(r ...
- var ev = ev || event
event是事件对象(也是window的属性),但不是标准的,只有IE支持. 在W3C标准支持的浏览器下事件对象是引发事件函数的第一个参数,参数名随意. 所以,我们一般使用事件对象: function ...
- web 模板引擎
baiduTemplate: http://baidufe.github.io/BaiduTemplate/ artTemplate: https://github.com/aui/artTempl ...
- .Net Remoting编程 ---- 系列文章
.Net Remoting(应用程序域) - Part.1 摘要: 本文是.Net Remoting系列的第一篇文章,讲述了Remoting的“前驱知识点”--应用程序域.传值封送(Marshal b ...
- bzoj 2259 [Oibh]新型计算机 ——最短路(建图)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2259 不是 n^2 条边!连那条边权为0的边之后,只要每个位置向它的前一个位置和后一个位置连 ...