POJ 1741 树分治
来源:互联网 发布:手机主题桌面软件 编辑:程序博客网 时间:2024/05/18 01:30
思路:
http://blog.sina.com.cn/s/blog_6d5aa19a0100o73m.html
//By SiriusRen#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 20005int n,k,xx,yy,zz,first[N],next[N],v[N],w[N],tot;int deep[N],ans,root,size[N],f[N],vis[N],sum,d[N];void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}void getroot(int x,int fa){ size[x]=1,f[x]=0; for(int i=first[x];~i;i=next[i])if(!vis[v[i]]&&v[i]!=fa){ getroot(v[i],x); size[x]+=size[v[i]]; f[x]=max(f[x],size[v[i]]); } f[x]=max(f[x],sum-f[x]); if(f[x]<f[root])root=x;}void getdeep(int x,int fa){ deep[++deep[0]]=d[x]; for(int i=first[x];~i;i=next[i]) if(!vis[v[i]]&&v[i]!=fa&&d[x]+w[i]<=k) d[v[i]]=d[x]+w[i],getdeep(v[i],x);}int calc(int x,int now){ d[x]=now,deep[0]=0,getdeep(x,0); sort(deep+1,deep+deep[0]+1); int t,l,r; for(t=0,l=1,r=deep[0];l<r;) if(deep[l]+deep[r]<=k)t+=r-l,l++; else r--; return t;}void work(int x){ ans+=calc(x,0),vis[x]=1; for(int i=first[x];~i;i=next[i])if(!vis[v[i]]){ ans-=calc(v[i],w[i]),sum=size[v[i]]; root=0,getroot(v[i],root),work(root); }}int main(){ while(scanf("%d%d",&n,&k)&&(n||k)){ memset(first,-1,sizeof(first)); memset(vis,0,sizeof(vis)); f[0]=10005,root=ans=tot=0,sum=n; for(int i=1;i<n;i++){ scanf("%d%d%d",&xx,&yy,&zz); add(xx,yy,zz),add(yy,xx,zz); } getroot(1,0),work(root); printf("%d\n",ans); }}
0 0
- POJ 1741 树分治
- POJ 1741 树分治
- 树的分治 poj 1741
- POJ 1741 树的分治
- poj 1741 (树的分治)
- POJ 1741 树的分治
- poj 1741 Tree 树分治
- poj 1741 Tree | 树分治
- POJ - 1741(树分治)
- poj 1741 树的分治
- poj 1741 Tree 树分治
- 【树分治】 POJ 1741 Tree
- POJ 1741(树分治)
- 【POJ 1741】Tree(树分治)
- poj 1741 树分治入门
- poj 1741 - Tree 树分治
- [POJ 1741]Tree[树分治]
- poj 1741 树的分治。。。。
- Laravel 5 Failed opening required bootstrap/../vendor/autoload.php 报错
- 【SSH网上商城项目实战08】查询和删除商品类别功能的实现
- BootStrap 3 模态框禁用空白处点击关闭
- Android开发 WebView TextToSpeech异常
- 三国乐投晨报 1月12日
- POJ 1741 树分治
- Mongodb 在update时 org.springframework.data.mapping.context.InvalidPersistentPropertyPath: No property
- Shell并发编程Demo
- Java中Json转换Bean对象并忽略大小写!
- 1151: 零起点学算法58——开灯问题
- 浅析Spring AOP源码(十六) 分析ProxyFactory
- 模拟HashSet
- <转> Spark 线性代数库 Breeze API 详解
- shape