BZOJ 1907 树DP 解题报告
2017-09-22 14:00
309 查看
1907: 树的路径覆盖
Description
![](https://img-blog.csdn.net/20170922135610090?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb25lcG9pbnRv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Input
![](https://img-blog.csdn.net/20170922135626863?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb25lcG9pbnRv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Output
![](https://img-blog.csdn.net/20170922135635308?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb25lcG9pbnRv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Sample Input
1
7
1 2
2 3
2 4
4 6
5 6
6 7
Sample Output
3
【解题报告】
首先将所有的链都考虑成以链上所有点的LCA为转折点的V字形
那么点有两种:转折点和非转折点
因此我们选择两种状态进行转移:还会和父亲组成链的状态和成为转折点的状态
代码如下:
Description
Input
Output
Sample Input
1
7
1 2
2 3
2 4
4 6
5 6
6 7
Sample Output
3
【解题报告】
首先将所有的链都考虑成以链上所有点的LCA为转折点的V字形
那么点有两种:转折点和非转折点
因此我们选择两种状态进行转移:还会和父亲组成链的状态和成为转折点的状态
代码如下:
/************************************************************** Problem: 1907 User: onepointo Language: C++ Result: Accepted Time:204 ms Memory:1500 kb ****************************************************************/ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 10010 int t,n,dp [2]; int cnt,head ; struct Edge{int to,nxt;}e[N<<1]; void adde(int u,int v) { e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt; e[++cnt].to=u;e[cnt].nxt=head[v];head[v]=cnt; } void dfs(int u,int fa) { int tmp=0;dp[u][0]=dp[u][1]=1; for(int i=head[u];~i;i=e[i].nxt) { int v=e[i].to; if(v==fa) continue; dfs(v,u); dp[u][0]=min(dp[u][0]+dp[v][0],dp[u][1]+dp[v][1]-1); dp[u][1]=min(dp[u][1]+dp[v][0],tmp+dp[v][1]); tmp+=dp[v][0]; } } int main() { for(scanf("%d",&t);t;--t) { cnt=-1; memset(head,-1,sizeof(head)); scanf("%d",&n); for(int i=1;i<n;++i) { int u,v;scanf("%d%d",&u,&v); adde(u,v); } dfs(1,1); printf("%d\n",dp[1][0]); } return 0; }
相关文章推荐
- [BZOJ1026][SCOI2009]windy数 解题报告|数位dp
- BZOJ 1068 [SCOI 2007] 区间DP 解题报告
- [BZOJ1076][SCOI2008]奖励关解题报告|状压DP
- BZOJ5072[Lydsy十月月赛] 小A的树 解题报告【树上背包/树形DP】
- BZOJ 2201 期望DP 解题报告
- BZOJ 2131 数据结构优化DP 解题报告
- BZOJ 1566 DP 解题报告
- BZOJ 3727 PA2014 树DP 解题报告
- BZOJ 3107 [cqoi 2013] DP 解题报告
- BZOJ 1003 DP+最短路 解题报告
- BZOJ 4720 [Noip 2016] 期望DP+最短路 解题报告
- BZOJ 4726 [POI 2017] 树DP 解题报告
- BZOJ 1419 DP 解题报告
- BZOJ3790 神奇项链 解题报告【字符串】【Manacher】【树状数组】【数据结构优化DP】
- BZOJ 1084 [SCOI 2005] DP 解题报告
- BZOJ 3193 [JLOI 2013] 计数DP 解题报告
- [BZOJ1087][SCOI2005]互不侵犯King解题报告|状压DP
- BZOJ 1833 数位DP 解题报告
- BZOJ 5072 [Lydsy 十月月赛] 树DP 解题报告
- BZOJ 1040 [ZJOI 2008] 树DP 解题报告