题目大意:

给出一棵树,现在要往这棵树上加边,使得所有的点都在环中,且每个点只能属于一个环

题解:

考虑DP:

\(dp[i][0]\)表示使\(i\)这颗子树的每个点都在环内需要加的最少边数。

\(dp[i][1]\)表示使\(i\)这颗子树除了根\(i\)之外的其余点都在环内要加的最少边数。

\(dp[i][2]\)表示使\(i\)这颗子树除了根\(i\)所在的一条链外的其余点都在环内要加的最少边数

考虑转移:

\[dp[u][1]=\sum dp[v][0]
\]

\[dp[u][0]=\min_x\ (\ (\sum_v dp[v][0])\ -dp[x][0]+dp[x][2]+1)
\]

\[dp[u][2]=\min_x\ (\ (\sum_v dp[v][0])\ -dp[x][0]\ +min(dp[x][1],dp[x][2]))
\]

\[dp[u][0]=\min_{x,y}\ (\ (\sum_v dp[v][0])\ -dp[x][0]-dp[y][0]+min(dp[x][1],dp[x][0])+min(dp[y][0],dp[y][1])+1)
\]

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define int long long using namespace std; namespace Tzh{ const int maxn=110,inf=0x3f3f3f3f;
int st[maxn],dp[maxn][3],n,cnt,head[maxn]; struct ed{
int next,to;
}e[maxn<<1]; void add(int u,int v){
e[++cnt].next=head[u],e[cnt].to=v,head[u]=cnt;
e[++cnt].next=head[v],e[cnt].to=u,head[v]=cnt;
} void dfs(int now,int fa){
int sum=0;
vector<int> st;
for(int i=head[now];i;i=e[i].next){
int tt=e[i].to; if(tt==fa) continue;
st.push_back(tt); dfs(tt,now); sum=sum+dp[tt][0];
}
dp[now][1]=sum; dp[now][0]=dp[now][2]=inf;
if(!st.size()) return ;
for(int i=0;i<st.size();i++){
dp[now][0]=min(dp[now][0],sum-dp[st[i]][0]+dp[st[i]][2]+1);
dp[now][2]=min(dp[now][2],sum-dp[st[i]][0]+min(dp[st[i]][1],dp[st[i]][2]));
}
for(int i=0;i<st.size();i++)
for(int j=i+1;j<st.size();j++)
dp[now][0]=min(dp[now][0],sum-dp[st[i]][0]-dp[st[j]][0]+1
+min(dp[st[i]][1],dp[st[i]][2])+min(dp[st[j]][1],dp[st[j]][2]));
} void work(){
scanf("%lld",&n); int u,v;
for(int i=1;i<n;i++) scanf("%lld%lld",&u,&v),add(u,v);
dfs(1,0);
printf("%lld",dp[1][0]==inf?-1:dp[1][0]);
return ;
}
} signed main(){
Tzh::work();
return 0;
}

POJ 1848 Tree 树形DP的更多相关文章

  1. POJ 1741 Tree 树形DP(分治)

    链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...

  2. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  3. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  4. POJ 2486 Apple Tree(树形DP)

    题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...

  5. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

  6. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  7. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  8. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  9. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

随机推荐

  1. Spring Cloud 微服务开发系列整理

    Spring Boot 系列精选 Spring Boot 自定义 starter Spring Boot 整合 mybatis-plus Spring Boot 整合 spring cache Spr ...

  2. Linux用户组权限简单解析

    Linux的用户是以组为单位,每个用户都属于某一个组,而用户组的权限,是指某个用户对某个文件(文件夹)的操作权限,这里涉及用户组的概念. 其中root用户拥有全Linux系统中最高的权限,比任何其他用 ...

  3. windows如何安装memcached

    官网上并未提供 Memcached 的 Windows 平台安装包,我们可以使用以下链接来下载,你需要根据自己的系统平台及需要的版本号点击对应的链接下载即可: 32位系统 1.2.5版本:http:/ ...

  4. PHP遍历文件夹下所有文件

    不论是面试还是正常工作需要都会用到遍历文件夹下的所有文件,今天就记录一下笔记.废话不多说直接上代码: <?php /** * 遍历当前文件夹展示所有的文件和目录 */ function dirL ...

  5. 前端开发中使用mac自带apache服务

    场景 前端开发中,总是会有这样的需求,就是快速的写一个脚本,或者一个简单的demo页面.这时,我们需要马上可以启动一个web服务,来支持开发. 我们可以安装一个全局的cli工具,通过node服务来满足 ...

  6. iframe中的a标签电话链接不能正常打开

    背景 经测试,android手机中没有这个问题, iphone手机中的Safari浏览器会出现这个问题. 例如: <a href = "tel://1-408-555-5555&quo ...

  7. CSS重要知识概述——Java Web从入门到精通第2章

    一.CSS简单规则 CSS样式表包含3部分内容:选择符.属性和属性值 其中选择符包括基本的3种选择器: 1.标记选择器,如<a></a>标签等: 2.类别选择器,用class属 ...

  8. 手把手教新手小白在window把自己的项目上传到github

    作为一个开发者,写博客,上传项目到github好像是不可不会的技能,很多有经验的老司机都会这么建议你.本宝宝第一次要把项目传到github的时候,确实有点蒙蔽,什么鬼,传个东西有必要这么难吗? git ...

  9. Spark 基本函数学习笔记一

      Spark 基本函数学习笔记一¶ spark的函数主要分两类,Transformations和Actions. Transformations为一些数据转换类函数,actions为一些行动类函数: ...

  10. vsftpd.configro

    mmp卸载了vsftpd后 配置文件没了 安装也没有 留个做备份 嘿嘿 原始的: # Please see vsftpd.conf. for all compiled in defaults. # # ...