【POJ1741】tree
来源:互联网 发布:淘宝开店考试答案2015 编辑:程序博客网 时间:2024/06/05 09:51
题解:
点分治第一题
其实除了统计答案这一步,其余基本都是套路
代码同法二
//by sdfzchy#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int inf=(1<<30),N=10010;int n,k;inline int in(){ char ch=getchar(); int f=1,tmp=0; while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();} return tmp*f;}struct E{ int to,nxt,dis;}e[N<<1];int sum,root,ecnt;int d[N],dis[N],head[N];void add(int u,int v,int w){ ++ecnt; e[ecnt]=(E){v,head[u],w}; head[u]=ecnt; ++ecnt; e[ecnt]=(E){u,head[v],w}; head[v]=ecnt;}bool vis[N];int siz[N];void dfs_root(int u,int F){ d[u]=0; siz[u]=1; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(vis[v]||v==F) continue; dfs_root(v,u); siz[u]+=siz[v]; d[u]=max(siz[v],d[u]); } d[u]=max(d[u],sum-siz[u]); if(d[u]<d[root]) root=u; }int q[N];void dfs_dep(int u,int F){ q[++q[0]]=dis[u]; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(vis[v]||v==F) continue; dis[v]=dis[u]+e[i].dis; dfs_dep(v,u); }}int cal(int u,int cur){ int ret=0; dis[u]=cur; q[0]=0; dfs_dep(u,0); sort(q+1,q+q[0]+1); int l=1,r=q[0]; while(l<r) { if(q[l]+q[r]<=k) ret+=r-l,l++; else r--; } return ret;}int ans;void dfs_sol(int u){ vis[u]=1; ans+=cal(u,0); for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(vis[v]) continue; ans-=cal(v,e[i].dis); sum=siz[v]; root=0; dfs_root(v,u); dfs_sol(root); }}int main(){ while(1) { ecnt=0; memset(head,0,sizeof(head)); memset(vis,0,sizeof(vis)); n=in(),k=in(); if(n==0) break; for(int i=1,u,v,w;i<n;i++) { u=in(),v=in(),w=in(); add(u,v,w); } sum=n; root=0; ans=0; d[0]=inf; dfs_root(1,0); dfs_sol(root); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- poj1741 Tree
- poj1741 Tree
- POJ1741 Tree
- 【poj1741】【Tree】
- [POJ1741]Tree
- POJ1741 Tree
- poj1741 Tree
- poj1741 Tree
- POJ1741 Tree
- 【poj1741】 Tree
- poj1741 Tree
- POJ1741 Tree.
- poj1741 Tree
- 【poj1741】Tree
- poj1741 Tree
- 【poj1741】Tree
- poj1741 Tree
- POJ1741-Tree
- Winfrom调用百度接口做的图片识别,带发音
- 微信小程序笔记1
- 数据库——游标
- mysql分组排序加序号(不用存储过程,就简简单单sql语句哦)
- IT行业的风投
- 【POJ1741】tree
- 笔记:udacity计算机科学导论- 课程11/19 python取幂, DRAM 存储原理、容量、速度
- win10安装scikit-image错误: geometry.obj" failed with exit status 127
- 071day(求排列的逆序数的解决和分治例题(最长公共子序列)的学习)
- WPF 绘制对齐像素的清晰显示的线条
- 9、新手入手树莓派教程--WiringPi控制GPIO
- POJ 1386(欧拉回路,欧拉通路,变相DNF)
- 奶爸英语学习课程要点(第10课, 如何积累两万词汇, 高级)
- Android App Inventor for the Absolute Beginner.pdf 英文原版 免费下载