链接

树上的一些操作还是不是太好想 直接dfs下去 不是最优的

一个节点最多保留两个度 如果它有两个以上的子节点 那么就与父节点断开 与k-2个子节点断开 再重新连

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 2000010
struct node
{
int u,v,next;
}ed[N<<];
int n,head[N],t,ans;
void init()
{
t = ;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
ed[t].u = u;
ed[t].v = v;
ed[t].next = head[u];
head[u] = t++;
}
int dfs(int pre,int u)
{
int i;
int s=;
for(i = head[u] ; i!=- ; i = ed[i].next)
{
int v = ed[i].v;
if(v==pre) continue;
s+=dfs(u,v);
}
if(s>=)
{
if(u!=)
ans+=*(s-);
else
ans+=*(s-);
return ;
}
return ;
}
int main()
{
int i,tt,u,v;
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
init();ans=;
for(i = ; i < n ; i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(-,);
printf("%d\n",ans+);
}
return ;
}

hdu4714Tree2cycle的更多相关文章

随机推荐

  1. oc 基础知识

    一.枚举 结构体 typedef enum{ sexMan, sexWoman }Sex;   tydedef struct{    int year;    int month;    int da ...

  2. Linq 查询 与方法调用

    通常,使用linq查询时需要一个实现IQueryable<T> 的查询对象 public class DataA<T> : IQueryable<T> {....} ...

  3. HDU 1058 Humble Number

    Humble Number Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humbl ...

  4. Cabarc Overview (Microsoft TechNet)

    Original Link:  Cabarc Overview Applies To: Windows Server 2003, Windows Server 2003 R2, Windows Ser ...

  5. c++实现类似Common Lisp的多参数加法和比较

    在CL里我们可以这样: $ sbcl * (+ 1 2 3) 6 * (< 1 2 3) T * (< 2 3 1) NIL * 从简单的方面看, CL的+和<就是一个接收多参数的函 ...

  6. CentOS7 安装LAMP环境

    1.使用yum安装 yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-pos ...

  7. 用EPPlus导入导出数据到excel

    项目上中要用到将数据库中所有表导出为Excel,以及将Excel数据导入数据库中的操作,使用EPPlus组件,编写以下两个函数. using OfficeOpenXml;using OfficeOpe ...

  8. 解决mac os x 10.9.1 AppStore ‘Use the Purchases page to try again’ 问题

    方法一: 关闭AppStore Terminal: open $TMPDIR/../C 删除 com.apple.appstore 下所有文件后进入AppStore重新下载 方法二: Terminal ...

  9. android适应屏幕

    生产android手机的厂商多不胜数,造就了android手机的屏幕尺寸也是不计其数.开发者为了使应用在各个品牌,各个型号的手机屏幕上保持一致的用户体验,就需要运用多种使应用的UI能适应不同屏幕尺寸的 ...

  10. 字符编解码的故事 字符集 GBK GB2312 GB18030 Unicode 的由来和区别

    本文为转载. 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们认为8个开关状态作为原子单位很好,于是他们把这称为"字节". 再后来 ...