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 ...
随机推荐
- spring利用后置处理器初始化bean属性
spring利用后置处理器初始化bean属性 参考:http://blog.csdn.net/heyutao007/article/details/50326793 @Configurable @En ...
- Node.js abaike图片批量下载Node.js爬虫1.00版
这个与前作的差别在于地址的不规律性,需要找到下一页的地址再爬过去找. //====================================================== // abaik ...
- hibernate学习系列-----(8)hibernate对集合属性的操作之Collectioon集合篇
度过短暂的周末,又到了周一,继续轻松而愉快的学习,紧承以前的学习,继续了解hibernate对集合的操作学习之旅! 在上一篇中,就已经提前说过,这一篇的内容以及下一篇的内容会有很多相似之处,这里就不再 ...
- 基于.NET的轻量级微信SDK
一.前言 特别不喜欢麻烦的一个人,最近碰到了微信开发.下载下来了一些其他人写的微信开发“框架”,但是被恶心到了,实现的太臃肿啦. 最不喜欢的就是把微信返回的xml消息在组装成实体类,所以会比较臃肿,现 ...
- 万字总结:学习MySQL优化原理(转)
本文转自:https://www.tuicool.com/wx/2eMBfmq 前言 说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引. ...
- QtAndroid具体解释(6):集成信鸽推送
推送是我们开发移动应用经经常使用到的功能,Qt on Android 应用也会用到,之前也有朋友问过,这次我们来看看怎么在 Qt on Android 应用中来集成来自腾讯的信鸽推送. 有关信鸽的 S ...
- Commons-VFS 使用SFTP
http://pro.ctlok.com/2011/06/apache-commons-vfs-for-sftp.html
- js var ImgObj=new Image();
API地址: 1 https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement 下面来看看Image到底是个什么东东,我先将Ima ...
- LuaStudio编辑调试软件
该编辑调试器最大特点就是能够注入到宿主程序内对lua脚本进行调试.还能够设置断点观察变量的值,功能很强大. 如今已有的编辑器有Notepad++, Editplus, luaforwindows, S ...
- RGB格式等比例缩放
原理为:将原始图像的每个像素通过一个比例关系式映射到相应的位置. /* lrgb: input 24bits rgb buffer srgb: output 24bits rgb buffer wid ...