Tree2cycle

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 3091    Accepted Submission(s): 719

Problem Description
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.

 
Input
The 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).
 
Output
For each test case, please output one integer representing minimal cost to transform the tree to a cycle.
 
Sample Input
1
4
1 2
2 3
2 4
 
Sample Output
3

Hint

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

 
Source
       求将一棵树拆分为一个环的最小的代价,可以拆解边和增加边,代价都是1.
     换一下思路,就是把这棵树拆掉det条边形成了det+1条链,再添加det+1条边,总代价就是det*2+1;我们只要求出最小的det就ok了。
对于节点u它可能有一个或多个儿子,如果只有一个儿子的话那就是一条链,不用再管,如果有son个儿子(son>1),我们需要拆掉son-1条边才能保证拆分成链,问题在于应该怎么拆掉这son-1条链才能最优。
            fa           
             |
    u
    /  |   \   
      v1  v2   v3
    对于上面这棵树,我们可以选择拆掉u-v2和u-v3或者是u-fa和u-v1,也就是删掉两个儿子或者一个儿子和一个父亲,会发现后者其实更优,因为避免了父亲形成多儿子的情况,就减少了删除的边数。知道这点就很容易了,递归返回儿子数目即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
int first[],tot;
struct Edge
{
int v,next;
}e[];
void add(int u,int v){
e[tot].v=v;
e[tot].next=first[u];
first[u]=tot++;
}
int aaa;
int dfs(int u,int fa)
{
int s=;
for(int i=first[u];~i;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
s+=dfs(v,u);
}
if(s>){
aaa+=s-;
if(u==) aaa--;
return ;
}
else {
return ;
}
}
int main()
{
int n,i,j,k,u,v;
int t;
cin>>t;
while(t--){aaa=;
memset(first,-,sizeof(first));
tot=;
scanf("%d",&n);
for(i=;i<n;++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,);
cout<<aaa+aaa+<<endl;
}
return ;
}

HDU-4714-贪心的更多相关文章

  1. HDU 4714 Tree2cycle:贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...

  2. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  3. HDU 4714 Tree2cycle DP 2013杭电热身赛 1009

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 Tree2cycle Time Limit: 15000/8000 MS (Java/Other ...

  4. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  5. hdu 4714 树+DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...

  6. hdu 1735(贪心) 统计字数

    戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...

  7. hdu 4974 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...

  8. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

  9. HDU 4714 Treecycle(树形dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给出一棵树,删除一条边和添加一条边的代价都是1,现在要把这棵树变成环,求需要花的最小代价. 思路: ...

  10. HDU 2307 贪心之活动安排问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)  ...

随机推荐

  1. (转)CentOs7.3 搭建 RabbitMQ 3.6 Cluster 集群服务与使用

    RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.ActionScript.XMPP.STO ...

  2. 最值得阅读学习的 10 个 C 语言开源项目代码

    1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连 ...

  3. win7_32下编译FFmpeg

    运行环境:  VC2010软件:                 [附:本文所用软件安装包:http://download.csdn.NET/detail/sinat_36666600/9705438 ...

  4. nginx + resin配合使用问题

    在A机器上用resin部署了一台接口服务器,用了一段时间后需要扩容. 从A上拷贝所有配置和数据至B服务器,修改了resin的IP配置. <server id="" addre ...

  5. web.xml servlet、servlet-mapping配置

    Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...

  6. 配置linux使用mail发送邮件到163邮箱

    1.进行配置 yum install -y mailx /etc/mail.rc添加对163的授权: ##########config 163 mail############set from=jso ...

  7. 20145216史婧瑶《Java程序设计》第5周学习总结

    20145216 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生 ...

  8. 20145324 《Java程序设计》第7周学习总结

    20145324 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章 1.静态sleep()用于流程暂停指定时间,单位是毫秒 2.一个Thread被标记为Daemon线程,在所 ...

  9. 20144303 《Java程序设计》第六周学习总结

    20144303 <Java程序设计>第六周学习总结 教材学习内容总结 第十章输入和输出 Java是以串流(Stream)的方式来处理输入与输出. 串流是一种抽象观念,从键盘输入资料,将处 ...

  10. 解读:MR多路径输入

    对于在一个MR-Job中使用多路径作为输入文件,一般有三种方法: 1).多次调用,加载不同路径: import org.apache.hadoop.mapreduce.lib.input.FileIn ...