[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=3365

[算法]

点分治

[代码]

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 40010 int i,n,m,k,tot,len,ans,root,u,v,w;
int head[MAXN],weight[MAXN],size[MAXN],d[MAXN],dep[MAXN];
bool visited[MAXN];
char c; struct Edge
{
int to,w,nxt;
} e[MAXN<<]; inline void addedge(int u,int v,int w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
}
inline void get_root(int u,int fa,int total)
{
int i,v;
size[u] = ;
weight[u] = ;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (v != fa && !visited[v])
{
get_root(v,u,total);
size[u] += size[v];
weight[u] = max(weight[u],size[v]);
}
}
weight[u] = max(weight[u],total-size[u]);
if (weight[u] < weight[root]) root = u;
}
inline void dfs(int u,int fa)
{
int i,v,w;
d[++len] = dep[u];
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (!visited[v] && v != fa)
{
dep[v] = dep[u] + w;
dfs(v,u);
}
}
}
inline int calc(int u)
{
int i,j;
int ret = ;
len = ;
dfs(u,);
i = ; j = len;
sort(d+,d+len+);
while (i < j)
{
if (d[i] + d[j] <= k)
{
ret += j - i;
i++;
} else j--;
}
return ret;
}
inline void work(int u)
{
int i,v,w;
dep[u] = ;
visited[u] = true;
ans += calc(u);
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (!visited[v])
{
dep[v] = w;
ans -= calc(v);
root = ;
get_root(v,,size[v]);
work(root);
}
}
} int main()
{ scanf("%d%d",&n,&m);
memset(visited,false,sizeof(visited));
tot = ;
for (i = ; i <= n; i++) head[i] = ;
for (i = ; i < n; i++)
{
scanf("%d%d%d %c",&u,&v,&w,&c);
addedge(u,v,w);
addedge(v,u,w);
}
scanf("%d",&k);
size[] = weight[] = n;
root = ;
get_root(,,);
ans = ;
work(root);
printf("%d\n",ans); return ;
}

[BZOJ 3365] Distance Statistics的更多相关文章

  1. BZOJ 3365 Distance Statistics 点分治

    这道题是一道点分治的题目,难度不大,可以拿来练手. 关键是对于找出来的重心的删除操作需要删掉这条边,这很重要. 还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz. 然后就A了 ...

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

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

  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 3365: [Usaco2004 Feb]Distance Statistics 路程统计

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

  6. 【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计

    Description 在得知了自己农场的完整地图后(地图形式如前三题所述),约翰又有了新的问题.他提供 一个整数K(1≤K≤109),希望你输出有多少对农场之间的距离是不超过K的. Input 第1 ...

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

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

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

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

  9. 【poj1987】 Distance Statistics

    http://poj.org/problem?id=1987 (题目链接) 题意 给出一棵树,求树上距离不超过K的点对个数. Solution 点分治,同poj1741. 代码 // poj1987 ...

随机推荐

  1. Several Ideas on Perl List Context

    According to Beginning Perl Book published by Tsinghua Pub., the list context appears when you are t ...

  2. Oracle数据库安装与连接与简介

    Oracle数据库的安装 1.登录Oracle官网——试用和下载 2.同意协议--->file1 3.完成配置 4.测试连接:打开Oracle developer--->新建连接,注意用户 ...

  3. SQL增删改

    USE sqlxx CREATE TABLE ygb( sid INT, sname VARCHAR(20), sgender VARCHAR(2), sbirthday DATE, semail V ...

  4. 百面机器学习--跟课Task整理

    涉及书中内容:特征工程&&模型评估 不同的特征如何进行特征工程? 特征是原始数据作为输入,特征工程的结果,作为输入供算法和模型使用. 对于数值型特征,为消除特征间的量纲影响,应对特征进 ...

  5. 用SufaceGO加微软全家桶做个遥控车(一)

    作为一个dotnet技术的新手我是不好意思写帖子的,原因就是本人技术太水了,写出的帖子肯定会让人笑话.所以这次我是厚着脸皮写出这个帖子的,希望大佬们轻喷了.我的目标就是用SurfaceGo实现一个和我 ...

  6. 3.3、Ansible命令参数详解

    0.ansible 命令参数详解: [root@localhost ~]# ansible Usage: ansible <host-pattern> [options] Options: ...

  7. JavaScript(DOM编程一)

    在什么位置编写js代码 一般把js代码写在window.onload方法中 执行该方法时页面已经加载完毕,可以获取到所有的dom元素 --------------------------------- ...

  8. HDU 3567 Eight II

    Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 3 ...

  9. (5)全局异常捕捉【从零开始学Spring Boot】

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...

  10. nyoj 38 简单并查集的应用&最小生成树

    #include<stdio.h> #include<stdlib.h> #define inf 0x3fffffff #define N 600 struct node { ...