点分模板题

都快改的跟题解一模一样了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. 使用datatables实现后台分页功能,减轻前端渲染压力

    注意不同版本,参数名字及参数内容存在差异,具体可以参考https://datatables.net/upgrade/1.10-convert#Options 控制页面显示的参数:https://dat ...

  2. MIUI应用权限设置

    不管你认为我写的好坏都能够在以下评论告诉我,你的支持是我继续写下去的动力,谢谢. 随着miui越来越封闭,小米对非自由渠道的应用限制越来越苛刻.我们公司的产品一半以上的用户都是来自小米,并且像我们这种 ...

  3. PHP引用是什么?

    引用是什么 在 PHP 中引用意味着用不同的名字访问同一个变量内容.这并不像 C 的指针,替代的是,引用是符号表别名.注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字.最 ...

  4. UVa 10773 - Back to Intermediate Math

    题目:渡河问题.给你河水宽度,水流速度,求垂直渡河与最快渡河的时间差. 分析:物理题,数学题. 最快渡河情况,传垂直运动,垂直渡河,船的水平分速度和水流速度抵消. 说明:注意水流速度不能为0. #in ...

  5. 设计模式-(12)迭代器模式 (swift版)

    一,概念 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常用的设计模式.这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示.迭代器模式属于行为型 ...

  6. Hadoop Hive概念学习系列之hive里的索引(十三)

    Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapReduce任务中需要 ...

  7. Lightoj 1009 - Back to Underworld

    1009 - Back to Underworld    PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 ...

  8. Linux shell 脚本中变量的数学计算【转】

    本文转载自:http://blog.csdn.net/qinghezhen/article/details/9194287 首先从一个例子说起: x=1+1 echo $x 你是不是期待着输出2啊?让 ...

  9. apply current folder view to all folders

    https://www.tenforums.com/tutorials/35093-apply-folder-view-all-folders-same-type-windows-10-a.html ...

  10. YTU 2902: H-Sum 3s

    2902: H-Sum 3s 时间限制: 1 Sec  内存限制: 128 MB 提交: 139  解决: 28 题目描述 You are given a number sequence a1,a2, ...