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. 模块讲解----json模块(跨平台的序列化与反序列化)

    一.json的特点 1.只能处理简单的可序列化的对象:(字典,列表,元祖) 2.json支持不同语言之间的数据交互:(python  -  go,python - java)   二.使用场景 1.玩 ...

  2. 工作笔记——限定input上传文件对话框中能选取的文件的格式

    原文:http://www.dengzhr.com/frontend/1059 input[file]标签的accept属性可用于指定上传文件的 MIME类型 . 例如,想要实现默认上传图片文件的代码 ...

  3. android 弹出带按钮的对话框

    package com.example.helloworld; import android.os.Bundle;import android.app.Activity;import android. ...

  4. jQuery动态效果学习笔记

    资料来源 W3Cschool 1.元素的显示与隐藏 1.1显示元素show() 语法 $(selector).show(speed,callback); 显示已经设置隐藏的元素 1.2隐藏元素hide ...

  5. Log4net 自定义字段 写入Oracle 使用ODP.NET Managed驱动

    一.环境说明: 开发工具:vs2010   ,数据库:oracle 11g ,版本:log4net的目前最新版本1.2.13.0    :  Oracle.ManagedDataAccess.dll ...

  6. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) D - Dynamic Problem Scoring

    地址:http://codeforces.com/contest/807/problem/D 题目: D. Dynamic Problem Scoring time limit per test 2 ...

  7. Ubuntu16.04安装MySQLdb

    buntu 系统下进行的操作 首先安装了pip工具 1 sudo apt-get install python-pip 然后使用 1 sudo pip install mysql-python   安 ...

  8. 如何交叉编译Python到ARM-Linux平台(转)

    源: 如何交叉编译Python到ARM-Linux平台

  9. 20145314郑凯杰《信息安全系统设计基础》第9周学习总结 PART B

    20145314郑凯杰<信息安全系统设计基础>第9周学习总结 PART B 明确教材学习目标 注意每个系统调用的参数.返回值,会查帮助文档 阅读教材,完成课后练习(书中有参考答案),考核: ...

  10. LabVIEW之Vision基础 (一)之软件

    一.软件准备 NI LabVIEW软件视觉开发必备软件 1.开发平台:LabVIEW 2015Chinese 32位中文版 链接:http://pan.baidu.com/s/1eRGmFVc 2.N ...