Tree POJ - 174
点分模板题
都快改的跟题解一模一样了2333333
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct E
{
int to,nxt,d;
}e[];
int f1[],ne;
bool vis[];
int n,k,root,sz[],fx[],sum,ans;
//sum:当前连通块节点总数
int dep[],tmp[];
void getroot(int x,int fa)
//获取x所在连通块内的根
{
sz[x]=;fx[x]=;
for(int k=f1[x];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
getroot(e[k].to,x);
sz[x]+=sz[e[k].to];
fx[x]=max(fx[x],sz[e[k].to]);
}
fx[x]=max(fx[x],sum-sz[x]);
if(fx[x]<fx[root]) root=x;
}
void getsz(int x,int fa)
//处理出当前连通块的大小
{
sz[x]=;
for(int k=f1[x];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
getsz(e[k].to,x);
sz[x]+=sz[e[k].to];
}
}
void getdeep(int u,int fa)
{
tmp[++tmp[]]=dep[u];
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
dep[e[k].to]=dep[u]+e[k].d;
getdeep(e[k].to,u);
}
}
int cal(int u,int cost)
{
dep[u]=cost;tmp[]=;
getdeep(u,);
sort(tmp+,tmp+tmp[]+);
int l,r,sum=;
r=tmp[]+;
for(l=;l<=tmp[];l++)
{
while(r>&&tmp[r-]>k-tmp[l]) r--;
if(l>=r) break;
sum+=(r-l-);
}
return sum;
}
void solve(int u)
//解决u所在连通块的问题,u作为该块的根
{
ans+=cal(u,);vis[u]=;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to])
{
ans-=cal(e[k].to,e[k].d);
root=;
getsz(e[k].to,);sum=sz[e[k].to];
getroot(e[k].to,);
solve(root);
}
}
int main()
{
int i,u,v,l;
fx[]=0x3f3f3f3f;
while()
{
memset(f1,,sizeof(f1));ne=ans=;
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&k);
if(n==&&k==) break;
for(i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&l);
e[++ne].to=v;e[ne].nxt=f1[u];e[ne].d=l;f1[u]=ne;
e[++ne].to=u;e[ne].nxt=f1[v];e[ne].d=l;f1[v]=ne;
}
sum=n;
getroot(,);
solve(root);
printf("%d\n",ans);
}
return ;
}
Tree POJ - 174的更多相关文章
- Apple Tree POJ - 2486
Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...
- E - Apple Tree POJ - 2486
E - Apple Tree POJ - 2486 Wshxzt is a lovely girl. She likes apple very much. One day HX takes her t ...
- AC日记——Tree poj 3237
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 9233 Accepted: 2431 Description ...
- Tree POJ - 1741【树分治】【一句话说清思路】
因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Apple Tree POJ - 3321 dfs序列构造树状数组(好题)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- Is It A Tree? POJ - 1308(并查集判树)
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...
- Day8 - F - Tree POJ - 1741
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...
- Day6 - J - Cartesian Tree POJ - 2201
Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binar ...
随机推荐
- 一句话从MySQL导出CSV文件
mysql -h <host> -u<user> -p<passport> crm -e "select ....." | csvcut -t ...
- windows下开发PHP扩展dll(无需Cygwin)
windows下开发php扩展网上很多资料都说需要Cygwin,其实完全可以不必安装该东东.没错,是可以在linux下生成骨架后拷到windos下来用,但是,如果没有linux环境呢?什么,装虚拟机? ...
- Python遍历路径下文件并转换成UTF-8编码
http://www.cnblogs.com/wuyuegb2312/archive/2013/01/11/2856772.html 开始学Python,这篇文章来自于应用需求. os.walk很方便 ...
- Random Forest 与 GBDT 的异同
曾经在看用RF和GBDT的时候,以为是非常相似的两个算法,都是属于集成算法,可是细致研究之后,发现他们根本全然不同. 以下总结基本的一些不同点 Random Forest: bagging (你懂得. ...
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
- 【Mongodb教程 第十六课 】 分享NO-SQL开发实战
最近研究了一下NOSQL,现整理目录如下: 一.关系数据库的瓶颈: 二.NOSQL概述: 三.NOSQL中的热门数据库MongoDB介绍及安装配置: 四.MongoDB开发模式及实战: 一.关系数据库 ...
- 【转载】TCP和TCP/IP的区别
TCP/IP协议(Transmission Control Protocol/Internet Protocol)叫做传输控制/网际协议, 又叫网络通讯协议,这个协议是Internet国际互联网络的基 ...
- MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式
MVC的验证(模型注解和非侵入式脚本的结合使用) @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...
- 像感冒一样的contains error
转自 http://blog.csdn.net/zhufuing/article/details/8135270 Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个 ...
- 实践001:char 类型字段在表中的长度
Rainy on 20170215 1.同事在 写RFC的时候遇到报错:"YTST_001" 必须为扁平结构.不能将内部表.字符# 原因是自建结构中字段定义为了string 类型. ...