Tree2cycle
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.
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).
4
1 2
2 3
2 4
In the sample above, you can disconnect (2,4) and then connect (1, 4) and
(3, 4), and the total cost is 3.
#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的更多相关文章
- HDU 4714 Tree2cycle
Tree2cycle dfs 不是根节点:如果边数大于等于2,则删除与父节点的边.并且是一条环,那么每个点的度数是2,则还要删除num(每个节点儿子数)-2,只留两个儿子.当然删除边的儿子也要连到环上 ...
- HDU 4714 Tree2cycle DP 2013杭电热身赛 1009
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 Tree2cycle Time Limit: 15000/8000 MS (Java/Other ...
- hdu 4717 Tree2cycle(树形DP)
Tree2cycle Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Tot ...
- HDU 4714 Tree2cycle (树形DP)
Tree2cycle Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Tot ...
- hdu 4714 Tree2cycle 树形经典问题
发现今天没怎么做题,于是随便写了今天杭电热身赛的一题. 题目:给出一棵树,删边和添边的费用都是1,问如何删掉一些树边添加一些树边,使得树变成一个环. 分析:统计树的分支数.大概有两种做法: 1.直接d ...
- hdu4714 Tree2cycle 把树剪成链
题目是问把一棵树通过剪边.加边形成一个环的最小代价. 分成两步,先把树剪成一些链,再把链连接成一个环. 设一棵有n个节点的树,剪掉X条边后,形成L条链. 那么代价为X+L. n-1-X=edgeNum ...
- hdu 4714 Tree2cycle dp
用树形dp做的,dp[t][i]表示t及其孩子入度都已经小于等于2并且t这个节点的入度等于i的最优解. 那么转移什么的自己想想就能明白了. 关键在于这个题目会暴栈,所以我用了一次bfs搜索出节点的顺序 ...
- HDU 4714 Tree2cycle (树形DP)
题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...
- 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 ...
随机推荐
- .net 关于数据库的链接
web.config的配置 <appSettings> <add key="Configpath" value="~/XmlConfig/webset. ...
- 学习java随笔第七篇:java的类与对象
类 同一个包(同一个目录),类的创建与调用 class Man{ String name; void GetMyName() { System.out.println(name); } } publi ...
- C#调用ActiveX控件
背景:最近项目中需要用到ActiveX控件,项目是在.Net平台下开发的.因此就直接在项目中添加了对ActiveX控件的引用,添加引用成功.在代码中实例化类的实例也没有问题,但在调用其方法或属性时总是 ...
- oracle set命令
SQL>set colsep' '; //-域输出分隔符SQL>set echo off; //显示start启动的脚本中的每个sql命令,缺省为onSQL> set ...
- HttpServletRequest 获取URL的方法及区别
HttpServletRequest 获取请求的URL的方法有: 1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它 ...
- [LeetCode OJ] Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 中文版Chrome浏览器不支持12px以下字体的解决方案
中文版Chrome浏览器不支持12px以下字体的解决方案 Chrome 27之前的中文版桌面浏览器会默认设定页面的最小字号是12px,英文版则没有限制,主要是因为chrome认为汉字小于12px就会增 ...
- winform程序开机自动启动代码
几天前头儿要我实现程序能开机自动启动,搞好了,整理起来写下来. private void checkBox1_CheckedChanged(object sender, EventArgs e) { ...
- windows下使用cxfreeze打包python3程序
1:下载适合版本的cxfreeze http://sourceforge.net/projects/cx-freeze/files/4.3.2/ 2:安装,注意python版本是否正确 3:安装完成后 ...
- C#小数点位数处理方法
//方法一: //保留小数位数,并能四舍五入 DecimalFormat de = new DecimalFormat("0.00"); System.out.println(de ...