树上点分治模板bzoj1468
来源:互联网 发布:unity3d 改变模型大小 编辑:程序博客网 时间:2024/05/22 02:02
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;struct edgee{int from, to, cost;edgee(int f, int t, int c) :from(f), to(t), cost(c){}edgee(){}};edgee edge[200000];int first[100000], nextt[200000],edgetot,n,k;int value[1000000];void addedge(int from, int to, int cost){edge[edgetot] = edgee(from, to, cost);nextt[edgetot] = first[from];first[from] = edgetot++;edge[edgetot] = edgee(to, from, cost);nextt[edgetot] = first[to];first[to] = edgetot++;}int size[100000],msize[100000];bool iscenter[100000];int center;void getsize(int now,int f,int total){size[now] = 1; msize[now] = -1;for (int i = first[now]; i != -1; i = nextt[i]){int to = edge[i].to;if (to == f||iscenter[to])continue;getsize(to, now, total);size[now] += size[to];msize[now] = max(msize[now], size[to]);}msize[now] = max(msize[now], total - size[now]);if (msize[now] < msize[center])center = now;}void getdpth(int num, int fa, int rvalue,int &tot){value[tot++] = rvalue; size[num] = 1;for (int i = first[num]; i != -1; i = nextt[i]){int to = edge[i].to;if (to == fa || iscenter[to])continue;getdpth(to, num, rvalue + edge[i].cost, tot);size[num] += size[to];}}int cal(int num,int rvalue)//这分两种一个是从center开始一个是从center的子节点开始,并且每次开始的时候size都是更新了的{int tot = 0;getdpth(num, -1, rvalue, tot);sort(value, value + tot);int l, r; l = 0; r = tot-1;int ans = 0;while (l < r){while (value[r] + value[l] <= k&&l<r)ans += r-l, l++;r--;}return ans;}int work(int s,int total,int fa){center = 0; msize[0] = 1000000000;getsize(s, -1, total);//这个时候就找centerl了;iscenter[center] = 1;int ans = 0;ans+=cal(center, 0);for (int i = first[center]; i != -1; i = nextt[i]){int to = edge[i].to;if (to == fa || iscenter[to])continue;ans -= cal(to, edge[i].cost);//这的时候重新优化了sizeans += work(to, size[to],center);//这我先开始写错了。。。。注意fa是center而不是s}return ans;}int main(){scanf("%d", &n);for (int i = 0; i <= n; i++)first[i] = -1;for (int i = 1; i < n; i++){int a, b, c;scanf("%d%d%d", &a, &b, &c);//cout << "I:" << i << " " << "a:" << a << " " << "b:" << b << " " << "c:" << c << endl;addedge(a, b, c);}scanf("%d", &k);int ans = 0;if(n)ans=work(1, n, -1);printf("%d\n", ans);}
阅读全文
0 0
- 树上点分治模板bzoj1468
- bzoj1468: Tree 点分治
- 【bzoj1468】【Tree】【点分治】
- bzoj1468 tree 点分治
- bzoj1468 Tree 点分治
- [BZOJ1468]Tree 点分治
- [bzoj1468]Tree(点分治)
- poj1741 树上点分治
- POJ1741Tree(树上点分治)
- 树上点分治入门
- 树上点分治学习记录
- POJ_P1741 Tree(树上点分治)
- JZOJ4715树上路径 点分治
- 树上基于点的分治
- 【jzoj4715】【树上路径】【树】【分治】【点分治】
- POJ 1741 Tree(树上的点分治)
- POJ 1987 Distance Statistics 树上点分治
- POJ 1987 Distance Statistics (树上点分治)
- ListView悬浮框效果及 动态tab效果
- Spark Yarn 调度器Scheduler详解
- HTML5页面如何在手机端浏览器调用相机、相册功能
- good-storage的使用
- IE8,9,10下table th不显示边框解决方法
- 树上点分治模板bzoj1468
- Makefile 经典教程(看完这篇就够了,不信你拉拉进度条)
- ERROR StatusLogger Log4j2 could not find a logging implementation.
- vs2013添加boost库
- 网络层-2、虚电路网络&数据报网络
- 【动态】矿工对以太坊拜占庭经济体制的争论持续发酵
- 酸奶市场上升趋势大 代理酸奶市场空间大
- HDU 6112 今夕何夕(水~)
- 腾讯云发布第三代云服务器矩阵,开放更强计算力赋能产业智能化