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. 在Centos 7中使用 Docker搭建MySQL异地双向复制环境

    (0)一些准备操作: Centos安装好之后(这里使用的是vm虚拟机) 将当前用户添加到sudoers中: su root vim /etc/sudoers 找到 root ALL=(ALL) ALL ...

  2. dubbo方法调用的timeout设置

    方法调用的默认超时时间为1s,但是具体的超时时间受限于服务端方法性能.服务端个数.客户端的并发数等因素,所以超时时间需要根据不同的场景进行调试. 基本步骤为: 测试服务端的TPS,单位为 任务数或线程 ...

  3. docker集群——介绍Mesos+Zookeeper+Marathon的Docker管理平台

    容器为用户打开了一扇通往新世界的大门,真正进入这个容器的世界后,却发现新的生态系统如此庞大.在生产使用中,不论个人还是企业,都会提出更复杂的需求.这时,我们需要众多跨主机的容器协同工作,需要支持各种类 ...

  4. 火车票订票API 用PHP完成火车票订票流程

    本教程用来演示聚合数据-火车票订票接口的使用流程. 配置好PHP环境,PHP版本最好大于5.5 去聚合数据-火车票订票接口申请key:http://www.juhe.cn/docs/api/id/17 ...

  5. python——TypeError: 'str' does not support the buffer interface

    import socket import sys port=51423 host="localhost" data=b"x"*10485760 #在字符串前加 ...

  6. Odoo8.0中允许用户动态调整TreeView栏位宽度

    现有的Odoo8.0中TreeView的栏位宽度是固定的,不可以手动调整,通过安装第三方插件后,可以实现手工动态调整. 下载模块安装即可.http://download.csdn.net/detail ...

  7. prototype 用法

    prototype使得js面向对象使用了prototype之后,使用它里面的属性或者函数 需要new出一个对象才可以使用.否则不使用prototype,直接向对象注入 function Person( ...

  8. linux 自启动

    使用chkconfig命令可以查看在不同启动级别下课自动启动的服务(或是程序),命令格式如下:chkconfig --list可能输出如下:openvpn 0:关闭 1:开启 ...... 6:关闭 ...

  9. [原创]FreeSWITCH实现多人来电思路

    [原创]FreeSWITCH实现多人来电思路 场景介绍 该篇文章主要用于介绍如何使用FreeSWITCH实现通讯系统中常见的多人来电功能. 具体场景如下: A与B正在通话中,此时C拨打A/B,Free ...

  10. 李洪强iOS开发之OC[002] - OC中注释以及@符号的使用