【POJ】1741 Tree
来源:互联网 发布:调用接口获取json数据 编辑:程序博客网 时间:2024/04/20 18:41
传送门:http://poj.org/problem?id=1741
点分裸题
感谢zw+1……
#include<stdio.h>#include<algorithm>#include<cstring>#define Push(a,b,c) push(a,b,c),push(b,a,c)#define cint const int &#define ___ cint u,cint f=0#define V e[_].v#define For for (int _=s[u];_;_=e[_].n) if (V!=f && !vis[V])using namespace std;#define N 10005int tot,s[N],dn[N],dis[N],st[N],key[N],ed,sum,C,ans,n,K;bool vis[N];struct edge{int v,c,n;}e[N<<1];inline void push(cint u,cint v,cint c){e[++tot]=(edge){v,c,s[u]};s[u]=tot;}void dfs(___){dn[u]=1;For dfs(V,u),dn[u]+=dn[V];}void find(___){ key[u]=sum-dn[u]; For find(V,u),key[u]=max(key[u],dn[V]); if (key[u]<key[C]) C=u;}void getd(___){For dis[V]=dis[u]+e[_].c,getd(V,u);}void ins(___){st[++ed]=dis[u];For ins(V,u);}inline int gets(cint u){ ed=0; ins(u); int rt=0,p2=ed; sort(st+1,st+ed+1); for (int p1=1;p1<p2;p1++) { while (K<st[p1]+st[p2] && p1<p2) p2--; rt+=p2-p1; } return rt;}void solve(___){ vis[u]=1;dis[u]=0; getd(u); ans+=gets(u); For ans-=gets(V); For dfs(V),sum=dn[V],C=0,find(V),solve(C);}inline void prepare(){ key[0]=n+1; tot=ans=0; memset(s,0,n+2<<2); memset(vis,0,n+2);}int main(){ while (scanf("%d%d",&n,&K),n) { prepare(); for (int i=1,u,v,c;i<n;i++) scanf("%d%d%d",&u,&v,&c),Push(u,v,c); solve(1); printf("%d\n",ans); }}
0 0
- POJ 1741 Tree
- POJ 1741 Tree
- 【POJ 1741】Tree
- poj-1741 Tree
- [POJ 1741]Tree
- poj 1741 bzoj1468 Tree
- POJ 1741Tree
- Poj 1741 Tree
- POJ 1741 Tree
- POJ 1741 Tree
- POJ 1741 Tree
- Poj 1741 Tree
- 【POJ】1741 Tree
- POJ 1741 Tree
- poj 1741Tree
- poj 1741 tree
- POJ 1741 Tree 笔记
- POJ 1741 Tree
- 2016-CSU1803(同余公式)
- Learn web development css初步
- Servlet监听器——实现在线登录人数统计小例子
- android颜色对应的xml配置值,颜色表
- unix系统之getpid()
- 【POJ】1741 Tree
- java中 set,list,array互转
- C#基础语言
- 苹果笔记本开机出现闪烁的问号文件夹解决方法
- 使用Spring实现读写分离( MySQL实现主从复制)
- java泛型概要
- Spring-4,IOC容器
- 使用归并思想查找数组中的逆序对的数量——Java实现
- 1_简答的多项式运算