Tree2cycle
A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of cost respectively. The nodes are labeled from 1 to N. Your job is to transform the tree to a cycle(without superfluous edges) using minimal cost.

A cycle of n nodes is defined as follows: (1)a graph with n nodes and n edges (2)the degree of every node is 2 (3) each node can reach every other node with these N edges.

InputThe first line contains the number of test cases T( T<=10 ). Following lines are the scenarios of each test case. 
In the first line of each test case, there is a single integer N( 3<=N<=1000000 ) - the number of nodes in the tree. The following N-1 lines describe the N-1 edges of the tree. Each line has a pair of integer U, V ( 1<=U,V<=N ), describing a bidirectional edge (U, V). 
OutputFor each test case, please output one integer representing minimal cost to transform the tree to a cycle. 
Sample Input

  1. 1
  2. 4
  3. 1 2
  4. 2 3
  5. 2 4

Sample Output

  1. 3

Hint

  1. In the sample above, you can disconnect (2,4) and then connect (1, 4) and
  2. (3, 4), and the total cost is 3.
  3.  
  4. 题解:
      

  

  1. 题解:
      对于子节点,如果有两个子节点,那么就要分开,分开子孙和祖先是一样的。
     对于根节点,取两个子孙连起来,将其它节点分开。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<cstdlib>
  7. #define N 1000007
  8. using namespace std;
  9.  
  10. int tot,head[N],Next[N*],info[N*];
  11. int dp[N];
  12.  
  13. void init()
  14. {
  15. memset(head,-,sizeof head);
  16. memset(dp,,sizeof dp);
  17. tot=;
  18. }
  19. void add(int fr,int to)
  20. {
  21. Next[tot]=head[fr];
  22. info[tot]=to;
  23. head[fr]=tot++;
  24. }
  25. bool dfs(int u,int f)
  26. {
  27. int ans=;
  28. for(int i=head[u];i!=-;i=Next[i])
  29. {
  30. int v=info[i];
  31. if(v==f) continue;
  32. if(dfs(v,u)) dp[u]+=dp[v]+;
  33. else
  34. {
  35. dp[u]+=dp[v];
  36. ans++;
  37. }
  38. }
  39. if(ans==||ans==) return false;
  40. if(ans>) dp[u]+=ans-;
  41. return true;
  42. }
  43. int main()
  44. {
  45. int cas;
  46. scanf("%d",&cas);
  47. while(cas--)
  48. {
  49. int n;
  50. scanf("%d",&n);
  51. init();
  52. for(int i=,x,y=;i<n;i++)
  53. {
  54. scanf("%d%d",&x,&y);
  55. add(x,y),add(y,x);
  56. }
  57. dfs(,-);
  58. printf("%d\n",dp[]*+);
  59. }
  60. }
  1.  

hdu4714树形DP+贪心(乱搞)的更多相关文章

  1. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  2. CF809E Surprise me!(莫比乌斯反演+Dp(乱搞?))

    题目大意: 给你一棵树,树上的点编号为\(1-n\).选两个点\(i.j\),能得到的得分是\(\phi(a_i*a_j)*dis(i,j)\),其中\(dis(i,j)\)表示\(a\)到\(b\) ...

  3. [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)

    传送门 1.树形DP #include <cstdio> #include <cstring> #include <iostream> #define N 1000 ...

  4. POJ 3671 DP or 乱搞

    思路: 1.DP f[i][j]:前i个数 最后一个数是j的最小花费 f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j));1<=k<=j 这种做法比较有普遍性 ...

  5. 2016 10 27 考试 dp 向量 乱搞

    目录 20161027考试 T1: T2: T3: 20161027考试 考试时间 7:50 AM to 11:15 AM 题目 考试包 据说这是一套比较正常的考卷,,,嗯,,或许吧, 而且,,整个小 ...

  6. bzoj 3829: [Poi2014]FarmCraft 树形dp+贪心

    题意: $mhy$ 住在一棵有 $n$ 个点的树的 $1$ 号结点上,每个结点上都有一个妹子. $mhy$ 从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装 $zhx$ 牌杀毒 ...

  7. 树形DP+贪心(乱搞)(HDU4714)

    题意:给出一个树形图,要求把该树形成一个环最少的步骤(断开一条边和形成一条边都需一步) 分析:很明显,要想把树形成一个环,就要先把其分裂成m条子链之后把子链形成环需要的步骤是2*m+1,所以只需要m最 ...

  8. [POI2014]FAR-FarmCraft 树形DP + 贪心思想

    (感觉洛谷上题面那一小段中文根本看不懂啊,好多条件都没讲,直接就是安装也要一个时间啊,,,明明不止啊!还好有百度翻译......) 题意:一棵树,一开始在1号节点(root),边权都为1,每个点有点权 ...

  9. [HNOI2003]消防局的设立 树形dp // 贪心

    https://www.luogu.org/problemnew/show/P2279 一开始就想到了贪心的方法,不过一直觉得不能证明. 贪心的考虑是在深度从深到浅遍历每个结点的过程中,对于每个没有覆 ...

随机推荐

  1. poj3252Round Numbers

    链接 也算是组合 以前按组合做过一次 忘记怎么做的了 这次按dp写的 dp[i][j][g][k] 表示第i位为k(0|1)而且有j个1,g个0的情况数 貌似写的麻烦了...这一类的题,进行逐位计算就 ...

  2. P2956 [USACO09OCT]机器人犁田The Robot Plow

    题目描述 Farmer John has purchased a new robotic plow in order to relieve him from the drudgery of plowi ...

  3. 访问github.com太慢的解决方法

    修改 c:\windows\system32\drivers\etc\host文件添加 192.30.255.112 github.com 151.101.72.249 github.global.s ...

  4. EmitMapper系列之二:EmitMapper的使用小结

    EmitMapper的入门 EmitMapper引用 EmitMapper案例 最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了Em ...

  5. objectbox基础

    objectbox基础 参考链接 官网地址 http://objectbox.io github地址 https://github.com/objectbox/objectbox-java https ...

  6. ubuntu下安装php-curl扩展

    查找包 apt-cache是linux下的一个apt软件包管理工具,它可查询apt的二进制软件包缓存文件.APT包管理的大多数信息查询功能都可以由apt-cache命令实现,通过apt-cache命令 ...

  7. Phalcon初认识

    Phalcon以c扩展交付的全堆栈php开发框架 基本功能 低开销:低内存消耗和CPU相比传统的框架 MVC和HMVC:模块.组件.模型.视图和控制器 依赖注入:依赖注入和位置的服务和它的本身他们的容 ...

  8. Hive工具类

    Hive2.x的工具类,对常用方法进行了封装,其中设置了kerberos认证. package com.ideal.template.openbigdata.util; import java.sql ...

  9. 测试常用的linux命令

    一.系统 1.halt:         关机   poweroff: 关机 2.reboot:     重启 二.处理目录和文件的命令 1.ll:     显示文件详细信息 ls:    显示文件目 ...

  10. MySQL-03 SQL语句设计

    学习要点 SQL语句分类 DML语句 DML 查询语句 SQL语句分类 数据操纵语言(DML):用来操纵数据库中数据的命令.包括:SELECT.INSERT.UPDATE.DELETE. 数据定义语言 ...