这道题是一道点分治的题目,难度不大,可以拿来练手。

关键是对于找出来的重心的删除操作需要删掉这条边,这很重要。

还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz。

然后就A了。。。

每次点分治 分两种情况讨论一下就可以啦!

/w\...

#include <cstdio>
#include <algorithm> using namespace std; const int maxn = 56789;
const int INF = 1000000007; int getint()
{
int r = 0, k = 1;
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar() ) if (c == '-') k = -1;
for (; '0' <= c && c <= '9'; c = getchar() ) r = r * 10 - '0' + c;
return r * k;
} int n, m, k;
int h[maxn], siz[maxn]; struct edge_type
{
int v, next, w;
bool baned;
} edge[maxn * 2]; int tote = 0; void ins(int u, int v, int w)
{
edge[++tote].v = v;
edge[tote].next = h[u];
edge[tote].w = w;
edge[tote].baned = false;
h[u] = tote;
} int fgans, fgsiz; void get_fgans(int now, int fa, int size) {
int tmp = 1;
siz[now] = 1;
for (int i = h[now]; i; i = edge[i].next) {
if (!edge[i].baned && fa != edge[i].v) {
get_fgans(edge[i].v, now, size);
siz[now] += siz[edge[i].v];
if (tmp < siz[edge[i].v]) tmp = siz[edge[i].v];
}
}
if (tmp < size - siz[now]) tmp = size - siz[now];
if (tmp < fgsiz) {
fgans = now;
fgsiz = tmp;
}
}
int find_gravity(int rt, int size) {
fgsiz = INF;
fgans = rt;
get_fgans(rt, -1, size);
return fgans;
} int dis[maxn];
int zhan[maxn], zcnt; void get_dis(int now, int nd, int fa) {
dis[now] = nd;
for (int i = h[now]; i; i = edge[i].next)
if (!edge[i].baned && fa != edge[i].v)
get_dis(edge[i].v, nd + edge[i].w, now);
} void dfs(int now, int fa) {
zhan[++zcnt] = dis[now];
for (int i = h[now]; i; i = edge[i].next)
if (!edge[i].baned && fa != edge[i].v)
dfs(edge[i].v, now);
} int calculate(int rt) {
int ret = 0;
zcnt = 0;
dfs(rt, -1);
sort(zhan + 1, zhan + zcnt + 1);
for (int i = 1, j = zcnt; i <= zcnt; ++i) {
for (; j && zhan[i] + zhan[j] > k; --j);
ret += j;
}
return ret;
} int Ans = 0; void dfz(int now, int size)
{
int rt = find_gravity(now, size);
get_dis(rt, 0, -1);
Ans += calculate(rt);
int v;
for (int i = h[rt]; i; i = edge[i].next)
if (!edge[i].baned) {
edge[i].baned = edge[((i-1)^1)+1].baned = true;
Ans -= calculate(edge[i].v);
dfz(edge[i].v, siz[edge[i].v]);
}
} int main()
{
n = getint();
m = getint();
int u, v, w;
for (int i = 1; i < n; ++i)
{
u = getint();
v = getint();
w = getint();
ins (u, v, w);
ins (v, u, w);
}
k = getint();
dfz(1, n);
printf("%d", (Ans - n >> 1));
return 0;
}

BZOJ 3365 Distance Statistics 点分治的更多相关文章

  1. POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)

    题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...

  2. [BZOJ 3365] Distance Statistics

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3365 [算法] 点分治 [代码] #include <algorithm> ...

  3. POJ 1987 Distance Statistics 树分治

    Distance Statistics     Description Frustrated at the number of distance queries required to find a ...

  4. BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治

    BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治 Description     在得知了自己农 ...

  5. BZOJ.4184.shallot(线段树分治 线性基)

    BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...

  6. [BZOJ 4025]二分图(线段树分治+带边权并查集)

    [BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...

  7. bzoj 3365 [Usaco2004 Feb]Distance Statistics 路程统计(点分治,单调)

    [题意] 求树上长度不超过k的点对数目. [思路] 和 Tree 一样一样的. 就是最后统计的时候别忘把根加上. [代码] #include<set> #include<cmath& ...

  8. bzoj 3365: [Usaco2004 Feb]Distance Statistics 路程统计【容斥原理+点分治】

    统计在一个root下的两个子树,每个子树都和前面的运算一下再加进去对于这种需要排序的运算很麻烦,所以考虑先不去同子树内点对的算出合法点对个数,然后减去每一棵子树内的合法点对(它们实际上是不合法的,相当 ...

  9. BZOJ 3365: [Usaco2004 Feb]Distance Statistics 路程统计

    Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献 ...

随机推荐

  1. XHPROF相关内容

    定义入口文件 define('XHPROF_OPEN', 0); define('XHPROF_ROOT', '/home/www/xhprof/'); // 开启调试模式 建议开发阶段开启 部署阶段 ...

  2. vux 表单提交数据 返回后页面跳转

    ps:仅作参考

  3. node06-path

    目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-exp ...

  4. SQLite入门语句之HAVING和DISTINCT

    一.SQLite入门语句之HAVING HAVING 子句允许指定条件来过滤将出现在最终结果中的分组结果. WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建 ...

  5. mysql 分表

    1.分表,即把一个很大的表达数据分到几个表中,这样每个表数据都不多. 优点:提高并发量,减小锁的粒度 缺点:代码维护成本高,相关sql都需要改动 2.分区,所有的数据还在一个表中,但物理存储数据根据一 ...

  6. 【转】B-树和B+树的应用:数据搜索和数据库索引

    B-树 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树: ⑴树中每个结点至多有m 棵子树: ⑵若根结点不是叶子 ...

  7. 为何Redis要比Memcached好用(转)

    转载链接:http://blog.csdn.net/renfufei/article/details/40598889 GitHub版本地址: https://github.com/cncounter ...

  8. [Java 基础]sun.misc.Unsafe

    使用Unsafe可以干一些好事. Unsafe对象初始化 如果你的代码属于trusted的,可以直接使用下面的方式: public static Unsafe getUnsafe() { Class ...

  9. UWP 解决Webview在Pivot里面无法左右滑动的问题

    //为了解决webview在PivotItem里面,阻止pivot左右滑动. if (webView != null) { rootGrid.Children.Remove(webView); web ...

  10. C#最简单例子

    using System; namespace ConsoleApplication1 { class People { int age; string name; public string get ...