一眼题...

  f[i][0]表示在i连接一个子树的最小值,f[i][1]表示在i连接两个子树的最小值,随便转移...

  样例挺强的1A了美滋滋...

UPD:学习了2314的写法之后短了好多T T

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=, inf=1e9;
struct poi{int too, pre;}e[maxn];
int n, T, tot, x, y;
int f[maxn][], last[maxn];
void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
inline int min(int a, int b){return a<b?a:b;}
void dfs(int x, int fa)
{
f[x][]=maxn; f[x][]=; int sum=;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs(too, x);
f[x][]=min(f[x][]+min(f[too][], f[too][]), f[x][]+f[too][]-);
f[x][]=min(f[x][]+min(f[too][], f[too][]), sum+f[too][]);
sum+=min(f[too][], f[too][]);
}
}
int main()
{
read(T);
while(T--)
{
read(n); memset(last, , (n+)<<); tot=;
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs(, ); printf("%d\n", min(f[][], f[][]));
}
}

旧代码:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=, inf=1e9;
struct poi{int too, pre;}e[maxn];
int n, T, tot, x, y;
int f[maxn][], last[maxn];
void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
inline int min(int a, int b){return a<b?a:b;}
void dfs(int x, int fa)
{
int mn1=inf, mn2=inf, mni1=, mni2=, tmp=; f[x][]=; f[x][]=-;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs(too, x);
f[x][]+=min(f[too][], f[too][]);
if(f[too][]==min(f[too][], f[too][])) tmp=;
if(f[too][]-min(f[too][], f[too][])<mn1) mn1=f[too][]-min(f[too][], f[too][]), mni1=too;
else if(f[too][]-min(f[too][], f[too][])<mn2) mn2=f[too][]-min(f[too][], f[too][]), mni2=too;
}
f[x][]+=tmp;
if(!(f[x][]-tmp)) {f[x][]=; f[x][]=inf; return;}
if(mn2==inf) {f[x][]=inf; return;}
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
if(too==mni1 || too==mni2) f[x][]+=f[too][];
else f[x][]+=min(f[too][], f[too][]);
}
}
int main()
{
read(T);
while(T--)
{
read(n); memset(last, , (n+)<<); tot=;
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs(, ); printf("%d\n", min(f[][], f[][]));
}
}

bzoj1907: 树的路径覆盖(树形DP)的更多相关文章

  1. 【bzoj1907】树的路径覆盖 树形dp

    题目描述 输入 输出 样例输入 1 7 1 2 2 3 2 4 4 6 5 6 6 7 样例输出 3 题解 树形dp 设f[x]表示以x为根的子树完成路径覆盖,且x为某条路径的一端(可以向上延伸)的最 ...

  2. BZOJ-1907 树的路径覆盖 贪心

    题意:给一个n个点的树,求树的最小路径覆盖.(这个最小路径覆盖不能有重点) 解法:往图论方向想很久,想得太复杂了,其实直接贪心.这个大佬题解写得很好: https://blog.csdn.net/bl ...

  3. BZOJ1907 树的路径覆盖

    ydc题解上写着贪心,后来又说是树形dp...可惜看不懂(顺便骗三连) 其实就是每个叶子开始拉一条链,从下面一路走上来,遇到能把两条链合起来的就合起来就好了. /******************* ...

  4. bzoj 1907: 树的路径覆盖【贪心+树形dp】

    我是在在做网络流最小路径覆盖的时候找到这道题的 然后发现是个贪心+树形dp \( f[i] \)表示在\( i \)为根的子树中最少有几条链,\( v[i] \) 表示在\( i \)为根的子树中\( ...

  5. [BZOJ 1907] 树的路径覆盖 【树形DP】

    题目链接:BZOJ - 1907 题目分析 使用树形 DP,f[x][0] 表示以 x 为根的子树不能与 x 的父亲连接的最小路径数(即 x 是一个折线的拐点). f[x][1] 表示以 x 为根的子 ...

  6. 『快乐链覆盖 树形dp』

    快乐链覆盖 Description 给定一棵 n 个点的树,你需要找至多 k 条互不相交的路径,使得它们的长度之和最大 定义两条路径是相交的:当且仅当存在至少一个点,使得这个点在两条路径中都出现 定义 ...

  7. BZOJ5123 线段树的匹配(树形dp)

    线段树的任意一棵子树都相当于节点数与该子树相同的线段树.于是假装在树形dp即可,记忆化搜索实现,有效状态数是logn级别的. #include<iostream> #include< ...

  8. 1113: [视频]树形动态规划(TreeDP)8:树(tree)(树形dp状态设计总结)

    根据最近做的几道树形dp题总结一下规律.(从这篇往前到洛谷 P1352 ) 这几道题都是在一颗树上,然后要让整棵树的节点或边 满足一种状态.然后点可以影响到相邻点的这种状态 然后求最小次数 那么要从两 ...

  9. [CEOI2007]树的匹配Treasury(树形DP+高精)

    题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...

随机推荐

  1. vue-cli的安装使用

    通过 npm install vue 安装vue 后: 1.打开cmd 输入 npm install --global vue-cli 全局安装vue-cli脚手架 输入 vue -V 返回vue-c ...

  2. 深圳第XX天

    今天早晨,面了一家小公司.先说结果吧,面过了.但是,总感觉太假了.面试中很多问题都没有回答上来.然后老板看了一下简历,问:期薪资多少?我想了想,说7000.啊,要不留下来看看?我答应了.不到十分钟,就 ...

  3. vue2.0做移动端开发用到的相关插件和经验总结

    最近一直在做移动端微信公众号项目的开发,也是我首次用vue来开发移动端项目,前期积累的移动端开发经验较少.经过这个项目的锻炼,加深了对vue相关知识点的理解和运用,同时,在项目中所涉及到的微信api( ...

  4. (转)ASP.NET Core 性能对比评测(ASP.NET,Python,Java,NodeJS)

    转:https://www.cnblogs.com/savorboard/archive/2016/10/17/dotnet-benchmarks.html 前言 性能是我们日常生活中经常接触到的一个 ...

  5. 【python 2.7】python读取json数据存入MySQL

    同上一篇,只是适配 CentOS+ python 2.7 #python 2.7 # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import json im ...

  6. Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

    一.错误信息 Entity Framework 6.0数据迁移:Add-Migration XXXX 命令发生错误 System.Reflection.TargetInvocationExceptio ...

  7. Masha and Bears(翻译+思维)

    Description A family consisting of father bear, mother bear and son bear owns three cars. Father bea ...

  8. 六周psp

    本周psp 本周进度条 代码累积折线图 博文字数累积折线图 饼状图

  9. 《JavaScript》JS中的跨域问题

    参考博客:https://www.cnblogs.com/yongshaoye/p/7423881.html

  10. NSURLErrorDomain错误代码

    kCFURLErrorUnknown = -998, kCFURLErrorCancelled = -999, kCFURLErrorBadURL = -1000, kCFURLErrorTimedO ...