POJ 1848 Tree
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3506 | Accepted: 1204 |
Description
Input
N
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
Sample Input
7
1 2
1 3
3 5
3 4
5 6
5 7
Sample Output
2
Source

#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的更多相关文章
- POJ 1848 Tree 树形DP
题目大意: 给出一棵树,现在要往这棵树上加边,使得所有的点都在环中,且每个点只能属于一个环 题解: 考虑DP: \(dp[i][0]\)表示使\(i\)这颗子树的每个点都在环内需要加的最少边数. \( ...
- poj 3237 Tree [LCA] (树链剖分)
poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...
- poj 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 3723 Tree(树链剖分)
POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- elasticsearch term 查询之一
1.前言 term级别查询将按照存储在倒排索引中的确切字词进行操作,这些查询通常用于数字,日期和枚举等结构化数据,而不是全文本字段. 或者,它们允许您制作低级查询,并在分析过程之前进行. term级别 ...
- numpy基础知识
官网简介: http://www.numpy.org/ ndarry基本属性 ndarry是Numpy中的N维数组对象(N dimentional arrya,ndarray) ndarry中所有的元 ...
- JSP页面的基本结构 及声明变量
一.JSP页面的基本结构 在传统的HTML页面文件里增加Java程序片和JSP标签就构成了一个JSP页面文件. 一个JSP页面可由5种元素组合而成: 1.普通的HTML标记符 2.Jsp标签.如指令标 ...
- 各大网站css初始化代码【转】
文章来源:http://blog.sina.com.cn/s/blog_71ed1b870101a52w.html 腾讯QQ官网(http://www.qq.com)样式初始化 body,ol,ul, ...
- [转载]Android开发者必须深入学习的10个应用开源项目
[转载]Android开发者必须深入学习的10个应用开源项目 原文地址:Android开发者必须深入学习的10个应用开源项目(http://blog.sina.com.cn/s/blog_7b8a63 ...
- JS动态插入HTML后不能执行后续JQUERY操作
通过js追加的html 发现 不能点击 执行函数 普通绑定事件:$('.btn1').click(function(){}绑定 事件委托 解决方法: $("body").d ...
- Vue 组件开发demo
1.代码地址 github:https://github.com/MengFangui/VueComponentDemo- 2.关键代码 (1)main.js //引入vue import Vue f ...
- /proc/meminfo详解 = /nmon analysis --MEM
memtotal hightotal lowtotal swaptotal memfree highfree lowfree swapfree memshared cached active bigf ...
- .Net 使用的快捷键
快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT + C显示类视 ...
- 保存Hive查询结果的方法
很多时候,我们需要将Hive的查询(select)结果保存起来,方便进一步处理或查看.在Hive里面提供了不同的方式来保存查询结果,在这里做下总结: 一.保存结果到本地 方法1:调用hive标准输出, ...