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 ...
随机推荐
- [原创+分享]Mandelbrot Explorer
Mandelbrot Explorer 是一款用于在MandelBort集/Julia集上进行无限漫游的软件,使用VS2013+CUDA6.5开发而成.它也是我学习CUDA开发的一个小小的成果,欢迎大 ...
- 到底该不该使用存储过程 MySQL查询性能优化一则
到底该不该使用存储过程 看到<阿里巴巴java编码规范>有这样一条 关于这条规范,我说说我个人的看法 用不用存储过程要视所使用的数据库和业务场景而定的,不能因为阿里巴巴的技术牛逼,就视 ...
- css3动画入门transition、animation
css3动画 transition.animation CSS3 transition demo <!DOCTYPE html> <html> <head> < ...
- Installation error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
解决方法: http://my.oschina.net/u/242764/blog/375909 当我们安装好Genymotion后,把Android运用部署到上面调试时,console 控制台会报错 ...
- CASE UPDATE
https://leetcode-cn.com/problems/swap-salary/description/ Given a table salary, such as the one belo ...
- 初识JVM虚拟机
前言: Java语言里负责解释执行字节码文件的是Java虚拟机,即JVM——Java Virtual Machine(Java虚拟机). 执行Java程序的两个步骤: 由Java语言编写的程序需要进过 ...
- Lightoj 1009 - Back to Underworld
1009 - Back to Underworld PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 ...
- import-module in $profile
$PROFILE C:\Users\clu\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Import-Module 'C:\Users\ ...
- BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离
BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离 Description It's a hot summer day, and Bessie the c ...
- 循环冗余检验 (CRC) 算法原理
Cyclic Redundancy Check循环冗余检验,是基于数据计算一组效验码,用于核对数据传输过程中是否被更改或传输错误. 算法原理 假设数据传输过程中需要发送15位的二进制信息g=10100 ...