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 ;
}

随机推荐

  1. 页面滚动到底部自动 Ajax 获取文章

    页面滚动到底部自动 Ajax 获取文章  代码如下 复制代码 var _timer = {};function delay_till_last(id, fn, wait) {    if (_time ...

  2. 数据库中Schema和Database有什么区别

    在MySQL中创建一个Schema好像就跟创建一个Database是一样的效果,在SQL Server和Orcal数据库中好像又不一样. 目前我只能理解,在mysql中 schema<==> ...

  3. c++ 成员函数

    #include <iostream> #include "Sales_item.h" int main() { Sales_item item1, item2; st ...

  4. lnmp停用nginx,改用apache

    编译安装的lnmp环境 总是出现502错误,修改了各种配置也没用,暂时先放弃nginx,改用apache apache使用yum安装方式 需要注意的事项,将网站根目录的用户组改为 chown apac ...

  5. hdu 1518 深搜

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  6. 【数学三角恒等变幻】【HDU2552】三足鼎立

    Problem Description MCA山中人才辈出,洞悉外界战火纷纷,山中各路豪杰决定出山拯救百姓于水火,曾以题数扫全场的威士忌,曾经高数九十九的天外来客,曾以一剑铸十年的亦纷菲,歃血为盟,盘 ...

  7. java并发包java.util.concurrent详解

    线程池ThreadPoolExecutor的使用 并发容器之CopyOnWriteArrayList 并发容器之CopyOnWriteArraySet 数据结构之ConcurrentHashMap,区 ...

  8. [NOIP2016]愤怒的小鸟 D2 T3

    Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可以用它向第一象限发射一只红色的小鸟,小鸟们的 ...

  9. idea中Hibernate反向生成工具

    HIbernate反向生成工具 1.使用idea数据库集成工具连接所需数据库 第一步 View-->Tool windows---->Database 第二步 找到所需连接的数据库 第三步 ...

  10. Finished yeah!

    终于到了最后的博客阶段,这时候才知道博客此时此刻是多么的惬意,它成了书写心声的自由平台!耗时一天完成这作业说起来也是蛮辛苦的,编译器需要新装,IDE需要熟悉,当然最主要的是之前浅入浅出的C++功底在此 ...