点分模板题

都快改的跟题解一模一样了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. Enhance Magento 404 page

    Magento default installation already has a predefined custom 404 page (no-route). But is it enough t ...

  2. [Unit Testing] Mock an HTTP request using Nock while unit testing

    When testing functions that make HTTP requests, it's not preferable for those requests to actually r ...

  3. 【求建议】毕业之声——信院IT类毕业学子经验分享交流会

    一:缘由 在和非常多学子交流,及上课的经历中,发现一个非常普遍的现象:部分大一学生即失去了对学习.对专业的兴趣.有人在迷茫之后奋起直追.从而珍惜利用不多的大学时光努力提高自己.有人在迷茫中沉沦,沉迷于 ...

  4. Input系统—ANR原理分析(转)

    一. 概述 当input事件处理得慢就会触发ANR,那ANR内部原理是什么,哪些场景会产生ANR呢. “工欲善其事必先利其器”,为了理解input ANR原理,前面几篇文章疏通了整个input框架的处 ...

  5. Ubuntu16.04 下python2 | python3

    在终端分别输入python,python2,python3 python和python2默认都是python2 python3才是python3 Ubuntu下是默认没有pip的,需要自己手动安装 s ...

  6. POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)

    Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10244   Accepted: 3864   S ...

  7. [IT学习]转载python 项目 计算器

    这个是从网上搜到的Python小项目之计算器(原文地址:http://www.2cto.com/kf/201402/279637.html).但该段代码估计是Python 2 写的. 如果你使用的程序 ...

  8. JS之RegExp对象(二)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zkn_CS_DN_2013/article/details/24243159 RegExp对象的经常 ...

  9. iOS设备,fixed布局出问题

    window.deviceId = '{{$deviceId}}'; window.iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? t ...

  10. C#温度报警

    using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...