POJ 1741 单次询问树上距离<=K的点对数 点分治
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + ;
const int MAXM = 1e5 + ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], ed = ;
int cost[MAXM << ];
inline void addedge(int u, int v, int c) {
to[++ed] = v;
cost[ed] = c;
nxt[ed] = Head[u];
Head[u] = ed;
}
inline void ADD(int u, int v, int c) {
addedge(u, v, c);
addedge(v, u, c);
}
int n, k, anser;
int sz[MAXN], f[MAXN], dep[MAXN], sumsz, root;
bool vis[MAXN];
int o[MAXN], cnt;
void getroot(int x, int fa) {
sz[x] = ;
f[x] = ;
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (v == fa || vis[v]) {
continue;
}
getroot(v, x);
sz[x] += sz[v];
f[x] = max(f[x], sz[v]);
}
f[x] = max(f[x], sumsz - sz[x]);
if (f[x] < f[root]) {
root = x;
}
}
void getdeep(int x, int fa) {
o[++cnt] = dep[x];
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (v == fa || vis[v]) {
continue;
}
dep[v] = dep[x] + cost[i];
getdeep(v, x);
}
}
int calc(int x, int d) {
cnt = ;
dep[x] = d;
getdeep(x, );
sort(o + , o + cnt + );
int l = , r = cnt, ansnow = ;
while (l < r) {
if (o[l] + o[r] <= k) {
ansnow += r - l, l++;
} else {
r--;
}
}
return ansnow;
}
void solve(int x) {
anser += calc(x, ); //满足Depth(i)+Depth(j)<=k的对数
vis[x] = ;
int totsz = sumsz;
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (vis[v]) {
continue;
}
anser -= calc(v, cost[i]);//满足Depth(i)+Depth(j)<=k且Belong(i)=Belong(j)的对数
root = ;
sumsz = sz[v] > sz[x] ? totsz - sz[x] : sz[v];
getroot(v, );
solve(root);
}
}
int main() {
while (scanf("%d %d", &n, &k) && (n + k)) {
cnt = anser = ;
memset(Head, , sizeof(Head));
memset(vis, , sizeof(vis));
ed = ;
int u, v, c;
for (int i = ; i < n; i++) {
scanf("%d %d %d", &u, &v, &c);
ADD(u, v, c);
}
root = , sumsz = f[] = n;
getroot(, );
solve(root);
printf("%d\n", anser);
}
return ;
}
POJ 1741 单次询问树上距离<=K的点对数 点分治的更多相关文章
- P3806 离线多次询问 树上距离为K的点对是否存在 点分治
询问树上距离为k的点对是否存在 直接n^2暴力处理点对 桶排记录 可以过 #include<cstdio> #include<cstring> #include<algo ...
- poj 1741 楼教主男人八题之中的一个:树分治
http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...
- POJ 1741:Tree(树上点分治)
题目链接 题意 给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 思路 <分治算法在树的路径问题中的应用> 图片转载于http://www.cnblogs.com/Paul-Gu ...
- 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在
P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 树上的点分治
题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...
- POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12276 Accepted: 3886 Description ...
随机推荐
- Books Exchange (easy version) CodeForces - 1249B2
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- 李宏毅 线性回归预测PM2.5
作业说明 给定训练集train.csv,要求根据前9个小时的空气监测情况预测第10个小时的PM2.5含量. 训练集介绍: (1):CSV文件,包含台湾丰原地区240天的气象观测资料(取每个月前20天的 ...
- Pairs of Numbers
#include<stdio.h> //we have defined the necessary header files here for this problem. //If add ...
- 前端CSS实现图片自适应背景大小
<body> <div> <!--背景图片--> <div id="web_bg" style="background-imag ...
- [SDOI2011]消耗战 题解
题面 虚树的模板题: 虚树的思想是只保留有用的点(在这道题目里面显然是标记点和lca),然后重新构建一棵树,从而使节点大大减少,优化复杂度 我们维护一条链(以1号点为根),这条链左边的所有在虚树上的位 ...
- Win32汇编之其他指令
汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操作码,用地 ...
- Codeforces 1239A. Ivan the Fool and the Probability Theory
传送门 注意到连续两个格子如果有相同颜色那么一路过去的都可以确定 比如一开始染了这两个位置: 然后发现后面整片过去都可以确定: 对于横着的情况也是一样,然后就会发现不可能出现横着两个和竖着两个同时都有 ...
- 六一广告页H5全屏滚动效果实现
明天就六一儿童了(放假了),在这里提前祝大家周末快乐,每逢节假日公司必然会推出h5活动页的需求,这次六一儿童节也不例外,产品这次倒是没提什么互动效果需求,只不过根据UI妹子给的设计图,图片与图片中颜色 ...
- Digester库
在之前所学习关于启动简单的Tomcat部分实现的代码中,我们使用一个启动类Bootstrap类 来实例化连接器.servlet容器.wrapper实例.和其他组件,然后调用各个对象的set方法将他们关 ...
- JDBC 24homework
编写程序: 1. 创建商品信息表Goods(包含编号Code.名称Name.数量Number.单价Price) 2. 设计程序流程,由用户选择:插入.删除.修改.查询 程序效果如下: (1)根据提示输 ...