poj1848 Tree
.....是我多想了。
我想开f[][0~3],看到百度上的题解都是[0~2]的,我就改了
方程不是特别难想。。
f代表最小代价
f[i][0]是子树有环过i
f[i][1]是子树除了i都成环了
f[i][2]是子树有条链过i(最少2个点,包括i)
转移见代码(有状态了转移很好想)。
/**
* Problem:POJ1848
* Author:Shun Yao
* Time:2013.9.2
* Result:Accepted
* Memo:TreeDP
*/ #include <cstdio> #define MAXN 110
#define inf 1000 long f[MAXN][3]; long min(long x, long y) {
return x < y ? x : y;
} class Edge {
public:
long v;
Edge *next;
Edge() {}
~Edge() {}
Edge(long V, Edge *ne) : v(V), next(ne) {}
} *g[MAXN]; void add(long x, long y) {
g[x] = new Edge(y, g[x]);
g[y] = new Edge(x, g[y]);
} void dp(long i, long pa) {
long sum;
Edge *e;
sum = 0;
for (e = g[i]; e; e = e->next)
if (e->v != pa) {
dp(e->v, i);
sum += f[e->v][0];
}
f[i][2] = inf;
f[i][0] = inf;
f[i][1] = sum;
if (g[i]->v == pa && !g[i]->next)
return;
static long x, y, z;
x = inf;
y = inf;
for (e = g[i]; e; e = e->next)
if (e->v != pa) {
f[i][2] = min(f[i][2], f[e->v][1] - f[e->v][0]);
f[i][2] = min(f[i][2], f[e->v][2] - f[e->v][0]);
f[i][0] = min(f[i][0], f[e->v][2] - f[e->v][0]);
z = min(f[e->v][1], f[e->v][2]) - f[e->v][0];
if (x > z) {
y = x;
x = z;
} else if (y > z)
y = z;
}
f[i][2] += sum;
if (y < inf)
f[i][0] = min(f[i][0], x + y);
f[i][0] += sum + 1;
} int main() {
static long n, i, u, v; #ifndef ONLINE_JUDGE
freopen("poj1848.in", "r", stdin);
freopen("poj1848.out", "w", stdout);
#endif scanf("%ld", &n);
for (i = 1; i < n; ++i) {
scanf("%ld%ld", &u, &v);
add(u, v);
}
dp(1, 0);
if (f[1][0] >= inf)
printf("-1");
else
printf("%ld", f[1][0]); fclose(stdin);
fclose(stdout);
return 0;
}
poj1848 Tree的更多相关文章
- POJ1848 Tree 【树形dp】
题目链接 POJ1848 题解 由题,一个环至少由三个点组成,一个点作为根时,可以单独成链,可以与其一个儿子成链,或者与其两个儿子成环,与其一个剩余链长度大于等于2的儿子成环. 那么我们设最小代价 \ ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- C语言的左位移能不能超过8位?
C语言的左位移能不能超过8位?比如b=a<<20; 这样可以不?如果可以,一个字节只有8个位,左移20位是不是连右边其它字节的12个位(20-8)也一起左移? 字符变量左移八次后,所有的位 ...
- 其实,前面倒腾那么多,只是为了想玩SPRING BOOT
嘿嘿,,曲线达到.. 看来看来很多国内的速成,都不爽. 官方教程最体贴~~~:) http://docs.spring.io/spring-boot/docs/current/reference/ht ...
- poj 1487 Single-Player Games
主要考察表达式的解析和高斯消元!!! #include<iostream> #include<stdio.h> #include<algorithm> #inclu ...
- android 内部存储相关知识点: getfilestreampath getDir 子文件夹
文件系统的API的命名方式和常规的不一样: 都是get命名,但是功能就是能创建文件夹... 这种方式的API 命名习惯和常规的不一样... createXXX ----方便查找 http://i ...
- D3DXCOLOR 和 D3DCOLOR 和 D3DCOLORVALUE
D3DCOLOR 是一个DWORD 型.第一个byte表示Alpha值,后面三个byte依次是r(红)g(绿)b(蓝)值.32位. 下面是一些关于D3DCOLOR 的宏: D3DCOLOR_ARGB( ...
- IE Web 开发支持将迁移到 StackOverflow
http://stackoverflow.com/questions/tagged/internet-explorer
- C#中string.Format()和ToString()格式化方法
C#数字格式化输出是我们在编程中经常需要处理的事情,那么这里向你介绍了一些C#数字格式化输出的例子,这样就会方便你来选择和比较,什么方式是比较适合自己项目的. int a = 12345678; C# ...
- Monitor vs WaitHandle
http://stackoverflow.com/questions/1355398/monitor-vs-waithandle-based-thread-sync A problem with Mo ...
- Complete The Pattern #2
Complete The Pattern #2 Description: Task: You have to write a function pattern which creates the fo ...
- The Material Sourcing Process Failed To Create Picking Suggestions in INVTOTRX (文档 ID 2003806.1)
In this Document Symptoms Cause Solution References Applies to: Oracle Inventory Management - Versio ...