POJ1741(点分治)
分治的时候SZ感觉是错的……但是貌似第一次找好重心就够了,之后SZ别太离谱就不会T,重心随一随缘就好……
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e4 + 5;
int n, k, mx, SZ, ans;
struct Edge {
int to, nxt, cost;
}e[maxn << 2];
int head[maxn], tot, size[maxn], root, dis[maxn], vis[maxn];
int l, r, Q[maxn];
void add(int u, int v, int c) {
e[++tot].to = v, e[tot].cost = c, e[tot].nxt = head[u], head[u] = tot;
}
void GetRoot(int cur, int fa) {
int tmp = 0;
size[cur] = 1;
for (int i = head[cur]; i; i = e[i].nxt) {
int son = e[i].to;
if (son == fa || vis[son]) continue;
GetRoot(son, cur);
size[cur] += size[son];
tmp = max(tmp, size[son]);
}
tmp = max(tmp, SZ - size[cur]);
if (tmp < mx) mx = tmp, root = cur;
}
void GetDis(int cur, int fa) {
Q[++r] = dis[cur];
for (int i = head[cur]; i; i = e[i].nxt) {
int son = e[i].to;
if (son == fa || vis[son]) continue;
dis[son] = dis[cur] + e[i].cost;
GetDis(son, cur);
}
}
int calc(int cur, int val) {
l = 1, r = 0;
dis[cur] = val;
GetDis(cur, 0);
sort(Q + 1, Q + 1 + r);
int res = 0;
while (l < r) {
if (Q[l] + Q[r] <= k) res += r - l, l++;
else r--;
}
return res;
}
void divide(int cur) {
ans += calc(cur, 0);
vis[cur] = 1;
for (int i = head[cur]; i; i = e[i].nxt) {
int son = e[i].to;
if (vis[son]) continue;
ans -= calc(son, e[i].cost);
mx = 2e9, SZ = size[son];
GetRoot(son, 0);
divide(root);
}
}
void init() {
ans = 0, tot = 0, mx = 2e9, SZ = n;
for (int i = 1; i <= n; i++) head[i] = 0, vis[i] = 0;
}
int main() {
while (scanf("%d %d", &n, &k) == 2 && (n | k)) {
init();
for (int u, v, cost, i = 1; i < n; i++) {
scanf("%d %d %d", &u, &v, &cost);
add(u, v, cost), add(v, u, cost);
}
GetRoot(1, 0);
divide(root);
printf("%d\n", ans);
}
return 0;
}
POJ1741(点分治)的更多相关文章
- POJ1741 点分治模板
传送门:http://poj.org/problem?id=1741 题意: 求树上两点间路径长度小于k的点对个数 题解: 参考资料 守望的淀粉质略解:https://www.luogu.org/bl ...
- POJ-1741 树上分治--点分治(算法太奇妙了)
给你1e5个节点的树,(⊙﹏⊙) 你能求出又几对节点的距离小于k吗??(分治NB!) 这只是一个板子题,树上分治没有简单题呀!(一个大佬说的) #include<cstdio> #incl ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- 【POJ1741】Tree(点分治)
[POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- [bzoj1468][poj1741]Tree_点分治
Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...
- Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
[POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...
- poj1741(入门点分治)
题目链接:https://vjudge.net/problem/POJ-1741 题意:给出一棵树,求出树上距离不超过k的点对数量. 思路:点分治经典题.先找重心作为树根,然后求出子树中所有点到重心的 ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- Mybatis 批量删除 单引号
MySQL效果: ' AND NAME IN ('policycustom1.xmlx','policycustom.xmlx','policycustom1.xmlx','policycustom. ...
- 《java编程思想》:异常丢失
finally子句的不恰当使用,会造成异常的丢失,此处列举两种典型的错误使用示例.编程中要避免这种情况 示例一: try{ throw new ExceptionA(); }finally{ thro ...
- 浅析linux 下shell命令执行和守护进程
执行shell脚本有以下几种方式 1.相对路径方式,需先cd到脚本路径下 [root@banking tmp]# cd /tmp [root@banking tmp]# ./ceshi.sh 脚本执行 ...
- NO1:安装VMLinux虚拟机,安装配置Samba实现Linux与Windows文件共享
离开技术好些年,仅凭记忆开始学习.同时决定在Linux系统学习C语言. 一.下午安装了VM 8.0,安装RedHat Enterpris Server 6.4虚拟操作系统,都还比较顺利. 二.要实现L ...
- 在Asterisk CLI里面采用originate发起一个呼叫
Asterisk cli下面可以执行很多命令,originate的用途是发起一个呼叫然后连接到指定的应用或上下文. 跟.call呼叫文件和AMI管理接口里的外呼功能一样,有两种语法格式: 呼叫成功转应 ...
- RabbitMQ的持久化机制
一.问题的引出 RabbitMQ的一大特色是消息的可靠性,那么它是如何保证消息可靠性的呢?——消息持久化.为了保证RabbitMQ在退出,服务重启或者crash等异常情况下,也不会丢失消息,我们可以将 ...
- docker的操作
查询容器 docker ps 只能查询到正在运行的docker镜像: 如果添加上-a的选项,则会显示所有的(包括已经exit,未启动)的容器 基于一个镜像来构建(run)容器,并启动 docker ...
- BZOJ3123:[SDOI2013]森林
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- ubuntu14.04 设置默认使用root用户登录
首先修改/etc/lightdm/lightdm.conf,设置autologin-user=root 但是此时直接重启,会报如下的错误: 解决办法: 使用命令: vim /root/.profile ...
- Spring 源码解析之DispatcherServlet源码解析(五)
spring的整个请求流程都是围绕着DispatcherServlet进行的 类结构图 根据类的结构来说DispatcherServlet本身也是继承了HttpServlet的,所有的请求都是根据这一 ...