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
#include <stdio.h>
#include <string.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define Maxn 1010000 struct Edge
{
int v;
struct Edge *next;
}*head[Maxn<<],edge[Maxn<<]; int n,cnt,ans;
void add(int a,int b)
{
edge[++cnt].v=b,edge[cnt].next=head[a];
head[a]=&edge[cnt];
} int dfs(int cur,int pa) //返回分支个数
{
int res=;
struct Edge * p=head[cur]; while(p)
{
if(p->v!=pa) res+=dfs(p->v,cur);
p=p->next;
}
if(res>=) //超过两个分支,将其它分支链过来
{
if(cur==) ans+=res-; //树根不用链到其它地方
else ans+=res-; //选两个,其它的边都要断开和重新链接一次
return ; //断开了
}
else return ; //作为一个单支
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cnt=;
memset(head,NULL,sizeof(head));
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
ans=;
dfs(,);
printf("%d\n",ans*+);
}
return ;
}

Tree2cycle的更多相关文章

  1. HDU 4714 Tree2cycle

    Tree2cycle dfs 不是根节点:如果边数大于等于2,则删除与父节点的边.并且是一条环,那么每个点的度数是2,则还要删除num(每个节点儿子数)-2,只留两个儿子.当然删除边的儿子也要连到环上 ...

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

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

  3. hdu 4717 Tree2cycle(树形DP)

    Tree2cycle Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Tot ...

  4. HDU 4714 Tree2cycle (树形DP)

    Tree2cycle Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Tot ...

  5. hdu 4714 Tree2cycle 树形经典问题

    发现今天没怎么做题,于是随便写了今天杭电热身赛的一题. 题目:给出一棵树,删边和添边的费用都是1,问如何删掉一些树边添加一些树边,使得树变成一个环. 分析:统计树的分支数.大概有两种做法: 1.直接d ...

  6. hdu4714 Tree2cycle 把树剪成链

    题目是问把一棵树通过剪边.加边形成一个环的最小代价. 分成两步,先把树剪成一些链,再把链连接成一个环. 设一棵有n个节点的树,剪掉X条边后,形成L条链. 那么代价为X+L. n-1-X=edgeNum ...

  7. hdu 4714 Tree2cycle dp

    用树形dp做的,dp[t][i]表示t及其孩子入度都已经小于等于2并且t这个节点的入度等于i的最优解. 那么转移什么的自己想想就能明白了. 关键在于这个题目会暴栈,所以我用了一次bfs搜索出节点的顺序 ...

  8. HDU 4714 Tree2cycle (树形DP)

    题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...

  9. HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)

    Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...

随机推荐

  1. WisDom.Net 框架设计(七) 验证框架

    WisDom.Net-验证框架 1.分类 这里我们将数据验证分为以下几种 数据类型校验      主要用于确保数据类型输入的正确  比如年龄一项输入 A岁 ,显然不合法 域检查             ...

  2. SqlSugar常用增删改操作

    一.添加数据 特别说明: 1.特别说明:对于自增长列的表插入数据后,当前自增长列的字段,仍旧为0,但可以通过Insert方法的返回值来获取 SqlSugarClient db = SugarConte ...

  3. 关于AVD不能导入文件的解决方案

    安卓虚拟机导入文件时报以下异常: [2013-01-23 16:09:18 - ddms] transfer error: Read-only file system [2013-01-23 16:0 ...

  4. WindowsServer2003SP2EnterpriseEdition在Virtual上的安装

    下载镜像(迅雷): http://192.168.0.101/WindowsServer2003SP2EnterpriseEdition.iso?fid=fWljwnwNgumTtRIy- *BY*a ...

  5. SQL学习:主键,外键,主键表,外键表,数据库的表与表之间的关系;

    在数据库的学习中,对于一个表的主键和外键的认识是非常重要的. 主键:在一个表中,能唯一的表示一个事物(或者一条记录)的字段,我们称之为主键 注意: 主键的设置可以不只是用一个字段,也可以用若干个字段的 ...

  6. Oracle 11g服务

    成功安装Oracle 11g后,共有7个服务, 这七个服务的含义分别为: 1. Oracle ORCL VSS Writer Service: Oracle卷映射拷贝写入服务,VSS(Volume S ...

  7. 判断用户输入是否为int整型方法之一!

    ; string vv = this.textBox2.Text; if (int.TryParse(vv, out age)) { age = int.Parse(this.textBox2.Tex ...

  8. Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)

    题目描述 We know thatif a phone number A is another phone number B's prefix, B is not able to becalled. ...

  9. php文件缓存

    1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...

  10. Django练习项目之搭建博客

    背景:自从今年回家过年后,来到公司给我转了试用,我的学习效率感觉不如从前,而且刚步入社会我总是想要怎么想明白想清楚一些事,这通常会花掉,消耗我大量的精力,因为我想把我的生活管理规划好了,而在it技术学 ...