点分模板题

都快改的跟题解一模一样了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的更多相关文章

  1. Apple Tree POJ - 2486

    Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...

  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 ...

  3. AC日记——Tree poj 3237

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9233   Accepted: 2431 Description ...

  4. Tree POJ - 1741【树分治】【一句话说清思路】

    因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...

  5. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  6. 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. ...

  7. Is It A Tree? POJ - 1308(并查集判树)

    Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...

  8. 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) ...

  9. 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 ...

随机推荐

  1. Meteor事件

    使用事件是非常简单的.我们将学习如何使用tag,class 和id作为事件选择器. 让我们创建HTML模板三大要素.第一个是 p 标签,第二个是 myClass 类,最后一个是myId. meteor ...

  2. ExpandableListView的使用以及信息的高亮显示

    ExpandableListView是ListView控件的延伸,它能够对数据进行分组显示和隐藏,并统计总数量.可进行滚动,对某一内容高亮显示. <1>编写xml布局文件,用于获取Expa ...

  3. mysql手记

    myisam innoDB是mysql经常使用的存储引擎 MyISAM不支持事务.也不支持外键.但其訪问速度快.对事务完整性没有要求. InnoDB存储引擎提供了具有提交.回滚和崩溃恢复能力的事务安全 ...

  4. 【Mongodb教程 第一课 】 MongoDB下载安装

    MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.以window平台 ...

  5. 下载Youku视频观看

    所需工具: 1.chorme内核浏览器,如Chorme.Firefox等等 2.vlc视频播放器 准备工作完成,开始工作 1.打开优酷的随便一个视频 2.按下F12选择Network(网络)选择Med ...

  6. 像感冒一样的contains error

    转自 http://blog.csdn.net/zhufuing/article/details/8135270          Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个 ...

  7. asp.net 实现搜索站内搜索功能

    首先有index和search 两个页面 index页面中有textbox1和button1两个控件 双击button1控件添加代码: protected void Button1_Click(obj ...

  8. 内存充足,但是为什么hadoop3无法启动nodemanager

    [root@hadoop3 hadoop]# xloStarting namenodes on [hadoop3]上一次登录:三 12月 27 16:06:01 CST 2017pts/24 上Sta ...

  9. maximize_window fullscreen_window minimize_window

    # Licensed to the Software Freedom Conservancy (SFC) under one# or more contributor license agreemen ...

  10. 通过fsharp 使用Enterprise Library Unity 3 - 三种拦截模式的探索

    这篇就三种拦截模式进行一下探索. 特性总结   类型 特点 其它 InterfaceInterceptor Innstance 仅单接口 类内部函数互相引用无法引起拦截行为 TransparentPr ...