题意:给一个N个节点的带权树,求长度小于等于K的路径条数

思路:选取一个点作为根root,假设f(root)是当前树的答案,那么答案来源于两部分:

(1)路径不经过root,那么就是完全在子树内,这部分可以递归统计

(2)路径经过root,这部分可以通过容斥原理统计,具体见有关点分治资料。。。

点分治有个特点,当考虑的问题由根这个子树转为儿子的子树时,可以选取任意点作为新的根,只要它在儿子的子树内,这就使得我们可以通过选取特别的点使得树深度更小,这个点就是树的重心(在程序里面是不断找子树的重心),树分治的复杂度是O(NH+TH)的,其中H是树的深度,T是每层计算答案的复杂度,重心可以将树的深度变成O(logN)。

另外,整体看树分治的遍历节点的过程,发现它与建堆的过程十分相似,也就从侧面说明了树分治的复杂度。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <map>
#include <vector>
using namespace std;
#define X first
#define Y second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define mset(a, x) memset(a, x, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(b))
#define cas() int T, cas = 0; cin >> T; while (T --)
template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;}
template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;}
typedef long long ll;
typedef pair<int, int> pii; #ifndef ONLINE_JUDGE
#include "local.h"
#endif const int N = 1e4 + 7;
const int M = N;
const int inf = 1e9 + 7; namespace Edge {
int last[N], to[M << 1], w[M << 1], next[M << 1], cntE;
void init() {
cntE = 0;
memset(last, -1, sizeof(last));
}
void addEdge(int u, int v, int w) {
to[cntE] = v;
Edge::w[cntE] = w;
next[cntE] = last[u];
last[u] = cntE ++;
}
} int n, K; namespace Center {
int root, siz, son[N];
void init() {
siz = inf;
}
void getRoot(int cur, int fa, int total, bool used[]) {
son[cur] = 0;
int buf = 0;
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (to != fa && !used[to]) {
getRoot(to, cur, total, used);
son[cur] += son[to] + 1;
buf = max(buf, son[to] + 1);
}
}
buf = max(buf, total - son[cur] - 1);
if (buf < siz || buf == siz && cur < siz) {
siz = buf;
root = cur;
}
}
} void getDepth(int cur, int fa, int sum, vector<int> &vt, bool used[]) {
vt.pb(sum);
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i], w = Edge::w[i];
if (to != fa && !used[to]) getDepth(to, cur, sum + w, vt, used);
}
} int getAns(vector<int> &vt) {
sort(all(vt));
int maxp = vt.size() - 1, ans = 0;
for (int i = 0; i < maxp; i ++) {
while (i < maxp && vt[i] + vt[maxp] > K) maxp --;
ans += maxp - i;
}
return ans;
} bool used[N]; int work(int cur) {
used[cur] = true;
vector<int> total;
total.push_back(0);
int ans = 0;
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i], w = Edge::w[i];
if (!used[to]) {
vector<int> local;
getDepth(to, cur, w, local, used);
ans -= getAns(local);
for (int j = 0; j < local.size(); j ++) {
total.push_back(local[j]);
}
Center::init();
Center::getRoot(to, cur, local.size(), used);
ans += work(Center::root);
}
}
return ans += getAns(total);
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (cin >> n >> K, n || K) {
int u, v, w;
Edge::init();
Center::init();
mset(used, 0);
for (int i = 1; i < n; i ++) {
scanf("%d%d%d", &u, &v, &w);
Edge::addEdge(u, v, w);
Edge::addEdge(v, u, w);
}
Center::getRoot(1, 0, n, used);
cout << work(Center::root) << endl;
}
return 0;
}

  

[poj1741 Tree]树上点分治的更多相关文章

  1. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

  2. POJ1741 tree 【点分治】

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25286   Accepted: 8421 Description ...

  3. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  4. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  5. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  6. poj1741 Tree(点分治)

    题目链接:http://poj.org/problem?id=1741 题意:求树上两点之间距离小于等于k的点对的数量 思路:点分治模板题,推荐一篇讲的非常好的博客:https://blog.csdn ...

  7. [POJ1741]Tree(点分治模板)

    传送门 良心解析 其实以前在求某段序列上的区间统计问题时就碰到过类似于这样的思想. 当时的区间统计问题思路大致是这样: 选取一个点作为中间点,从这个点的左边和右边统计出满足条件的点对.然后当前的中间点 ...

  8. POJ1741 Tree (点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25772   Accepted: 8566 Description ...

  9. [POJ1741] Tree【树分治 点分治】

    传送门:http://poj.org/problem?id=1741 写的第一道树分治题,撒花纪念~ 对于每一对点对(i, j),它有三种情况: ① 其中一个是根节点.这种情况比较简单,直接加上就好了 ...

随机推荐

  1. Co-prime 杭电4135

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  2. C#客户端打印条形码

    第一种方法: 引用第三方插件文件zxing.dll // 1.设置条形码规格 EncodingOptions encodeOption = new EncodingOptions(); encodeO ...

  3. 米特运输——(dfs)

    米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储 存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市为首都.这N个城市由N- ...

  4. JS静态变量和静态函数

    本文链接:https://blog.csdn.net/u012790503/article/details/46278521 function A(){this.id = "我是AA&quo ...

  5. cocos2dx新建项目

    首先你得下载好cococs2dx,还有python2.x版本,还有vs2017 然后cmd在你Cocos2dx的路径下输入 python setup.py 然后你就回车回车回车 然后重新打开cmd 这 ...

  6. Python带你做个愉快的"动森"玩家! (超简单代码)

    最近Switch上的<动物森友会>可谓是炙手可热,它几乎算是任天堂版的<模拟人生>了,它的最新游戏<集合啦!动物森友会>(以下称“动森”)在发售后,取得了不错的媒体 ...

  7. Java ASM3学习(3)

    MethodVisitor ClassVisitor的visitMethod能够访问到类中某个方法的一些入口信息,那么针对具体方法中字节码的访问是由MethodVisitor来进行的 访问顺序如下,其 ...

  8. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  9. 配置IIS5.5/6.0 支持 Silverlight

    在安装完Silverlight1.1 Alpha后,要使自己的IIS服务器支持Silverlight的浏览还需要配置一下IIS网站的 Http头->MIME映射添加内容如下:扩展名        ...

  10. Docker安装和基本操作

    一.Docker安装 CentOS7安装Docker CE $sudo yum install -y yum-utils device-mapper-persistent-data lvm2 $sud ...