Tree
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3506   Accepted: 1204

Description

Consider a tree with N vertices, numbered from 1 to N. Add, if it is possible, a minimum number of edges thus every vertex belongs to exactly one cycle.

Input

The input has the following structure: 



x(1) y(1) 

x(2) y(2) 

... 

x(N-1) y(n-1) 

N (3 <= N <=100) is the number of vertices. x(i) and y(i) (x(i), y(i) are integers, 1 <= x(i), y(i) <= N) represent the two vertices connected by the i-th edge. 

Output

The output will contain the value -1 if the problem doesn't have a solution, otherwise an integer, representing the number of added edges.

Sample Input

7
1 2
1 3
3 5
3 4
5 6
5 7

Sample Output

2

Source


树形DP啊,可是知道也推不出公式来,。无奈的看了解题报告。发现前方的道路还非常漫长啊,这思路神了
http://www.cnblogs.com/vongang/archive/2012/08/12/2634763.html
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#define N 110
#define INF 150
using namespace std;
struct num
{
int y,next;
}a[N*N];
int b[N],Top;
int dp[N][3];
bool ch[N];
int main()
{
//freopen("data.txt","r",stdin);
void addeage(int x,int y);
void dfs(int x);
int n;
while(scanf("%d",&n)!=EOF)
{
Top = 0;
memset(b,-1,sizeof(b));
for(int i=1;i<=n-1;i++)
{
int x,y;
scanf("%d %d",&x,&y);
addeage(x,y);
addeage(y,x);
}
for(int i=1;i<=n;i++)
{
dp[i][0] = dp[i][1] = dp[i][2] = INF;
}
memset(ch,false,sizeof(ch));
dfs(1);
//cout<<dp[3][1]<<endl;
if(dp[1][0]==INF)
{
printf("%d\n",-1);
}else
{
printf("%d\n",dp[1][0]);
}
}
return 0;
}
void addeage(int x,int y)
{
a[Top].y = y;
a[Top].next = b[x];
b[x] = Top++;
}
void dfs(int x)
{
ch[x] = true;
bool uv = true;
for(int i=b[x];i!=-1;i=a[i].next)
{
int y = a[i].y;
if(!ch[y])
{
uv = false;
break;
}
}
if(uv)
{
dp[x][1] =0;
return ;
}
dp[x][1] = 0;
int sum = 0;
bool ch2[N];
memset(ch2,false,sizeof(ch2));
for(int i=b[x];i!=-1;i=a[i].next)
{
int y = a[i].y;
if(!ch[y])
{
dfs(y);
ch2[y] = true;
dp[x][1]+=dp[y][0];
sum+=dp[y][0];
}
}
if(dp[x][1]>=INF)
{
dp[x][1] = INF;
}
for(int i=b[x];i!=-1;i=a[i].next)
{
int y = a[i].y;
if(!ch2[y])
{
continue;
}
int w = sum-dp[y][0]+min(dp[y][1],dp[y][2]);
if(w>=INF)
{
w = INF;
}
dp[x][2] = min(dp[x][2],w);
w = sum-dp[y][0] + dp[y][2]+1;
if(w>=INF)
{
w = INF;
}
dp[x][0] = min(dp[x][0],w);
}
for(int i=b[x];i!=-1;i=a[i].next)
{
int y1 = a[i].y;
if(!ch2[y1])
{
continue;
}
for(int j=b[x];j!=-1;j=a[j].next)
{
int y2 = a[j].y;
if(y1==y2||!ch2[y2])
{
continue;
}
int w = sum-dp[y1][0] - dp[y2][0] + min(dp[y1][1],dp[y1][2]) + min(dp[y2][1],dp[y2][2])+1;
if(w>=INF)
{
w = INF;
}
dp[x][0] = min(dp[x][0],w);
}
}
}

POJ 1848 Tree的更多相关文章

  1. POJ 1848 Tree 树形DP

    题目大意: 给出一棵树,现在要往这棵树上加边,使得所有的点都在环中,且每个点只能属于一个环 题解: 考虑DP: \(dp[i][0]\)表示使\(i\)这颗子树的每个点都在环内需要加的最少边数. \( ...

  2. poj 3237 Tree [LCA] (树链剖分)

    poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...

  3. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  4. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  5. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  6. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  7. POJ 3723 Tree(树链剖分)

    POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...

  8. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. HA分布式集群配置三 spark集群配置

    (一)HA下配置spark 1,spark版本型号:spark-2.1.0-bin-hadoop2.7 2,解压,修改配置环境变量 tar -zxvf spark-2.1.0-bin-hadoop2. ...

  2. 向Solr数据集提交Json格式数据(Scala,Post)

    import scalaj.http.Http class SolrAdd () {// 方法接受两个参数,dataType为数据集名称,jsonString为数据json字符串 def postTo ...

  3. Java类集-set

    Set接口是Collection接口的子接口,Set接口中不能插入反复元素 Set接口的经常使用子类: HashSet是set接口的一个子类.特点:里面不能存放反复元素,并且採用散列的存储方式.所以没 ...

  4. 用Volley-nullpointerexception

    public Request(int method, String url, Response.ErrorListener listener) { mMethod = method; mUrl = u ...

  5. iOS开发-自动布局之autoresizingMask使用详解(Storyboard&Code)

    前言:现在已经不像以前那样只有一个尺寸,现在最少的IPHONE开发需要最少需要适配三个尺寸.因此以前我们可以使用硬坐标去设定各个控件的位置,但是现在的话已经不可以了,我们需要去做适配,也许你说可以使用 ...

  6. mysql分组取最大(最小、最新、前N条)条记录

    在数据库开发过程中,我们要为每种类型的数据取出前几条记录,或者是取最新.最小.最大等等,这个该如何实现呢,本文章向大家介绍如何实现mysql分组取最大(最小.最新.前N条)条记录.需要的可以参考一下. ...

  7. linux下 目录(扩容)挂载磁盘

    1.常用命令 查看硬盘的分区 #sudo fdisk -l 查看IDE硬盘信息 #sudo hdparm -i /dev/hda 查看STAT硬盘信息 #sudo hdparm -I /dev/sda ...

  8. svn:database disk image is malformed问题解决方法

    今天一客户使用我们软件时突然停电,再次启动软件查询SQLite数据库报 The database disk image is malformed 错误. 百度一下基本上全部是http://www.cn ...

  9. Spring 使用

    一.工具类中使用bean IService service = (IService)ContextLoader.getCurrentWebApplicationContext().getBean(se ...

  10. Xilinx-7Series-FPGA高速收发器使用学习—概述与参考时钟篇

    xilinx的7系列FPGA根据不同的器件类型,集成了GTP.GTX.GTH以及GTZ四种串行高速收发器,四种收发器主要区别是支持的线速率不同,图一可以说明在7系列里面器件类型和支持的收发器类型以及最 ...