POJ 1741:Tree(树上点分治)
题意
给一棵边带权树,问两点之间的距离小于等于K的点对有多少个。
思路
图片转载于http://www.cnblogs.com/Paul-Guderian/p/6782671.html
我对于点分治的理解:对于树上的一些问题,可以转化为答案只与当前根有关的问题,然后分治递归求解每一棵子树,统计答案。找的根应当是当前子树的重心,具体证明可以看上面的论文。
对于当前正在处理的树,这棵树的路径有两种情况:
经过根结点。
不经过根节点(在子树内)。
对于第二种情况, 我们可以递归求解转化为第一种情况来处理。于是问题变成求解第一种情况了。
这道题在cal统计答案的时候,因为我们在处理以 root
为根节点的子树的答案贡献的时候,求的是在不同子树中的距离小于等于k的点对(第一种情况),但是我们cal出来的是两种情况都包括的,因此需要减去第二种情况,即再cal一遍处理子树。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
typedef long long LL;
struct Edge {
int v, nxt, w;
} edge[N*2];
int n, k, head[N], tot, dep[N], son[N], dis[N], f[N], vis[N], sum, root, ans;
void Add(int u, int v, int w) {
edge[tot] = (Edge) { v, head[u], w }; head[u] = tot++;
edge[tot] = (Edge) { u, head[v], w }; head[v] = tot++;
}
void getroot(int u, int fa) { // 找重心
son[u] = 1; f[u] = 0;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa || vis[v]) continue;
getroot(v, u);
son[u] += son[v];
f[u] = max(f[u], son[v]); // 最大的子树
}
// 当前的树中除了以u为根的树以外的结点数
// 因为当以u为根的话,除了u为根的树的结点之外的所有结点在一个子树里面
f[u] = max(f[u], sum - son[u]);
// 找一个根节点使得最大的子树最小
if(f[u] < f[root]) root = u;
}
void getdeep(int u, int fa) {
// 处理出dep数组,也是当前点到根节点的距离的数组,dep[0]表示数量
dep[++dep[0]] = dis[u];
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(vis[v] || v == fa) continue;
dis[v] = dis[u] + w;
getdeep(v, u);
}
}
int cal(int u, int now) {
dep[0] = 0, dis[u] = now;
getdeep(u, 0);
sort(dep + 1, dep + 1 + dep[0]);
int res = 0, l = 1, r = dep[0];
while(l < r) {
// 对于连着l和r的两个端点,之间的所有点都可以使得距离小于等于k
if(dep[l] + dep[r] <= k) res += r - l, l++;
else r--;
} return res;
}
void work(int u) {
// 计算满足dep(i)+dep(j)<=k的数目
ans += cal(u, 0);
vis[u] = 1;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(vis[v]) continue;
// 减去满足dep(i)+dep(j)<=k并且i和j在同一个子树的数目(第二种情况)
ans -= cal(v, w);
sum = son[v];
getroot(v, root = 0); // 递归处理子树
// printf("root : %d\n", root);
work(root);
}
}
int main() {
while(~scanf("%d%d", &n, &k), n + k) {
memset(head, -1, sizeof(head)); tot = 0;
memset(vis, 0, sizeof(vis));
for(int i = 1; i < n; i++) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
Add(u, v, w);
}
sum = n, f[0] = INF, ans = 0, root = 0;
getroot(1, 0);
// printf("root : %d\n", root);
work(root);
printf("%d\n", ans);
}
return 0;
}
/*
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
*/
POJ 1741:Tree(树上点分治)的更多相关文章
- POJ 1741 Tree 树上点分治
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...
- poj 1741 Tree(点分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15548 Accepted: 5054 Description ...
- POJ 1741 Tree (树分治入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8554 Accepted: 2545 Description ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ 1741 Tree【树分治】
第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重 ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- POJ 1741 Tree ——(树分治)
思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述. 复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
随机推荐
- Angular路由守卫 canActivate
作用 canActivate 控制是否允许进入路由. canActivateChild 等同 canActivate,只不过针对是所有子路由. 关键代码 创建路由守卫 import { Injecta ...
- dumpbin判断windows程序是32还是64位(包括DLL)
http://blog.csdn.net/csfreebird/article/details/10105681 dumpbin /HEADERS gdal18.dll(or xxx.exe) 如果安 ...
- ListView、TreeView和DataGrid。
原文:ListView.TreeView和DataGrid. 1.ListView. ListView继承自简单的没有特色的ListBox,并使用View属性进行扩展.增加了对基于列显示的支持,并增加 ...
- WPF 数据库增删改查
<Window x:Class="DataBindingExam.MainWindow" xmlns="http://schemas.microsof ...
- Qt中嵌入Directx11(有句柄就可以)
最近要做个游戏场景编辑器,需要directx11配合gui框架使用,所以简单地弄了一个directx11嵌入到Qt窗体中的程序. 1 建立工程 建一个Qt的工程,配置好directx的包含目录和库目录 ...
- Delphi XE5 Android 调用手机震动(通过JObject测试是否支持震动)
源码如下: uses Androidapi.JNI.Os, Androidapi.JNIBridge; function GetVibratorArray(const AIntArr: array o ...
- layui弹出框打开第二次select内容无法显示问题
今天, 在使用layui弹出框的时候, 第一次进入select内容加载是正常的, 将弹出框关闭再次进入后select下拉框内容为空, 经排查是因为每次弹出窗口z-index都会改变, 弹出框的z-in ...
- C# 开机自动启动
if (ConfigurationManager.AppSettings["IsBoot"].ToString().Trim().ToUpper() == "TRUE&q ...
- 【转】ORACLE AWR报告
转自:http://blog.csdn.net/liqfyiyi/article/details/8236864 About Oracle AWR Oracle AWR is a powerful m ...
- Win10的UWP之标题栏的返回键(一)
原文:Win10的UWP之标题栏的返回键(一) 关于返回键,放在标题栏是目前较为完美的一种方案.继前一篇的Hello World,博主进行一些修改实现该方法. - - - - - - - - - - ...