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. HierarchicalDataBoundControl 错误

    出现以上错误原因是控件Datasources绑定出错,可能原因是没有区分树形结构的控件如Treeview的绑定与二维数据如datagridview绑定之间的区别.

  2. [页面辅助] 最新的 PageValidate 类 (转载)

    代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Text. ...

  3. mvc5 + ef6 + autofac搭建项目(四).1视屏上传生成截图

    即上一篇中上传涉及到的 一个视频生成截图的问题,这个很简单,这是上一篇中的代码片段 #region 视频上传,生成默认展示图片(自动剪切) try { string fileSavePath = Da ...

  4. C++Primer学习笔记(二、基础)

    1.两种初始化方式,直接初始化语法更灵活,且效率更高. ); // 直接初始化 direct-initialization ; // 赋值初始化 copy-initialization 2.const ...

  5. 如何写类库方法、属性等的注释,才能在其他地方调用dll文件时,在代码里出现智能提示?

    我的本意是想整理下以往写过的代码库,给自己的代码增加复用性.一段时间后,可能自己对写过的代码是什么含义会忘掉,或者别人看自己的代码, 增加可懂性的考虑,决定要添加注释.(好像语句不通:)可是发现,在其 ...

  6. oracle查看用户信息

    1.查看所有用户:select * from dba_users; select * from all_users; select * from user_users;2.查看用户或角色系统权限(直接 ...

  7. clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y ,offsetTop,offsetLeft 详解

    clientX/Y: clientX/Y获取到的是触发点相对浏览器可视区域左上角距离,不随页面滚动而改变 兼容性:所有浏览器均支持 pageX/Y: pageX/Y获取到的是触发点相对文档区域左上角距 ...

  8. WPF自定义窗口(Windows Server 2012 Style)

    先上图 新建CustomControl,名:HeaderedWindow Themes\Generic.aml文件夹下加入 笔刷,转换器 <SolidColorBrush x:Key=" ...

  9. go和swift

    你生命中的有些东西终究会失去,比如我住了6年的陈寨,这个聚集了郑州十几万IT民工的地方,说拆就拆了.再比如我玩了3年的坦克英雄,这个带给我太多快乐的游戏,说停就停了. 编程对我而言是种爱好,我上学6年 ...

  10. The Lost Art of C Structure Packing

    对齐要求 首先需要了解的是,对于现代处理器,C编译器在内存中放置基本C数据类型的方式受到约束,以令内存的访问速度更快. 在x86或ARM处理器中,基本C数据类型通常并不存储于内存中的随机字节地址.实际 ...